If you’re interested in learning Vim you’re in luck – because there are some great resources out there. In this post I’m going to document the ones I’ve been using, in a step-by-step kinda way – hopefully making it easy to follow for anyone else keen on learning. By the end of it, you should (hopefully) no longer be a novice
Step one – learn the basics
First I suggest you have a quick read of my last Vim post which acts as a bit of an overview, and why Vim is so good – especially for Rails development.
Then, type vimtutor into your terminal and follow the tutorial – which is fairly short, but easy and fun!
Step two – see it in action
And pick up a thing or two along the way! Derek Wyatt‘s Vim tutorials are pretty awesome. He’s a funny guy who keeps the videos informative and interesting… you never know what he might say or do next – in one for example, he shares that he has no pants on! Crazy nutta! lol.
There are lots more on his site – but this was enough for me for now. Step 3 is where we start reinforcing some of this stuff.
Step three – do, repeat, reinforce
Firstly, start using Vim as your main editor – that’s when things will start sticking. But also, for the next week or so, read the following once every night (it’s basically some key points from Derek’s screencasts). Repeating will help reinforce and cement the commands into your brain. Mwwhahahaaaaa.
f/ goes to first character after the f on that line – in this and most of the following cases that’s a forward slash (/)
; repeats the above (to the next forward slash)
t/ goes to just before (till) the character
; repeats the above
F/ goes backwards
T/ goes until backwards
4f/ go to fourth specified character
ct/ change till character
c2tc change until second c (replace 2 with any number)
4f/ go to fourth character (/)
CTRL U move up half a page
CTRL D move down half a page
H go to head (top) of screen (does not change text position on screen)
M go to middle of screen
L go to last (bottom) of screen
48G got to line 48
* changes current word into a search term, hit n for next word.
# same as above, but goes backwards
N in either of the above, changes direction
g* case insensite of *
g# matches part words
/WORD manual search (replace word with what you want to find)
?WORD backwards search
% go to matching bracket
v visual mode
V visual mode – by line
ma mark line
You can then go to wherever you want, use Y to copy the line, and then use ‘a to go back, and then use p to paste.
‘‘ goes back to the line you were on, eg, after a M or 20G.
`` goes back to the exact character you were on.
I insert at start of line
A append at the end of the line
i insert at position
a append (just after position)
o insert in line below
O insert in line above
x delete at cursor
X delete before cursor (like a backspace)
dd delete line
. repeat (eg dd…..)
dw delete to next word from cursor (includes whitespace up to next word)
diw delete inner word – deletes from start of word to end of word (leaves whitespace)
daw delete a word – deletes from start of word to start of next word (inlcudes whitespace)
de delete to end of word only
cw change word (or to end of word)
c$ change to end of line
cc change entire line
r replace single character
R replace/overwrite from
c2w change two words
3r change 3 chracters
s substitute (delete) character at cursor and go into insert mode
s substitute (delete) entire line and go into insert mode
yy yank (copy) line
Y yank (copy) line
P paste to line below
yw yank word
J join to line below seperated by a space (can be repeated – you can also highlight a number of lines and join them)
6Y yank 6 lines
gUiw make current word uppercase
guiw make current word lowercase
CTRL V block highlight
CTRL Ve block highlight to end of word
From here you can move around(/highligh) as usual, then do something like i to insert some text and then when you click escape it will insert the text to the block you highlighted. You can do any editing, such as cw, x, r etc
gv repeats previous block (handy if you need to re-edit the same block)
CTRL n auto-completion (start typing a word) next completion
CTRL p auto-completion (start typing a word) previous completion
Buffers are really cool. Vim keeps track of all the files you’ve opened in a ‘buffer’. So you can easily go back to files as and when you need to – without having to have squillions of tabs or windows open. Being a TextMate tab-a-holic, I love them – as I can see they are going to be much more useful.
:ls lists buffers
:b followed by number or name of file (can use TAB for autocompletion) – to go to that buffer.
:b# flip to last buffer (and again to flip back)
:bd deletes current buffer
:bd 23 delete buffer 23
:bd file-name delete buffer file-name
:bd 23 44 deletes buffers 23 and 44
:bd file-1 file-2 deletes buffers named file-1 and file-2
:22,26 bd deletes buffers in range from 22 to 26
:wall write all
:bn next buffer
:be previous buffer
:bfirst first buffer
:blast last buffer
Windows are not buffers. So two windows with the same file equals the same buffer. Closing the window does not close the buffer (but closing a buffer can close the window that the buffer is shown in).
:sp split window
:vsp split window vertically
:sp file-name split window with file name.
CTRL W s split window
CTRL W v split window vertically
CTRL W j move to lower window
CTRL W h move to left window
CTRL W c close window
CTRL W K move window above/span/span
Step 4 – you decide
There’s so much more to learn, but it really depends on which aspect of Vim you want to go deeper with. I’ll probably watch the rest of Derek’s videos, because they keep my interest – and he’s kinda mad which makes it all the more compelling.
Well, good luck! I hope you’ll enjoy Vim as much as I think I will, and if I come across more resources that I think are worth sharing, I’ll be sure to add them here./span etc