Monday, June 20, 2011

VIM tips

Vim is a shortcut for Vi IMproved and  it is a powerful console-based text editor.
This article briefly describes some useful operations and tips you can do in vim editor.


Vim has two basic modes which you can use: command mode and  writing mode (there is also third mode - visual, which lets you mark and copy text areas and fourth mode - recording, where you can record macros) . In the command mode you can type commands and navigate in the document, in the writing mode you can type into your document and navigate with arrows (arrow navigation doesn't work in vi and on some systems like Solaris. Other option how to navigate in your document is to use keys: h, j, k, l).

Opening files:
To open a file use vim <file>. Opening several files is possible with vim file1 file2, then you can switch between files with commands :wn[ext] and :wp[revious]. You can open more files in horizontal or vertical layout with vim -o file1 file 2 or vim -O file1 file2. Use CTRL+w followed by arrow key to move between opened files.
To open a new file in horizontal or vertical view use :new or :vnew.



Navigation:
The first thing to know is how to navigate in vim. You can use standard arrows or letters h (left), j (down), k (up), l (right). To get at the top of a document use gg to get at the bottom use G (Shift+g). To move cursor between words use letters w and b (or e). You can also use Page Up and Page Down to move one screen up or down. To jump to 15. line use :15. This will be enough to know about how to navigate in a document.

Writing:
Now to start writing in document: hit i to start writing before cursor or a to start writing after cursor. Use I to write at the beginning of the line or A to write at the end. To start writing on the new line type o or type O to start newline before the cursor position.
You can switch between INSERT mode and REPLACE with the Insert key. If you want to replace just a single character under cursor, in the command mode type r and a letter or symbol. You can start writing in replacement mode directly with R.

Deleting:
To delete whole line use dd, to delete only single word type dw. If you want to delete line from the cursor position to the end use D. To delete single character under cursor use x.

Here I'd like to mention that you can use number before the commands. e.g to delete 5 lines use 5dd, or to delete 6 characters use 6x. You can also combine several commands, e.g. to delete from the cursor position to the end of file use dG.
Sometimes you need to delete several words or string from cursor position to some character you choose. E.g. use df <space> to delete from current position to the next space, or df " to delete to the next double quote.

Copying and pasting:
To copy whole line use yy. Again you can use number before this command to copy several lines, e.g. 6yy. To copy a word use yw and to copy a character use yl. This commands work only within the document, they don't use system clipboard. You can paste with p after cursor and P before cursor.
To copy and paste from the system clipboard select the text and press CTRL+SHIFT+C (this is standard shortcut for copying from the console) to copy and SHIFT+Insert (or CTRL+SHIFT+V) to paste (remember to enter writing mode before pasting).

Inserting file or command output to vim:
To insert a file to vim document use :r FILE, this will insert content of FILE after the cursor. To insert command output use :r !command.

Undo, redo and repeat:
To undo last operations use u. To repeat last operation use ".". CTRL+r will do redo.

Searching and replacing text:
To search in the document type /<string to search> (for forward search) or ?<string to search> (for backward search) or asterisk or double hash (*, #) to search word under cursor forwards or backwards. Press enter to search and use n to search next match and N to search previous match.
To replace some string use :s/string/replacement/ to replace first match on the current line or :%s/string/replacement/g to replace string in the whole document (the g at the end means that all matching strings in line will be replaced, without g only the first match will be replaced).
To find and replace string in file with prompt whether to substitute or not, use :%s/old/new/gc (note the 'c' for prompt).
By default, search is case sensitive. To make it case insensitive type :set ignorecase or :set ic (or place set ignorecase line in your .vimrc), use :set noic to disable it.

Code formatting:
If you are writing C or Java codes (works also for other languages) you can automatically format them. First you might want to set following: :set tabstop=4 softtabstop=4 shiftwidth=4 to set proper tab width. Then you can easily format the whole code with command gg=G.
To find matching parentheses use %.

Other tips:
To keep formatting of the pasted text (from clipboard) use :set paste before entering insert mode (:set nopaste to turn off).
Turn on syntax highlighting with :syntax on (:syntax off to turn off).
To turn off highlighting after e.g. searching for string use :nohl.
To comment several lines use :.,+15 s/^/#/ this will comment (replace line beginnings with '#') 16 lines from the cursor position (including cursor position).
To show line numbers use :set number (or just :se nu). To hide line numbers use :set nu! or :se nonu.
To increment or decrement number under cursor: CTRL+a or CTRL+x

Close and Save:
To close the document use :q or to close without saving changes :q!. To save document use :w or to override the read only attribute use :w!. To close and save the document use :wq or ZZ.
If you edit file, you will cope with situations when you're done with editing and want to save changes but then you'll find out you don't have permissions to save the file. You can save it with following command :w !sudo tee %.

VISUAL MODE
In visual mode, you can select text and provide some action on multiple lines, e.g. you can select multiple lines and insert # at the beginning.

To start visual mode press v or CTRL+v to start visual mode in block mode or SHIFT+v to start it in line mode. Then you can select text and use SHIFT+i or SHIFT+a to add text before/after cursor, press x to delete selection, press y to copy selection or press : and line like :'<,'> will appear, then you can type w FILE to save selected text to FILE (this applies only to whole lines).
You can type gv to select last visual block.

RECORDING MODE
In this mode you can record what you do and apply it to another document. This is useful when you have to edit several files in the same way.
To start recording press q followed by letter or number which represents the recording register (e.g. qa).
Then do your job and stop recording with q. Now you can repeat the macro with @a or 10@a to repeat it 10 times.
To empty the register use qaq.

To edit the register:
:new      # new buffer
"xp       # paste register x into the buffer
[edit the keystrokes]
<Esc>     # return to normal mode
0"xy$     # go to beginning of line; into register x, yank to end of line
:bd!      # delete the new buffer without saving
@x        # execute modified recording

For more info refer to vim wiki.
 

No comments:

Post a Comment