Vim

Dr. Tom V. Mathew

February 7, 2013

Vim is very good and professional text editor. We need editors to create source for c/c++/fortran programming. Use the links at the section 3 to learm VI/VIM.

1 Frequently used commands

1.1 Help

home/user> vimtutor at the linux command line to get tutor file
:help inside vim to get general help
:help set get help on the command set
Note: These commands are cryptitic and for a begineer refer some online help 3.

1.2 Very basic

vim file.txt open the file with file.txt
i start insert text mode command
my text type some test
Esc escape from the insert mode
:wq write to the file and quit
Note: These commands are cryptitic and for a begineer refer some online help 3.

1.3 invisible commands

command description
yy copy one line
4yy copy 4 line
dd delete one line
4dd delete 4 line
p paste after the current cursor location
P paste before the current cursor location
g* keep the cursor in a word and them issue this command to search for that word forward
g# keep the cursor in a word and them issue this command to search for that word backward
gq} keep the cursor at the beginning of a paragraph and issue the command to format that paragraph
gq) keep the cursor at the beginning of a line and issue the command to format that line
. repeat the previous command

1.4 search commands

command description
/car To search for word car
n Repeat the previous search forward
N Repeat the previous search backwar
:%s/xx/yy/c search for the word xx and replace with yy. It ask a confirmation every time
:%s/xx/yy/g search for the word xx and replace with yy. It is gloable replacement with out confirmation
:g/xx list all the lines that contains the word xx

1.5 visible commands

command description
:77 cursor goes to the line 77
:$ cursor goes to the last line
:r file.txt read the contents of file.txt to the current file
:split split the file into two
:edit f1.c open f1.c for editing
:next show the next file it it is opened
:next show the previous file it it is opened
:vsplit split vertically
:vsplit f2.c split vertically v2.c
:  

1.6 control commands

command description
[ctrl+] y copy from the above line
[ctrl+] e copy from the below line
[ctrl+] w w switch to next window
[ctrl+] w + increase the size of the window
[ctrl+] w = set the size equal to all the windows

1.7 command line options

command description
vim f1.c f2.c open f1.c and f2.c
:next show the next file
:n show the next file
:previous show the previous file
:pre show the previous file
vim -o f1.c f2.c open f1.c and f2.c in each window

2 Advanced commands

2.1 Copy buffers

"a3yy copy three lines to buffer a
"ap paste buffer a (We can have maximum 26 buffers)
"A3yy append three lines to buffer a/A

2.2 More search options

:g/\ <mon\> list all the lines that contains the word mon, but does not show monday and summon. That is full word only.
:set ignorecase Ignore case in search
/xx\|yy To search for word xx OR yy in a line
/.*xx\&.*yy To search for word xx AND yy in a line
/xx.*yy To search for all characters from xx till yy
:10,+5 s/xx/yy/g search for the word xx from lines 10 to 15 and replace with yy
:1,. s/xx/yy/g search for the word xx from lines 1 to the current line and replace with yy
:.,$ s/xx/yy/g search for the word xx from lines from the current line till end of file and replace with yy

2.3 More search options

vim -s sfile file.tex open file.tex and execute the script in sfile The script file coule be any file that contains vim commands.
:s/apple/orange/g
:wq
This could be the content of a script file. This can be save in file named say sfile
vim *.txt
qq
:%s/orange/apple/g
:wnext
q
@q
99@
To search in multiple file (say all txt files) and do the changes

2.4 C program

> Indent the code
:set ai Set auto indent while coding
% issue this at { or ( and this command will find the closing match
* issue this at a word and this command will search similar words

2.5 .vimrc

This file stores the settings and abbreviations and act as default initilizing file. My .vimrc looks like this and can be downloaded. My .vimrc This is located at my home area. You will have problem in viewing the new line character here.

3 Links

  1. Mastering the VI editor
  2. An Introduction to Vim
  3. Mastering the VI editor Univ of Hawai.
  4. A quick Guide to VIM
  5. VIM FAQ
  6. Some good tips A single html files
  7. Tips and tricks with Vim. Collection of large tips submitted by various people.
  8. Vim Official documentation Official and exhaustive documentation for an advanced user.
  9. Home page of Bram Moolenaar, the man who develped VIM!


Prof. Tom V. Mathew 2013-02-07