When I first encountered Unix in the early 1990s, I needed a text editor. I tried Emacs but the meta key and double control keys struck me as wrong. I tried Vi but couldn’t figure out how to type anything. I came across Pico and for years used that as my editor. Why? I wasn’t doing much other than modifying some shell scripts or dot files and it was easy. All the commands I needed were listed at the bottom of the screen. Pico is the Notepad.exe of the Unix world. Years later I was forced to do some programming on a Linux system via an ssh connection. Needless to say, Pico isn’t really a great programming editor. I had a friend who was really excited about Vim (Vi Improved) at the time so I gave that a try. Emacs still didn’t sound inviting.
I began by reading tutorials on the web. They all intended to get me up to speed on all the nifty functions in Vim very quickly. For instance, most tutorials start by teaching that hjkl are the cursor keys. They are, but you don’t really need to know that for basic functionality. The ones with arrows on them will work just fine for most purposes. Options like this serve just to confuse at the beginning. The trouble is, I can only learn so much at once. Before I could learn how to run, I had to learn how to walk. I needed to be comfortable doing the basics before I could grok the extra stuff. I needed to be able to use Vim like notepad functionality before I could move on to various visual modes, regular expressions, and about 80 billions ways to get around the screen.
This then is the tutorial I wish I had found. It is how to use GVim as Notepad. GVim is the graphical version of Vim. It’s the same as the shell-based version, but you get to use your mouse and there are menus. What I am about to present doesn’t show any of the power of Vim, but it will help you become comfortable enough that the myriad other tutorials on the web can be of assistance.
The first thing to understand about Vim is that it is modal. Most editors are modeless. Letters are entered by typing them. Control keys do special things. This is true all the time. Control-S will invoke Save in notepad no matter what you are doing. Not so in Vim. Vim has different modes and different commands will have a different effect depending on which mode the editor is in. It is important to keep in mind which mode Vim is in. For now there are 2 modes you need to know about.
First there is “normal” mode. Vim always opens in normal mode. This is the mode where you enter commands. Whether you want to save a document or change the case of a letter, normal mode is where you want to be. The confusing part is that in normal mode, you can’t actually add text to your document. If you start typing in a blank document, not much will happen. If you start typing in a full document, all sorts of strangeness will. In Vim’s normal mode, nearly every key on the keyboard is a command. See this graphical cheat sheet if you want to see them all.
Insert mode is probably what you are familiar with. It’s just like being in Notepad or Pico or even Word. Insert mode works just like you expect an editor to. The letters you type show up in the document as text. Cursor keys or the mouse can be used to move the cursor around. To enter insert mode, just type i. The cursor will change from an XOR cursor to a bar cursor. The word — INSERT — appears at the bottom of the screen when in insert mode. If you are new to Vim, this is the mode you’ll spend the most time in.
Esc will exit insert mode and return to normal mode.
If you are using GVim, editing is simple. Just enter insert mode and it works the ways Notepad works. You can select text with the mouse, cut with <ctrl-c>, paste with <ctrl-v>, cut with <ctrl-x>, and delete with the del key.
To save your work, hit esc to go to normal mode. The combination :w (write) will save the document. Many commands in Vim are prepended with a colon ‘:’. There are good reasons for this, but we won’t go into them now. Just accept that they are needed to increase the number of available commands.
Exiting Vim can be done via the menus or by issuing the :q command from normal mode. If you want to quit without saving, :q! will accomplish that.
To load a new file, I recommend using the menus at this point in your Vim career. If you must, :e filename will load filename.
Search & Replace
Often times you’ll need to find something in your document. To do so, enter normal mode (hit esc). Type / following by the word you are looking for. For example, /Steve will find the next time Steve appears in the document. As you type, you’ll notice two things. First, the cursor immediately jumps to the first S following the cursor, then to the first St, etc. It’s doing an incremental search as you type. In this way, you can stop when you have reached the desired location. No need to type the whole word. To stop, just press <enter>. The other thing you’ll notice is that all instances of the word Steve have been highlighted. This makes it easy to see all the places in your document where the word appears. To go to the next instance, press n while still in normal mode.
To replace some text, say the word “from” with other text, the word “to”, do this in normal mode: :%s/from/to/g. :%s means substitute on every line of the document. The /’s separate the parts of the command ala regular expressions. The trailing g says to replace all instances on each line.
If you already search for the word you want to replace, the syntax can be simplified by leaving out from. Thus :%s//to/g will replace whatever was last searched for with “to”.
This should be enough to get you up and running in Vim. You won’t notice any advantage from Vim yet, but you will be able to edit. Once you are comfortable with GVim as a Notepad alternative, you’ll be ready to learn the parts that make Vim such a great editor. I have a post to help with that here.
For the Non-Visual User
If you happen to be using Vim from a command-line instead of GVim, a few more commands will be necessary for basic operation.
To select text, hit <ctrl-v> to enter visual mode and use the cursor keys* to select the text you want to operate on. If <ctrl-v> does a paste, you can use <ctrl-q> instead.
To cut or delete the selection, press x.
To copy the selection, press y (yank).
To paste a previously cut or copied selection, go to where you want to insert it and then (from normal mode) press p.
*This won’t work in GVim. If you know how to make it work, let me know.