vi Basics

Improvements? Suggestions? email lif.zone.main+dna@gmail.com

Minimal

Spark's shortcuts

g - Open gvim (from bash)

Files

:q - quit
:w - save
:x - save and quit
:wqa - save and quit on all open windows

Jumping to lines

401gg - line 401
gg - begining of file
Shift+G - end of file

Editing

I - insert mode (to add the word hello at the possition of the cursor type ihelloESC)
Shift+A - add text at the end of the line (add ; at the end of this line: A;ESC)
Shift+I - adds at beginning of line
O - add a new line and goto insert mode (Shift+O add above the cursor)
ESC - exit edit mode
x - delete a char
dd - delete a line (dw delete a word)
yy - copy a line into buffer (yw copy a word)
p - paste buffer (Shift+P paste before cursor)

Basics

Editing

"*y - copy to Windows buffer yunk
"*p - paste from Windows buffer
>> - indent section one tab (<< unindent)
= - automatically indent section according to language rules

Movement

W - next word (Shift+W search for whitespace), b and B for back word, e - end of word (E search for whitespace)
]] - next function ([[ back)
][ - end of function ([] back)
F+<char> - next char (Shift+F back), T and Shift+T to get to before next char

Visual

V - select area
Ctrl+V - select area in column mode

after area is selected:
D - delete
>> - indent section
Y - copy to buffer (yunk)
Ctrl+C - copy to Windows Buffer (you can paste with Windows applications)

Formatting

gq - format (wrap text) selected area
gqq - format (wrap text) current line
gqap - format current paragraph
Shift+J - merge current line with the following line

'Macro' like and misc

[n][operation] - perform an operation n times
examples:
5dd - delete 5 lines
29[down arrow] - go down 29 lines
U - undo
Ctrl+R - redo

Using VIM from telnet

H J K L - left down up right
Ctrl+F - page down (Ctrl+B - page up)

Search and Replace

* - search for the current word the cursor is on (same as Ctrl+F3 in MSDEV)
N - find next (Shift+N back)
/main - search for main (such as main_app)
/\<main\> - search for main as whole word
/\<print - search for words starting with print (such as printf)
:se ic - set the option of ignorecase
/main - after the set option, search for main and ignore case (such as Main) (:se noic - this option is: no ignorecase)
:%s/printf/kprintf/g - search in all file (% - all the file) for printf, replace with kprintf, replace all occurrences (g) - without the g option, if there are some occurrences in the same line - only the first one will be changed.
other options: ignore case (i), conditional - ask yes/no (c).
:%s/add(a,\([a-z]\+\))/add(a, \1)/g - search in all file all occurrences of the 'add()' function and make sure it's second variable has a space after the comma. \( and \) group the second variable pattern and refer to it inside the replacement pattern by its special number \1.

Advanced

In insert mode

Ctrl+X - auto completion.
Ctrl+P - complete word from current file and included files (Ctrl+N same but without included files)
Ctrl+D definition of macro
Ctrl+F file name

Buffers

:e app.c - open app.c in a new buffer
gf - when standing on a file-name, opens the file in a new buffer
:b 2 - goto buffer number 2
:buffers - list open buffers
:bd - close buffer
:b# - goto last buffer
Ctrl+6 - goto last buffer
:set hidden - Allow switching buffers without saving.

Compiling and handling errors

Shift+K - show the man page for current word (same as F1 in Windows)
:make - run make (gmake), and show results
:cc - goto source of current error (:cn next error, :cp previous error)
:cl - list the

Graphical diff

startup:
gvim -d FILE1 unknown

Ctrl+W+W - toggle between windows

Make the current window part of the diff windows:
:diffthis main.c~

View the same buffer in diff mode in one window and "normal" in another:
::!cp % tempfile
::diffsplit tempfile

To force the differences to be updated use: :diffupdate

Jumping to diffs:
[+C (or shift+tab)- Jump backwards to the previous start of a change.
]+C (or tab) - Jump forwards to the next start of a change.

Diff copying:
:[range]diffget - Modify the current buffer to undo diff with another buffer.
:[range]diffput - Modify another buffer to undo diff with the current buffer.

Spark specific extensions (after installing vimrc_zon)

bash alias gvim->g: $ g a_file.txt
zdiff: executing cvsdiff on all modified (M) files