Personal tools

Vim

From MohidWiki

Revision as of 21:23, 9 January 2008 by 192.168.20.177 (talk) (Configure VIM)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Here are some useful references for using Vim, the powerful customizable wrist-friendly text-editor:

Basic editing

[ESC] to return in normal mode.

i key to insert text.

h, j, k, l keys for moving around the text.

x key to delete a character.

rx keys replaces current character with the x character.

u key to undo an editing operation.

U key to undo all editing operations performed to the current line.

[Ctrl]-r keys to redo (undo the undo).

Motion keys

$ key to move to the end of line.

0 key to move to the beginning of line.

w key to move to the first character of the next word.

e key to move to the last character of the current(or next) word.

Operators

Operators are useful to perform operations a repeated number of times. Syntax is

Op [count] motion

wher Op is the operator, [count] is the number of times, and motion is the scope of the operation.

Delete

d key.

dd deletes the whole line.

Put

Whenever a delete operation is performed, the deleted characters are stored in the put variable.

p key.

example: 'dd' followed by 'p' performs a cut/paste one-line operation

Change

c key.

Cursor location

[CTRL-g] shows the cursor current position in text.

gg Goes to the beginning of text.

G Goes to the end of text.

[line]G goes to the given line.

Search

/text keys finds the first occurence of text in the buffer from the cursor.

?text keys finds the first backward occurence of text in the buffer from the cursor.

n key finds the next occurence of text.

N key finds the previous occurence of text.

[Ctrl-o]

[Ctrl-i]

Replace

s/old text/new text replaces first occurence in line.

s/old text/new text/g replaces all in line.

#,#s/old text/new text/g replaces all between lines # and #.

%s/old text/new text/g replaces all in buffer.

%s/old text/new text/gc replaces all in buffer but prompts before each replacement.

Add ftplugin

Copy the plugin into the $VIM/ftplugin directory then type at the vim prompt

:filetype plugin on

Configure VIM

Edit the /etc/vimrc file or the ~/.vimrc in linux or the $VIM/_vimrc file in windows.

Space indentation

This will correctly set <tabs> to 4 spaces.

set shiftwidth=4        " defines the indentation characters
set tabstop=4           " defines the indentation characters
set expandtab           " defines the indentation characters

Tips & tricks

# performing edits on multiple files (pipe separates commands) 
$ vim -c "argdo %s/ABC/DEF/g | w" *.txt  
> vim -c "argdo %s/FOO/BAR/g | update" `grep -l FOO *`

Windows users, you might want to check out Notepad++ instead of using vim.

External References