gVim Tips

Vim/gVim editor – some useful commands.
Following gVim commands are frequently used ones and the tips are meant for beginners. I’ll add more tips/commands to this section as and when I get time.
Download (free) vim software from http://www.vim.org/download.php

Open file:

vim <filename>
vim *.v  means open all .v files in the current directory

Save/Quit:

Save
:w
Quit without saving file
:q
Save and Quit
:wq
Forcefully Save
:w!
Forcefully Quit
:q! {it will ignore all changes after last save}
Forcefully Save and Quit
:wq!

Edit commands:

Insert  char/s
I
Insert at start of line
I
Insert line after
O
Insert line before
O
Append after cursor
A
Append at the EOL (End of Line)
A
Delete char
 x {delete n char -> nx}
Delete word
 dw {delete n words -> ndw}
Delete line 
dd {delete n lines -> ndd}
Delete lines n1 to n2
:n1,n2 d
Delete rest of the line
D
Change word 
cw {change n words -> ncw}
Change entire line
Cc
Change rest of the line
C
Replace char 
r {replace n chars -> nr}
Replace till <ESC>
R 
Swap char postion
xp
Undo last edit
U

Search and Substitution:
Search
/<search_item>
Search and Substitution
:%s/item1/item2/options OR :n1,n2s/item1/item2/options
  Substitute first occurrence item1 by item2
  Options:
   
g -> global change
   
c -> confirm each change

Example:
Suppose you want to replace every occurrence of the word "idiot" with the word "manager" and you want to review each change.

execute the command
:%s/\<idiot\>/manager/gc

This command will make the change and pause after each change to give you a chance to confirm it. You can enter "y" to accept the change or "n" to not accept it.
The parts of this command are:

:
Enter command mode
%
Perform this command on all lines i.e. from first to last line
s
substitute command
/\<idiot\>/
Search text: "idiot"
  \< to match a word start
  \> to match the end of a word
/manager/
Replacement text: "manager"
gc
g -> global change
c -> confirm each change

If you don’t want to confirm each change,

execute the command
:%s/\<idiot\>/manager/g

If you want to make changes between two lines, say 3rd and 95th lines (both inclusive).

execute the command
:3,95s/\<idiot\>/manager/g

Copy, Cut and Paste:

Yank (Copy)
yy {yank n lines -> nyy}
Cut can be of characters -> nx, words -> ndw, lines -> ndd (or n1,n2 d)
After yank (copy) or cut, position the cursor properly and use p to paste the copied/cut stuff.

Put
p {equivalent to paste}

Move text within a file:
Suppose you want to move a paragraph, from one place to another, within a file.


Move the cursor to the top of the paragraph you want to move.
ma
Place a mark named "a" at this location. (Vim will give you no indication that this command has been executed. In other words, the screen will not change.)

Move the cursor to the bottom of the paragraph to be moved.
d'a
Delete to mark "a". This puts the deleted text in a cut buffer.

Move the cursor to line where the paragraph is to go. The paragraph will be placed after this one.
p
Paste the cut stuff in below the cursor.

Instead of placing a mark and do deletion, you can also use either use visual mode (v or V) or delete lines with ndd. Pasting part using p remains same.
Move text to another file:
Suppose you want to move a paragraph from one file to another.


Move the cursor to the top of the paragraph you want to move.
v
Start visual mode. If you want to copy a block of full lines, use V to go start Visual Line Mode

Go to the bottom line to be copied. The text to be copied will be hightlighted
d
Perform a visual delete. In other words delete the highlighted text.
:sp
Open another window containing the second file. (This is the file in which the text is to be inserted)
p
Paste the cut stuff after the cursor.

Search Patterns (Regular Expressions):

Beginning of line
^
End of line
$
Any character
. {Dot}
Zero or more of previous character
*
Matches any character from A to Z 
[A-Z]
Matches any character from a to z
[a-z]
Matches any character from 0 to 9 
[0-9]
Matches a, b, or c
[abc]
Matches any character BUT a, b or c
[^abc]
Escape character for literal
\ / $ . ^ [ ' & * | ~  \

Trimming the blanks off an end of line:
:%s/[ <tab>]//g
:
Enter command mode
%
Perform this command on all lines i.e. from first to last line
s
substitute command
[ <tab>]*
Search for spaces and tabs;
* tells vim that the spaces/tabs can occur more than once
//
Since there is nothing in replacement text, spaces/tabs would be effectively removed.
gc
g -> global change

You have a list of names in the form:
            Last, First
And you want these to change into following form:
            First Last
It can be done with one command:
    :%s/\([^,]*\), \(.*$\)/\2 \1/
Explanation: There are always 3 seperators - :%s///
In between ///, we have to put old and new strings.
Here, old string is \([^,]*\), \(.*$\) and new strings is \2 \1

\([^,]*\), \(.*$\)
Old string. This regexp can search to "Last, First". You can try this.
The \( ... \) delimiters are used to inform the editor that the text that matches the regular expression side is to be treated special.
\([^,]*\)
The text in the first \( ... \) i.e. \([^,]*\) is assigned to \1 in the replacement text.
[^,] means anyting but a comma, * means more than one => any bunch of characters that doesn't include comma.
First regexp \([^,]*\) is followed by ,<space>. This should exactly match in the old string.
\(.*$\)
The second set of text inside \( ... \) i.e. \(.*$\) is assigned \2 in the replacement text.
.* means match anything upto the end of line : $