More on Vim
So you thought you could get by with gedit? Not when you must configure an application on your server in a data center two states away! When it comes to remote system administration, all roads lead back to Vim. Not a Vim expert? No problem, you just need a handful of commands to do everything you need to do.
When it comes to remote system administration, all roads lead to Vim. While Vim’s capabilities are vast — entire books have been written about it, and they still fail to capture all of its functionality — knowing a handful of commands will enable you to do much of what you need to do.
Entire books have been written about Vim, and they still fail to capture all of its functionality. But the core command set is all you really need if you’re just making a few changes to a few files over SSH. You might not do it as efficiently as a Vim expert, but it’s good enough to get the job done. In fact, if you’re working with Vim (the pre-eminent vi-clone) on a server, there’s a good chance it’s “vim-tiny,” a stripped-down Vim that offers the traditional vi functionality but not the full set of Vim features.
The first thing you must grok is that Vim has several modes — command mode, insert mode, and last-line mode (also known as ex mode). When you start Vim, you’ll be in command mode. Here, all of the keys are used to perform commands, not input text. To switch to input mode, hit
i, and you’re able start editing the file, adding text, using Backspace, etc.
To return to command mode, hit
Esc. To enter last-line or Ex mode, use
:, and then input the command you wish to enter.
Vim Command Mode
I could go on for days about the commands needed in command mode, but we’re just here for the basics, so let’s look at movement. Movement is based on the standard alphabetic keys:
hMove the cursor to the left one character.
lMove the cursor to the right one character.
jMove the cursor down one line.
kMove the cursor up one line.
You can move faster by using
w to move backward and forward by one “word” at a time, respectively. Vim looks at “words” as a string of alphanumeric characters. So “word” is a word, but “one-year” is multi-word because it’s broken up by a non-alphanumeric character.
Deleting is done with
x. To delete a single character, move the cursor over that character and use
dw will delete the word the cursor is over, and
db will delete the previous word.
To delete an entire line, use
dd. To delete from the cursor to the end of the line, use
d$. To delete from the cursor to the beginning of the line, use
$ is shorthand for “end of the line,” and
0 is shorthand for beginning of the line. You can also use
Copying and Pasting in Vim
Let’s look at copying and pasting real quick. To highlight text to copy, use the
Ctrl-V commands. You might have guessed by now that Vim commands are case-sensitive, So
V are different things.
v command simply allows you to highlight changes character by character using the movement (
hljk and others) or arrow keys. The
v command highlights entire lines. And the
Ctrl-v command highlights blocks of text — very useful for highlighting and copying columns of text.
Once you’ve highlighted the text you want to copy, hit
y to “yank” the text into the buffer.
To paste the text, use
P to paste. The
p command will paste after the cursor, and
P pastes before the cursor.