Well, after the massive spike in traffic to my site after writing the blog post on GNU Screen and Byobu Made Easy, I decided to do another quick tutorial on another Linux command-line tool, ViM. ViM, or “Vi Improved”, is a command-line editor that has been around since the dawn of Linux command lines, and is deceivingly powerful. Although we won’t get into the more powerful parts of the program today, stay tuned for some power tips later.
Now, anyone that has been around two or more Linux command-line junkies I’m sure has heard the Emacs vs. ViM argument at some point. Regardless of which one you like, they’re both great editors. Give them both a shot and choose your favourite.
Vim can be very overwhelming to start off on, but is great once you get used to it. When you first open ViM, you will be presented with a blank document and you will be in Normal mode (see below). Soon, we will be able to start writing text, undoing a mistake, perform cuts, copies, and pastes, as well as some search-and-replace.
A quick note that almost everything is case-sensitive!
There are six main program modes in ViM, listed below. We will only cover three of them in this tutorial for the time being.
- Normal Mode. This is where you type all of your commands, typically to move into one of the other modes.
- Insert Mode. Here is where you’ll actually type text into your document.
- Visual Mode. Visual mode is mainly used for yanking (copying) and deleting (cutting). Although it can do more than this, we’ll focus on these for now.
- Select Mode. Similar to Visual mode, Select mode is typically used for deleting a selection of text and immediately typing over top of it.
- Command-Line Mode. This is where you type your commands, such as saving, searching (and replacing), and the ability to edit ViM’s options.
- Ex Mode. All-in-all, this is pretty much command-line mode, except after typing a command you end up staying in command-line mode instead of reverting back to Normal mode.
Let’s Write Something!
You’ve just opened up ViM, but every time you try to type text, nothing seems to happen? What gives?! Well, right now you’re in Normal mode, and ViM is waiting for an instruction. In order to start typing text, just type “i” (for “Insert”) or “a” (for “Append”). The Append mode will move your cursor one character forward before you can type, so keep note of that. After you’re finished typing what you want, just hit Escape to get back into Normal mode.
Let’s say, that you just wrote “ViM is awesome!” in your spiffy new document, but saying it once just isn’t enough! You want to say it over and over again, but typing it out so many times just seems like a waste, doesn’t it? Time to go into Visual mode! Move your cursor to the beginning of your text using the cursors, then type “v” (lower-case). This puts you into Character-Select Visual mode. Move your cursor to the end of the text, and press “y” (for “Yank”), which copies the text into it’s built-in clipboard. Move your cursor to where you want to paste, and type “p” (for “Paste”). Note that “P” will paste BEFORE your cursor, so keep that in mind.
Whoops! You pasted it one-too-many times, or you pasted it in the wrong spot! Never fear, the Undo tool is here! Make sure you’re in Normal mode (just hit Escape if you’re not sure), and press “u” (for “Undo”).
What if, for example, you ended up typing “ViM is awsemoe!” (hey, your fingers got tied up; it happens). It doesn’t make sense to undo all of that, so let’s just do a search-and-replace. Go into normal mode and type “:%s/awsemoe!/awesome!”, then hit enter. Poof! Problem solved! I’ll discuss the search-replace a bit more in the cheat sheet. If you just want to search for text, type “/your-text-here” in Normal mode, then hit enter. “n” will move you forward through all the findings, and “N” will move you backwards.
Saving and Exiting
Saving and exiting is really easy. “:w myfile.txt” will write the file to myfile.txt. If you opened an existing file, you don’t need the file name, so “:w” is all you need. To do a save and quit at the same time, type “:wq”.
What if you want to quit but don’t save your changes? The best way to do this is “:q!”, which will quit without heeding any warnings about the file not being saved.
- a - Append
- i - Insert
- /<your-text-here> - Search for <your-text-here>. Does NOT use regular
- dd - Delete the entire line that your cursor is on
- x - Delete the character your cursor is hovering on.
- :42 - Move to line 42
- G - Go to the last line in the document
- :s%/<search>/<replace> - Regular expression-compatible search-replace.
- :s/<search>/<replace>/g - Same as above, except replaces everything on a
single line. Remove the "g" to replace only the first occurrance.
- :s42/<search>/<replace>/g - Same as above, except replace on line 42.
- V - Line-select Visual mode
- v - Character-select Visual mode
- y - Copy (yank) the selected text
- d - Delete the selected text
- :w - Write the file to disk
- :wq - Write and quit
- :q - Quit
- :q! - Quit without saving
Hopefully those that are starting out on Linux will find this useful. I plan on going in to greater depth in the near future, so stay tuned for that. If you like this article, I’d love for you to Digg or Reddit
this page below. It’s such a great feeling when your traffic spikes to 1000 hits in a day. And, for those Emacs lovers, I’ll be doing an Emacs writeup as well.