Sunday 13 January 2013

vi useful commands

To Get Into and Out Of vi

To Start vi

    To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.

* vi filename edit filename starting at line 1
  vi -r filename recover filename that was being edited when system crashed

To Exit vi

    Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file.

    Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is completed by hitting the <Return> (or <Enter>) key.

* :x<Return> quit vi, writing out modified file to file named in original invocation
  :wq<Return> quit vi, writing out modified file to file named in original invocation
  :q<Return> quit (or exit) vi
* :q!<Return> quit vi even though latest changes have not been saved for this vi call

Moving the Cursor

    Unlike many of the PC and MacIntosh editors, the mouse does not move the cursor within the vi editor screen (or window). You must use the the key commands listed below. On some UNIX platforms, the arrow keys may be used as well; however, since vi was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi and should be avoided.

    If you go back and forth between a PC environment and a UNIX environment, you may find that this dissimilarity in methods for cursor movement is the most frustrating difference between the two.

    In the table below, the symbol ^ before a letter means that the <Ctrl> key should be held down while the letter key is pressed.

* j or <Return>
  [or down-arrow] move cursor down one line
* k [or up-arrow] move cursor up one line
* h or <Backspace>
  [or left-arrow] move cursor left one character
* l or <Space>
  [or right-arrow] move cursor right one character
* 0 (zero) move cursor to start of current line (the one with the cursor)
* $ move cursor to end of current line
  w move cursor to beginning of next word
  b move cursor back to beginning of preceding word
  :0<Return> or 1G move cursor to first line in file
  :n<Return> or nG move cursor to line n
  :$<Return> or G move cursor to last line in file

No comments:

Post a Comment