I Am vi, the Great and Powerful!You can almost hear a fearsome voice echoing eerily around the walls of your office or home. If there is one editor that strikes fear in the hearts of newbies everywhere, it is certainly vi (or vim), the visual editor. vim, by the way, stands for "vi improved" and is the version of vi that you will find in your Linux system. Anyhow, pay no attention to that fearsome voice behind the program. vi is not so frightening once you get to know it. To start vi, enter the command name on its own, or start it by specifying a filename: vi /tmp/test_file To start entering information, press i to go into insert mode. Type several lines; when you are done editing, press <Esc>. Moving around with vi is easy. Depending on the terminal emulator you are using, you can usually simply use your cursor keys. In the absence of cursor key control, the up, down, and sideways motions are all implemented with single keystrokes. To move left, press the letter h. To move right, press l. The letter k is up, and j is down. A little further on, I've included a quick cheat sheet. When you work with vi, the <Esc> key is your friend. If you don't know where you are or what mode you are in (insert, replace, or append), press the <Esc> key. You'll go back to normal vi command mode. Your second best friend is u or U, which stands for "undo." The uppercase undo will undo every change to the current line and the lowercase undo will undo the last change only. :q, :w, :wq, and ZZAll done editing? When it comes time to save your work, press <Esc> (to get out of whatever mode you are in) and type ZZ. Another way to exit is to type :wq (write and quit). At any time during an editing session you can type :w to save your current work. Finally, if you really don't want to save anything you have done, type :q!. The exclamation point essentially says that you won't take no for an answer. Had you modified the file and simply typed :q, vi would warn you that you were trying to exit from a modified file without having saved your changes.
I urge you not to let vi frighten you. Get to know it. The likelihood that you will ever log on to a modern Linux (or UNIX) system that doesn't have some form of vi installed is virtually nonexistent. That said, if you need more information than I've given you here, consider the vi tutor. This little tool is distributed as part of the vim documentation. It is essentially a text file that tells you what to do and how to do it as you read it. To start the tutor, type the following command: vimtutor When the tutorial starts, you should see a picture like the one in Figure D-3. The entire tutorial takes 25 30 minutes for most people to complete. Figure D-3. Learning vi or vim from the vim tutor.Recovering a vim SessionFrom time to time you may find yourself trying to edit a file while someone else, maybe you, is already editing it. That session may be open, or something may have happened to terminate it accidentally. As a result, you get a nice, long-winded message along the lines of "swap file found" and a whole lot of information on what you can do about it. Here's a shortened version of that message: E325: ATTENTION Found a swap file by the name ".linux_companies.swp" owned by: mgagne dated: Sun May 22 10:41:26 2005 file name: ~mgagne/linux_companies To locate these files, you can use the famous find command and look for anything with an .swp extension. Another and better way is to have vi report on these for you. You can check for the existence of swap files by using the -r option, which will provide you with a little more information than the simple find: Swap files found: In current directory: 1. .linux_companies.swp owned by: mgagne dated: Sun May 22 10:41:26 2005 file name: ~mgagne/linux_companies modified: YES user name: mgagne host name: francois.salmar.com process ID: 2191 In directory ~/tmp: -- none -- In directory /var/tmp: -- none -- In directory /tmp: -- none -- Power vi: Start-up OptionsNext time you need a reason to use vi instead of one of the other editors, consider some of these tricks for getting to what you want as quickly as possible. vi +100 ftl_program.c This will take you right to line 100 in the file called (in this case) ftl_program.c. This can be a great little timesaver when you are compiling programs and something goes wrong, as in the following example: gcc -O2 -Wall -c o ftl_program.o ftl_program.c ftl_program.c:100: parse error before `<' make: *** [ftl_program.o] Error 1 Another useful start flag is the same one you use to search for text inside a file: the forward slash (/). In the next example, I want to get back to the same place I was working on in my file. To mark my place, I had written my name on a blank line. This vi starter gets me right to where I was working: vi +/Marcel ftl_program.c Note the plus sign before the slash. |