Vim

Vim

My personal list of vim-shortcuts/-commands.
Thousands of such lists exists, but nothing is like noting it down yourself ;-)
Write one for yourself, this is my list :P

And work your way through one of the tutorial tools like vim-tutor, the vim-help itself, etc.!

Random updates to this list

# : search for full current word, regardless of where in word the cursor is.

Esc-replacement

Ctrl-[ : acts as Esc

Extremly useful, otherwise clicking Esc forces to leave normal position of hands.
This is hardly mentioned in the guides I went through.

Tabs

Next to splitting the window, tabs are very useful to work on several files.
Click here to read more about using tabs or simply type :help tabpage in vim for all the details.

Basic Movement

h : left
j : down
k : up
l : right

Advanced Movement

w : word start
e : word end
0 : goto start of line (0 motion)
^ : first non-blank character of line
G : end of file
gg : start of file
1G : same as gg, goto line 1
13G : goto line 13
Ctrl-g : show line count at bottom
) : Forward to beginning of sentence
( : Backwards to beginning of sentence
} : Forward to next blank line (paragraph)
{ : Backward to next blank line (paragraph)

Working with several files

:split name : open “name” and split window horizontally
:vsplit name : open “name” and split window vertically
Ctrl-w,w : switch ot next window
Ctrl-w,x : swap window with next
Ctrl-w,r : rotate windows
Ctrl-w,> : increase windows size
Ctrl-w,< : decrease windows size
30Ctrl-w,> : increase size by 30 chars
:qa : close all windows and exit

Inserting

i : insert at cursor
a : append after cursor
ea : insert after word
I : insert at beginning of line
A : append to line
p : paste
o : new blank line after current, insert there
O : new blank line before current, insert ther

Replacing, Changeing

r : replace character, example: “rt” replace with “t”
c : change
ce : change word (change to end of word)
cw : same as ce, preserves space before next word! Unexpected but useful
behaviour.
c$ : change to end of line

Change will actually delete and then go in insert-mode.

Exit, File Writing

:q : exit
:q! : exit without saving
:w : save
:wq save and exit
:w name.txt : safe as “name.txt”

Cutting/Deleting

x : delete character
dw : delete word (from current position forward)
d$ : delete to end of line
dd : delete line

So “dw” is actually a combination of “d” (delete) and “w” (to start of next word) – cool!

Copying

y : yank
yw : yank (copy) word
yb : copy word backwards, cursor jumps
y0 : copy line backwards, cursor jumps
y$ : copy to end of line
yy : copy line
5yy : copy 5 lines
3y} : copy 3 paragraphs

Undo, Redo

u : undo
Ctrl-R : redo (not “U”)
U : set last line to original state (a new change for undo, best not used to
avoid confusion!)

Count Numbers

This really rocks, best explained by examples:
8e : jump to end of 8th word
3w : jump to start of 3rd word
5$ : jump to end of 5th line
d3w : cut/delete 3 words (actually: to start of 3rd word)
d8d : cut/delete 8 lines

I just cut 8 lines with “d8d”, moved cursor and pasted with “p”, wow!

Searching

/ : search forward
/word : search “word”
? : search backwards
?word : search backwards for “word”
n : next (search forward)
N : next backwards

Search & Replace, Substitution

:s : substitute on single line
:%s : substitute in document
:20,40s : substitute between lines 20 and 40
examples:
:s/old/new/ : Substitute first “old” with “new” on current line
:s/old/new/g :Substitute all “old” with “new” on current line
:%s/old/new/ : substitute first “old” with “new” in document
:%s/old/new/g : substitute all “old” with “new” in document
:20,40s/old/new/g : substitute all between lines 20 and 40
:s/old/new/c : “c” triggers confirmation

Execute external commands

Useful to execute a command without leaving vim.
Might also be possible to import external output into vim (todo: research)
:!command
Example:
:!date : temporarily drop to shell, execute date, wait for “Enter”, then
return to vim.
Better example:
You opened a file, edited whatever but forgot that it’s read only.
If you can’t open a new shell, you might even have to exit vim, thus losing all changes.
So … in vim, type “:!sudo chown user file” (or similar), then you can save the file without leaving vim.

Operators

$ : to end of line

Misc

:set number : show line numbers
:set nu : same
:set nonumber : unset line numbers
:set nonu : same

Usefull config

Paste toggle
From http://vim.wikia.com/wiki/Toggle_auto-indenting_for_code_paste
Pasting with indents can conflict with vims own indentation.
This fix works around it.
Put this in /etc/vim/vimrc:

set pastetoggle=<F2>

Now you can toggle indenting on/off with F2

Tab Width
Displayed tab width is a personal preference, but please USE tabs not spaces!
I like this:

set tabstop=4

to display/indent tabs as 4 spaces.

My current additions
So far I appended this to /etc/vim/vimrc:

" paste-mode from external programm
set pastetoggle=<F2>
" displayed tab width
set tabstop=4
" indent tab width
set shiftwidth=4
" color scheme desert for nice 256 colors
colorscheme desert

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: