Just enough vim to survive.
How to make sense of editing files on the command line.
Opening a file.
vim filename
This will open the file in command mode.
Hang on a minute… whats a mode???
Ok , vim has these things called modes, at least 6 of them. Command mode is the default. And you’re in it. Its from here you issue commands. The other ones we’ll worry about is insert mode (more of which in a second), and visual mode.
Editing a file.
esc esc i
Whilst not always necessary, I would suggest the habit of double escape then i. Double escape guarantees you are in command mode. Then i takes you into insert mode. Where you can start inserting / replacing / creating as much as you’d like.
Saving a file.
From command mode
:w
And you write the file. Dont forget the double escape trick to guarantee your in command mode.
The all important — get me out of here and don’t change anything command.
Sometimes, you’ll get to a place where you don’t know what you’ve done, or what to do next. (in life, as well as in vim).
In vim, the magic life saving set of key strokes is
esc esc :q!
Chaining things together — save and exit.
So now we’ve seen some basic commands. Lets look at how you can chain things together in vim. Taking the write a file, and the quit operation you get
esc esc :wq
easy as that.
Speed up tips
Moving around in a file
Theres a slow way, and a quick way to move around a file in vim.
First the slow way — the arrow keys, or hjkl =(h left, l right, j down, and k up).
The quicker way is to learn some more of the motion keys.
w — navigate by word
$ — to the end of the line
A — to the end of the line, and go into insert mode
:<num> — jump to that line
Deleting text.
Deleting text is done with the d command. The simplest, easiest to remember version of this is
dd
That deletes the line of text you are on. you can combine d with motion operators. — so dw , delete a word, for example
dG
Deletes to the end of the file.
one of my favourite tricks is
di”
read that as ‘delete inside “’ so if your in a quoted string, this deletes its contents, leaving the quotes. Similarly:
dib
deletes in a block surrounded by brackets (this kind)
diB
deletes in a block surrounded by curly brackets {this kind}
Copying and pasting , undo redo and search.
I tend to use visual mode for this.
esc v
and then use the motion keys to select what you want.
once selected
y
‘yanks’ that into the buffer , and
p
pastes it.
Undo is simple, as is redo.
u and Ctrl-r
Searching is done with a regular expression.
/this
searches for ‘this’
/this/i
searches for ‘this’ , and ‘This’, and ‘tHis’
the n key goes through the matches.
Search and replace is very powerful.
:%s/this/That/g
replace all instances of this, with That
Syntax highlighting, line numbering
:syntax on
sets highlighting in the editor
:set number
sets the line numbering on (set number! to turn it off)
As with most linux commands, you can set defaults in a vimrc file.
heres a good start for a Drupal developer — put it in .vimrc
that enables the right tab stops, syntax highlighting, and enables the mouse.