Note: This is not mine! I don’t want to be harrassed, sued, or have kittens thrown at me. This great piece I luckily stumbled upon when I was first trying to learn vim. The
original piece was part of the original Gentoo Wiki. Because the archive does not have information on the author, I am writing this without his permission. If you are the author please email and tell me what you think. This piece has been slightly editing for clarification.
Introduction
Vim is an advanced text editor that seeks to provide the power of the de-facto Unix editor ‘Vi’, with a more complete feature set. Vim is not a simple text editor like nano or pico. It does require some time to learn, and a great amount of time to master.
About Vim
Vim is probably the most popular incarnation of its predecessor vi, but all vi packages are similar.
Vim is designed to make your fingers work as little as possible, and you should never have to use the mouse. This may seem odd, but once you master Vim, you’ll wonder why other apps don’t behave like it.
Features
- Vim has syntax highlighting.
- No-nonsense editor
- Command mode allows for simple, robust keybindings
- Vim is very powerful for advanced editing tasks
- vimtutor is a vim-based tutorial to learn… indeed… vim
- Uh…vim is good?
Starting Vim
If you start vim with vim somefile.txt you’ll see a blank document (providing that somefile.txt does not exist. If it does, you’ll see what’s in there). You will not be able to edit right away – you are in Command Mode. In this mode you are able to issue commands to vim with the keyboard.
Note: Vim is an example of classic UNIX-style ware. This means that its not flashy, and it won’t hold your hand. It doesn’t come with built-in paperclips and games. It will allow you to get the job done however, and quickly too. Also, all commands are case sensitive. Sometimes the uppercase versions are “blunter” versions (s will replace a character, S will replace a line), other times they are completely different commands (j will move down, J will join two lines).
Let’s work on something. It can be any text file, really. Open that file with vim:
vim foo.txt
Basic Editing
You begin in command mode. If you’re not sure what mode you’re in, press ESC to get to command mode.
You insert text (stick it before the cursor) with the i command. I inserts text at the end of the line. You append text (place text after the cursor, what most people expect) with a. Typing A will place the cursor at the end of the line.
Return to command mode at any time by pressing ESC.
Moving Around
Single Characters
In Vim, you can move the cursor with the arrow keys, but that’s no very efficient is it? You’d have to move your right hand all the way from the standard typing position all the way to the arrow keys, and then back. Not fun.
In vi you can move down by pressing j. You can remember this because the “j” hangs down. You move the cursor back up by pressing k. Left is h (its left of the “j”), and right is l (its right of the “k”).
^ will put the cursor at the beginning of the line, and $ will place it at the end.
^ and $ are commmonly used in regular expressions to match begin and end of the line. Regular expressions are very powerfull and are commonly used in *nix environment, so maybe it is a little bit tricky now, but later you will notice “the idea” behind most of the key mappings. Other commands also use ^ and $ to move/do something from cursor to begin or end of the line.
Multiple Characters
To advance a word, press the w key. W will include more characters in what it thinks is a word. To go back a word, b is used. Once again, B will include more characters in what vim considers a word. To advance to the end of a word, use e. If you haven’t guessed it, E includes more characters to be a word.
To advance to the beginning of a sentence, ( will get the job done. ) will do the opposite, moving to the end of a sentence. For an even bigger jump, { will move the the begining a whole paragraph. } will advance to the end of a whole paragraph.
To advance to the header (top) of the screen, H will get the job done. M will advance to the middle of the screen, and L will advance to the last (bottom).
The repetition department of the repetition department of the…
Here’s an awesome thing: if you press a number before a command, then that command will be executed that number of times over (there are exceptions, but they still make sense, like the s command). For example, pressing 3i then “Help! ” will print “Help! Help! Help!“. Pressing 2} will advance you two paragraphs. This comes in handy with the next few commands…
Deleting
The x command will delete the character under the cursor. X will delete the character before the cursor. This is where those number functions get fun. 6x will delete 6 characters. Pressing . (dot) will repeat the previous command. So, lets say you have the word foobar in a few places, but after thinking about it, you’d like there just to be “foo”. Move the cursor under the b, hit 3x, move to to the next foo bar and hit . (dot). BAM!
The d will tell Vim that you want to delete something. After pressing d, you need to tell Vim what to delete. Here you can use the movement commands. dW will delete up to the next word. d^ will delete up unto the beginning of the line. Prefacing the delete command with a number works well too: 3dW will delete the next three words. D (uppercase) is a shortcut to delete until the end of the line (basically d$). Pressing dd will delete the whole line.
Undo and Redo
vim has a built-in cutboard. Actions and be undone and again redone. Use u to undo and ctrl+r to redo.
Advanced Editing
Pressing s will erase the current letter under the cursor, and place you in edit mode. S will erase the whole line, and place you in edit mode. Pressing 5s will erase 5 letters and place you in edit mode.
Pressing v will put you in visual mode . Here you can move around to select text, when you’re done, you press y to yank the text into the buffer (copy), or you may use c to cut. p pastes after the cursor, P pastes before. V, Visual Line mode, is the same for entire lines. c^v is for blocks of text.
Note: Whenever you delete something, that something is placed inside your buffer and is available for pasting.
Search and Replace
To search for a word or character in the file, simply use / and then the characters your are searching for and press enter (e.g. /myword). To view the next match in the search press n.
To search and replace use the substitute :s/ command. The syntax is: [range]s//]/[arguments]. Some examples:
Command Outcome
:s/xxx/yyy/ Replace xxx with yyy at the first occurence
:s/xxx/yyy/g Replace xxx with yyy global (whole sentence)
:s/xxx/yyy/gc Replace xxx with yyy global with confirm
:%s/xxx/yyy/g Replace xxx with yyy global in the whole file
You can use the global :g/ command to search for patterns and execute a command for each hit. The syntax is: :[range]:g//[cmd]. Some examples:
Command Outcome
:g/^#/d Delete all lines that begins with #
:g/^$/d Delete all lines that are empty
To replace the current word. Place the cursor on the word and execute the command cw. This will delete the word and change the mode to “input”. To replace a letter use r.
Other things
Vim will auto indent. This can be annoying when you have to paste something that contains a space or tab at the beginning of the line. In command mode typing :set paste will disable this. Typing :set nopaste will reenable it.
Saving and Quitting
Write a file with :w or if the file doesn’t have a name :w <filename.txt>. Quitting done with :q. If you choose not to save your changes, use :q!. To save and quit :x.
Using Tabs
If you want to edit multiple documents at once you can use tabs to make it easier:
vim -p <document 1> <document 2>…
Configuration File
vim is ultimably customizable and can be used for many different program languages, personal perferences… and is done so in the configuration file. There’s alot you can do in a .vimrc file, for new users here’s a basic one:
.vimrc
Place in your home directory.
Conclusion
You now know how to use Vim to do slightly more than you could do in a simple editor. As your knowledge of Vim grows, you will be able to use it highly efficiently and do amazing things with text files. And most importantly, you’ll feel right at home playing nethack.