Linux DevCenter    
 Published on Linux DevCenter (http://www.linuxdevcenter.com/)
 See this if you're having trouble printing code examples


Problem Checklists: Appendix B - Learning the vi and Vim Editors, Seventh Edition

by Linda Lamb, Arnold Robbins, Elbert Hannah
Learning the vi and Vim Editors, Seventh Edition book cover

This excerpt is from Learning the vi and Vim Editors, Seventh Edition.

The standard guide for vi since 1986, this book has been expanded to include detailed information on vim, the leading vi clone that includes extra features for both beginners and power users. You learn text editing basics and advanced tools for both editors, such as writing macros and scripts to extend the editor, power tools for programmers, multi-window editing -- all in the easy-to-follow style that has made this book a classic.

buy button

 

This appendix consolidates the problem checklists that are provided throughout Part I, “Basic and Advanced vi”. Here they are presented in one place for ease of reference.

Problems Opening Files

  • When you invoke vi , the message [open mode] appears.

    Your terminal type is probably incorrectly identified. Quit the editing session immediately by typing :q. Check the environment variable $TERM. It should be set to the name of your terminal. Alternatively, ask your system administrator to provide an adequate terminal type setting.

  • You see one of the following messages:

      Visual needs addressable cursor or upline capability
      Bad termcap entry
      Termcap entry too longterminal:  Unknown terminal type
      Block device required
      Not a typewriter

    Either your terminal type is undefined, or there’s probably something wrong with your terminfo or termcap entry. Enter :q to quit. Check your $TERM environment variable, or ask your system administrator to select a terminal type for your environment.

  • A [new file] message appears when you think a file already exists.

    Check that you have used the correct case in the filename (filenames are often case-sensitive). If you have, you are probably in the wrong directory. Enter :q to quit. Then check to see that you are in the correct directory for that file (enter pwd at the Unix prompt). If you are in the right directory, check the list of files in the directory (with ls) to see whether the file exists under a slightly different name.

  • You invoke vi , but you get a colon prompt (indicating that you’re in ex line-editing mode).

    You probably typed an interrupt before vi could draw the screen. Enter vi by typing vi at the ex prompt (:).

  • One of the following messages appears:

      [Read only]
      File is read only
      Permission denied

    “Read only” means that you can only look at the file; you cannot save any changes you make. You may have invoked vi in view mode (with view or vi -R), or you do not have write permission for the file. See the next section, the section called “Problems Saving Files”.

  • One of the following messages appears:

      Bad file number
      Block special file
      Character special file
      Directory
      Executable
      Non-ascii filefile non-ASCII

    The file you’ve called up to edit is not a regular text file. Type :q! to quit, then check the file you wish to edit, perhaps with the file command.

  • When you type :q because of one of the previously mentioned difficulties, this message appears:

      No write since last change (:quit! overrides).

    You have modified the file without realizing it. Type :q! to leave vi. Your changes from this session will not be saved in the file.

Problems Saving Files

  • You try to write your file, but you get one of the following messages:

      File exists
      Filefile exists - use w!
      [Existing file]
      File is read only

    Type :w! file to overwrite the existing file, or type :w newfile to save the edited version in a new file.

  • You want to write a file, but you don’t have write permission for it. You get the message “Permission denied.”

    Use :w newfile to write out the buffer into a new file. If you have write permission for the directory, you can use mv to replace the original version with your copy of it. If you don’t have write permission for the directory, type :w pathname/file to write out the buffer to a directory in which you do have write permission (such as your home directory, or /tmp).

  • You try to write your file, but you get a message telling you that the file system is full.

    Type :!rm junkfile to delete a (large) unneeded file and free some space. (Starting an ex command line with an exclamation point gives you access to Unix.)

    Or type :!df to see whether there’s any space on another file system. If there is, choose a directory on that file system and write your file to it with :w pathname. (df is the Unix command to check a disk’s free space.)

  • The system puts you into open mode and tells you that the file system is full.

    The disk with vi’s temporary files is filled up. Type :!ls /tmp to see whether there are any files you can remove to gain some disk space.[76] If there are, create a temporary Unix shell from which you can remove files or issue other Unix commands. You can create a shell by typing :sh; type CTRL-D or exit to terminate the shell and return to vi. (On most Unix systems, when using a job-control shell, you can simply type CTRL-Z to suspend vi and return to the Unix prompt; type fg to return to vi.) Once you’ve freed up some space, write your file with :w!.

  • You try to write your file, but you get a message telling you that your disk quota has been reached.

    Try to force the system to save your buffer with the ex command :pre (short for :preserve). If that doesn’t work, look for some files to remove. Use :sh (or CTRL-Z if you are using a job-control system) to move out of vi and remove files. Use CTRL-D (or fg) to return to vi when you’re done. Then write your file with :w!.

Problems Getting to Visual Mode

  • While editing in vi , you accidentally end up in the ex editor.

    A Q in the command mode of vi invokes ex. Any time you are in ex, the command vi returns you to the vi editor.

Problems with vi Commands

  • When you type commands, text jumps around on the screen and nothing works the way it’s supposed to.

    Make sure you’re not typing the J command when you mean j.

    You may have hit the CAPS LOCK key without noticing it. vi is case-sensitive; that is, uppercase commands (I, A, J, etc.) are different from lowercase commands (i, a, j), so all your commands are being interpreted not as lowercase but as uppercase commands. Press the CAPS LOCK key again to return to lowercase, press ESC to ensure that you are in command mode, then type either U to restore the last line changed or u to undo the last command. You’ll probably also have to do some additional editing to fully restore the garbled part of your file.

Problems with Deletions

  • You’ve deleted the wrong text and you want to get it back.

    There are several ways to recover deleted text. If you’ve just deleted something and you realize you want it back, simply type u to undo the last command (for example, a dd). This works only if you haven’t given any further commands, since u undoes only the most recent command. On the other hand, a U will restore the line to its pristine state, the way it was before any changes were applied to it.

    You can still recover a recent deletion, however, by using the p command, since vi saves the last nine deletions in nine numbered deletion buffers. If you know, for example, that the third deletion back is the one you want to restore, type:

    "3p

    to “put” the contents of buffer number 3 on the line below the cursor. This works only for a deleted line. Words, or a portion of a line, are not saved in a buffer. If you want to restore a deleted word or line fragment, and u won’t work, use the p command by itself. This restores whatever you’ve last deleted.



[76] Your vi may keep its temporary files in /usr/tmp, /var/tmp, or your current directory; you may need to poke around a bit to figure out where exactly you’ve run out of room.

If you enjoyed this excerpt, buy a copy of Learning the vi and Vim Editors, Seventh Edition.

Copyright © 2009 O'Reilly Media, Inc.