Month of Vi(m): Starting Vi

For purposes of this series I am assuming you are starting vi from a terminal emulator for the purposes of this post. Terminal in OS X, Konsole, Xterm, anything like that will work.

Before we actually fire up vim there are two things to keep in mind. Vi and its descendants are modal editors. A modal interface means that commands that are entered mean different things, depending setting the program is in. The second is that vi does not edit files, strictly speaking. It will copy the file(s) you want to edit in an area of memory and that is what you are interacting with while editing. When you save the file to disk whatever is in the buffer overwrites the file. That is a very simple way of looking at it and I am probably making technical errors in my generalization but that is all one needs to know for now.

The simpliest way of starting vi is typing "vi" at the command prompt:

stinkpad:~$ vi</pre><br />This will start vi with a blank buffer and vi will be in command mode. Command mode is almost always the first mode you start in. This is where you can move around the file, delete, copy, paste, and a bunch of other moves. This is also where you enter other modes, such as insert, append, replace, etc.</p><br /><p>You can also start vi already editing a file that you want to work on:<br /><pre>stinkpad:~$ vi filename</pre><br />This will read the file on the disk into vi's buffer and lets you edit the buffer. Again you will be in command mode when you do this.</p><br /><p>What if you have multiple files you want to edit?<br /><pre>stinkpad~$ vi file1 file2 file3</pre><br />file1 file2 and file 3 will all have a copy in a different buffer that you than can edit. To switch between the files press the collon key while in command mode and types "enter" or "previous", depending on what you want to do.</p><br /><p>You can also start at a particular line. This comes in handy for me when testing bash scripts and I get an error on about a command at a certain line.<br /><pre>stinkpad~$ vi +4 file</pre><br />This will open file at take the cursor to line #4 of the file. You can also open the file at a particular pattern that you are searching for.<br /><pre>stinkpad~$ vi +/phone file

This will open file at take the cursor to the first occurance of the word phone that vi finds.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.