≡ Menu

Vim Tutorial and HowTos

Vim is a text editor. It was created as an extended version of the vi editor, with many additional features designed to be helpful in editing program source code ( rss/feed ) .

Q. I use vi / vim as IDE (Integrated development environment) a software development system using Ubuntu Linux. How do I auto complete file name under vim / vi text editor while editing text file or java / c / python program? I can auto complete file nameunder bash shell by hitting [TAB] key.
[click to continue…]

How do I print line numbers on the printer for my c / java / c++ code written using vi or vim text editor under Linux / UNIX like operating system?
[click to continue…]

How To Write First UNIX Korn Shell Script Program

Can you tell me how to write a UNIX korn shell script program? I'm new to UNIX scripting and HP-UX UNIX operating system.
[click to continue…]

Q. I’ve just installed CentOS Linux server and started to use vi text editor to make changes to config files. How do I perform search and replace operation using vi / vim text editor?

A. vi (vim) is not difficult to learn, the vi editor is well known and used for both writing code and editing config files.

VI search and replace command format

Simple format is as follows:
%s/old-string/new-string/

VI search and replace command examples

Let us say you would like to find a word called "foo" and replace with "bar".

First hit [Esc] key

Type : (colon) followed by %s/foo/bar/ and hit [Enter] key.
:%s/foo/bar/
Above command will replace first occurrence of word foo with bar on all lines. The % is shorthand for all lines.

To replace all occurrences of word foo with bar on all lines, use the g option (which indicates all occurrences on a line).
:%s/foo/bar/g

Note that the g can be replaced with a number 1,2,...N to change only the n'th occurrence on each line.

Use find and replace on line ranges (match by line numbers)

You can also make changes on range of lines i.e. replace first occurrence of foo with bar on lines 5 through 20 only, enter:
:5,20s/foo/bar/

Following command will replace first occurrence of foo with bar starting at the current line for the next 100 lines:
:.,+100s/foo/bar/

Match by words

Finally, you can match by words i.e. replace first occurrence of foo with bar starting at at the next line containing a word "test":
:/test/s/foo/bar/g
As usual you can specify ranges:
:/test/,/guest/s/foo/bar/g

Please note that all search/replace commands should be start with the [ESC]: keystroke combination only.

FreeBSD install VIM text editor

Q. Quick question – How do I install VIM text editor for my FreeBSD server? Currently it has old version of vi. I like vim as it is a highly configurable text editor and efficient one.

A. VIM (Vi IMproved ) is not installed by default under FreeBSD. Vim is a text editor that is upwards compatible to Vi. It can be used to edit all kinds of plain text. It is especially useful for editing programs. You can install vim binary package or use FreeBSD's ports system.

Task: Install vim binary package

Type the following command to install vim lite version:
# pkg_add -r -v vim-lite

You can also use FreeBSD ports system to install vim:
# cd /usr/ports/editors/vim-lite/
# make install clean

To start vim type command:
$ vim

You can also create a alias using alias command:
$ alias vi='vim'

Add above alias to your shell start up file such as ~/.bashrc / ~/.cshrc

Task: Setup global vimrc file

You need to copy default vimrc file from /usr/local/share/vim/vim71/vimrc_example.vim to /usr/local/share/vim:
# cp /usr/local/share/vim/vim71/vimrc_example.vim /usr/local/share/vimrc

Task: Personal ~/.vimrc file

You can customize ~/.vimrc as per your requirements. Here is my own file:
$ cat ~/.vimrc
Sample output:

set nocompatible " must be the first line
filetype on
filetype indent on
filetype plugin on
set laststatus=2
set statusline=%<%f\%h%m%r%=%-20.(line=%l\ \ col=%c%V\ \ totlin=%L%)\ \ \%h%m%r%=%-40(bytval=0x%B,%n%Y%)\%P

Q. I am using vim under Ubuntu Linux and whenever I type following command:
syntax on

I’m getting an error which read as follows:
E319: Sorry, the command is not available in this version: syntax on

How do I fix this problem?
[click to continue…]

How do I turn on or off color syntax highlighting in vi or vim text editor on Linux or Unix like operating systems?
[click to continue…]