Kevin van Zonneveld

On Development and Internet System Engineering

Learning Vim

| Comments

In an attempt to familiarize myself with the unfamiliar, I decided to build a fun side-project in Ruby and Vim. Effectively learning a new language, framework, and editor.

Coming from Nano, Quanta, Eclipse PDT, TextMate, Netbeans; I found (Mac/g)Vim is big a step, and first two weeks you should not expect to be productive.

But after some persistence, I’m now faster in Vim than I was in my previous editors. Except for NetBeans still. Maybe that changes as I get better, but having an editor that understands your code is also a powerful thing. Scope-aware refactoring and jumping to declarations make that I keep both NetBeans and Vim around. Cause Vim on the other hand let’s you navigate and hack on text like no other.

Investing in a tool like this pays off. For life.

Cause as so long as computers can’t read our minds, we’re better of maximizing the efficiency of our typing. If your brain has to wait for your hands to transfer the message, you’re just throwing away time & creativity flow.

Here are the resources that helped me get started with Vim.

Vim screencast tutorial (awesome series):

Other screencasts:

More resources:

Keyboard shortcuts

There are many Vim cheat-sheets out there, better than this one (see resources above). But still, I’ll continuously log useful shortcuts here so I won’t regress : )

Mode switching

1
2
3
4
5
^[  exit mode
:   enter command mode

i   enter insert mode before cursor
a   enter insert mode after cursor

Command mode

1
2
3
4
5
6
w [filename]  write file
e filename    edit file
e!            reload current file

<range>s      substitute
.             repeat commands

Normal mode

This is the mode you should generally be in. Don’t stick around in others longer than necessary.

Movement / Motions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
j  move down
k  move up
h  move left
l  move right

0  move to line start
^  move to first char in line
$  move to last char in line

}  move down a paragraph
{  move up a paragraph

^d down a page
^u up a page

gg move to top
G  move to bottom

#  prev word like current
*  next word like current

%  move to matching closing/starting tag/comment/brace/statement
"  move to your last edit

b  back a word
w  to next word
W  to next WORD (space terminated, ignore e.g. commas)
e  to end of the current word
E  end of the current WORD (space terminated)

Manipulation

1
2
3
4
5
6
7
8
9
10
11
12
u          undo
x          delete character (and saves to clipboard)

c<motion>  change (enters insert mode)
y<motion>  copy (yank)
d<motion>  cut (delete) (and saves to clipboard)

p          paste after cursor
P          paste before cursor

<          unindent
>          indent

Search & Replace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/   start search down the document
?   start search up the document
^M  stop  search

n   move to next occurance
N   move to prev occurance

f   find symbol after cursor
F   find symbol before cursor
;   move to next occurance of symbol
t   find until symbol after cursor

m<bookmark> create bookmark. e.g.: m1
`<bookmark> jump to bookmark (exact cursor position)
'<bookmark> jump to bookmark (beginning of line)
`.          jump to last edit (exact cursor position)
'.          jump to last edit (beginning of line)
:marks      show bookmarks

Ranges

1
%  entire document

Combined

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ctK                 change until next capital 'K'
yy                  copy (yank) line
d2e                 delete till the end of the second word

%s/name/kevin/g     change all occurances of name to kevin in entire document
3,6 s/name/kevin/g  change all occurances of name to kevin from line 3 to 6

D                   delete til end of line
Y                   copy (yank) til end of line

ct,                 change until ','
cf,                 change until & including ','

>7j                 indent this line & 7 next lines
<}                  unindent until next paragraph
3dd                 delete 3 lines (3x delete current line)
12x                 delete 12 characters

Insert mode

1
2
^n                  Next autocomplete suggestion
^p                  Previous autocomplete suggestion

Shell filtering

1
2
3
4
5
!            enter mode
!!<command>  apply filter on current line
!}sort       sort paragraph
:%!sort      sort entire file
!G           sort until bottom

Alright, that’s all I got for now. What are your experiences with Vim?

Imported comments

These were imported from my old blog. Please use disqus below for new comments

nelson on 2012-05-29 03:43:58
I also had some trouble to learn vim. Each feature I learned, though kicked away another editor. It is today my favorite editor.

It surely pay off to learn a powerful tool like this.

Bryce Verdier on 2012-02-07 01:20:39
I have to agree with Tin Dalos; how did you (efficiently) admin linux boxes without vim?

Thanks for the article, might I also recommend the pdf from vimgeeks here: http://billodom.com/vim/vim-from-essentials-to-mastery-2011.pdf

Lots of really cool tips and tricks in there to really help with your VIM mastery.

Tin Dalos on 2011-07-27 17:09:34
Hi KvZ,
How the hell can you ( efficiently ) administer linux (UNIX) servers without knowing vi ( or vim if available ) ? No graphic sessions possible on secured machine and Nano is not always available.

Note: On Windows you can gVIM too, kill Notepad

Kevin on 2011-04-17 17:03:20
@ primeminister: MacVim. And gVim on Ubuntu.

primeminister on 2011-03-24 23:35:28
He Kevin,
What editor do you use on the mac? Just vin in your terminal of macvim or something else?

dilicious on 2011-03-24 17:07:06
has subscribed you blog ,like you php article

Comments