Sunday, September 03, 2006

Vi Editor Quick Reference

Read this brief introduction of Vi editor cheat sheet for better understanding on the guides follow:

  1. Always use vim (VI Improved editor) instead of vi, as some of the features outlined below are found only with vim.

    Another great feature of vim is that, it will always open the file at the line where the cursor stop right before existing from the last file access!

    If it has been a habit with vi command name, then make an alias and put it into the login scripts

    alias vi='vim'

    to retain the simplicity of the command name. For the same reason (of simplicity), the vi command name is used to refer vim, throughout this Vi command cheat sheet!

  2. Bold font used to denote Vi editor's command code.

  3. To execute Vi command code, press ESC to enter command mode.

  4. On start up, Vi editor is in neutral mode. Press ESC follow by i to start editing (insert) mode.

  5. Once complete typing the command code, Vi editor automatically shift to editing mode. Some command codes which initial with ":" (colon), there is a need to press ENTER key to end the command code.

  6. If it is not comfortable with the Vi editor color scheme, turn them off by typing:

    • :syntax off - turn off color scheme
    • :set nohlsearch - turn off search pattern highlighting

  7. If Vi editor color scheme is a must, but found that some of the color (especially the color of comment lines) is dim and not bright enough for easy reading, tweak the Vi editor by

    • :set bg=dark
               or
    • :set bg=light

    to find the best Vi editor background color for a better colored visual aid and comfortable reading.

  8. Press V to mark the whole line under the cursor, continuous as cursor move. Press v to start highlight as cursor move.

  9. To get "online" help on any of the Vi editor command codes, press :help command code in the editor.
Bulk delete or copy then paste a block of sentences within a file
  1. Move the cursor to the first line of the block of sentences and press mm
  2. Move the cursor to the last line of the block of sentences
  3. Press d'm to delete the block of sentences from current line where the cursor stop to the line where "m" marker initiated in step 1, OR
  4. Press y'm to copy the block of sentences from current line where the cursor stop to the line where "m" marker initiated in step 1, THEN
  5. Move the cursor to a position of file where it is intended to paste the copied block of sentences
  6. Press p to paste the copied block of sentences.
Bulk copy a block of sentences in file A and paste the copied sentences to file B
  1. Move the cursor to the first line of the block of sentences and press mm
  2. Move the cursor to the last line of the block of sentences
  3. Press "by'm to copy the block of sentences from current line where the cursor stop to the line where "m" marker initiated in step 1
  4. Press :e fileB to open another file called fileB for editing

    • Use command code :!e fileB instead of :e fileB if there is a need to continue editing fileA after exiting fileB.

  5. Move the cursor to a position of file where it is intended to paste the copied block of sentences
  6. Press "bp to paste the copied block of sentences from fileA to fileB.

Copy portion of text in a line then paste to elsewhere within a file
  1. Move the cursor to the first character of the intended portion of text to be copied and press mm
  2. Move the cursor to the last character of the intended portion of text to be copied
  3. Press y`m to copy the portion of text from the first character where "m" marker initiated in step 1 to the last character where current cursor stop.
  4. Move the cursor to the intended position of the file
  5. Press p to paste the copied portion of text.
  • To copy portion of text from file A and paste the copied text to file B, use command code "by`m instead of y`m
  • Read carefully that y'm function differently from y`m as shown. They are different syntax, indeed!

Moving around within the file
  1. Press G to move the cursor to the last line of file
  2. Press 1G or gg to move the cursor to the first line of file
  3. Press nG to move the cursor to the n-th line of file
  4. Press CTRL+b to move the cursor backward in 1 full screen
  5. Press CTRL+f to move the cursor forward in 1 full screen
  6. Press 0 (numeric zero) to move the cursor to the beginning of line
  7. Press $ to move the cursor to the end of line
  8. Press h to move the cursor to the left or press CTRL+h while in editing / inserting mode
  9. Press j to move the cursor down or press CTRL+j while in editing / inserting mode
  10. Press k to move the cursor up or press CTRL+k while in editing / inserting mode
  11. Press l to move the cursor up or press CTRL+l while in editing / inserting mode
  • Use the arrow key of the keyboard to move up, down, right, or left if it works in your terminal.

Check the current position or line number of the file edited currently
  1. Press :set nu to turn on display line numbering feature
  2. Press :set nonu to turn off line numbering feature
  3. Press CTRL+G to show current file name, current line number, total lines, and percentage of cursor location in the file.

Search a keyword within the file
  1. Press :set ignorecase to search in case insensitive mode
  2. Press :set hlsearch to turn on feature of highlighting matched search term
  3. Place the cursor under the search term exists in the file. Then press * (asterisk) to instruct Vi editor to search for next occurrence of this search term in forward position.
  4. Press /apple to search for "apple" keyword in forward position.
  5. Press ?apple to search for "apple" keyword in backward position.
  6. To search for "Apple" or "apple", apply the regular expression by typing command code /[Aa]pple or ?[Aa]pple
  • While in searching mode, press N to instruct Vi editor to search in backward position. Press n to instruct Vi editor to search in forward position again.

Replace matched search keyword with new word
  1. Press :s/Color/Colour/gic to replace "Color" with "Colour" in the current line by ignoring case sensitive and ask for confirmation before replacing.

    • the g flag indicate to replace all occurrence of the pattern found within the line
    • the i flag indicate case insensitive
    • the c flag indicate to ask for confirmation before replacing.

  2. Press & while in next line to tell Vi editor repeating the last used :s command (substitution)
  3. Press :1,$s/apple/orange/g or :%s/apple/orange/g to instruct Vi editor to replace all apple found in file to orange
  4. Replace "apple" with "^" in the previous example to tell the Vi editor to insert orange in every beginning of lines in the file.

Blogger Digest :: Notes of regular expression
  1. The . means any single character except newline
  2. The * means zero or more occurrences of any character
  3. The [] means any single character specified inside the bracket
  4. The [^] means any single character not specified inside the bracket
  5. The \< means match at the beginning of the word
  6. The \> means match at the end of the word
  7. The \<ABC\> means match the word "ABC" exactly (and not "deABCfg")
  8. The ^ means match at the beginning of the line
  9. The $ means match at the end of the line
  10. The ^ABC$ means to search for ABC within the line.

Common editing functions
  1. Press A to indicate start of appending at the end of line
  2. Press a to indicate start of appending after the cursor
  3. Press I to indicate start of inserting at the end of line
  4. Press i to indicate start of inserting after the cursor
  5. Press O to insert one blank line before the current line
  6. Press o to insert one blank line after the current line
  7. Press J to join the next line into the end of the current line
  8. Press 3,5J to join the lines of 3rd to 5th into the end of the current line
  9. Press R to indicate start of replacing characters continuously from the current cursor until the start of next command mode (by pressing ESC key)
  10. Press r to replace one single character
  11. Press ~ to toggle character to uppercase or lowercase
  12. Press u to undo one at a time the series of changes made in backward sequence
  13. Press U to undo the latest change made
  14. Press CTRL+R to redo changes made by undo
  15. Press x to delete character to the right of the cursor
  16. Press X to delete character to the left of the cursor
  17. Press dd to delete the whole line where the cursor rest
  18. Press ndd to delete n lines from the line where the cursor rest
  19. Press d$ or D to delete all characters from the cursor to the end of line.
  20. Press N,Md to delete all lines from line N to line M, inclusively.
  21. Press :! top to execute a shell command called "top" (in this example), and return to the edited file when existing from "top" command.
  22. Press : top to execute a shell command called "top" (in this example), and exit to the shell command prompt when existing from "top" command.
  23. Press :!! to repeat the latest executed shell command inside the vi editor
  24. Press CTRL+l to refresh the editing screen
  25. Place the cursor under a filename that exists in the currently edit file, say "stdio.h" that exists in a typical C program source code, then press gf to instruct the Vi editor search (start from the current directory - hence priority of file returned is from current directory) and open the file named "stdio.h"
  26. Place the cursor under a word which is likely part of the Linux man pages, for example "if", then press K will instruct the Vi editor to look for man page of "if" keyword. If there is, the man page is opened right from the Vi editor.
  27. Press :split to split horizontally the currently edited file into two Vi editor windows.
  28. Press :split file2 to split the Vi editor windows horizontally. The upper Vi editor windows access to file2 while the bottom Vi editor windows access to the currently edited file.
  29. While in splitting mode, press CTRL+WW to switch between split Vi editor window. Copy between files made even easier in split Vi editor windows - just copy the block of lines and press CTRL+WW switching to target file then press p to paste the copied lines.

Customize Vi editor environment
  1. Press :set all to print all current VI setting to screen
  2. Press :set nooption to turn off option (e.g. :set nonu to turn off line number printing)
  3. Press :set tabstop=2 to specify each tab equal to 2 spaces
  4. Press :set autoindent to tell Vi perform auto-indention as per previous indented line in the file
  5. Press :set noautoindent to turn off auto-indention
  6. Press :set nu to turn on line number display.
  7. Press :set nohls or :set nohlsearch to turn off feature of highlighting matched search term.
  8. Press :set hls or :set hlsearch to turn on feature of highlighting matched search term.
  9. Press :syntax enable or :syntax on or :syn on to turn on Vi syntax color scheme
  10. Press :syntax off or :syn off to turn off Vi syntax color scheme
  11. If it is very attractive to use Vi editor color scheme, but found that some of the color is dim and not bright enough for easy reading (especially the comment lines), tweak the Vi editor background color and "lighting" by

    • :set bg=dark
               or
      :set bg=light

    • :colorscheme blue
               or
      :colorscheme murphy (best colored aid)
               or
      Find out what others background color schemes are available by looking at the /usr/share/vim/vim61/colors directory. Find the colors directory from the file system if it is installed at different path, by typing

      find / -type d -name colors

      The colors directory contains *.vim file such as blue.vim, murphy.vim, peachpuff.vim, shine.vim, etc.
Note! The Vi editor environment setting could either

  • Place at the bottom of the file content and remarked as comments by the comment identifier. The comment identifier used is depend on the file type. For example, typical C program source code use /* while shell scripts use # as comment identifier. This method is meant to customize Vi editor environment based on file basis rather than apply globally to all files open in Vi editor.

    When the file opened in Vi editor, Vi editor will start to interpret the Vi editor environment setup and respond accordingly. Press :set modeline command code if Vi editor is not responding to the setup automatically.

    • The customization lines seen in a C program source code:

      /* vim: set autoindent: */
      /* vim: set tabstop=2: */

    • The customization lines seen in a typical shell scripts:

      # vim: set autoindent:
      # vim: set tabstop=2:

  • Create a file called ".vimrc" in home directory. This file will be read by VI editor to customize Vi editor environment for all files opened by Vi editor in the current login session as long as there is no customization embedded in the edited file.

    The $HOME/.vimrc file contains one customization setting per line. Hence the $HOME/.vimrc could be seen as

    • set autoindent
      set tabstop=2
      set nu
      set nohls
      syn off

General options to startup VI editor
  1. Type vi + filename to direct Vi editor open the file at the last line of file
  2. Type vi +n filename to direct Vi editor open the file at the n-th line of file
  3. Type vi -r filename to direct Vi editor to edit the last saved version of the file after crash
  4. Type vi -d f1 f2 to open f1 and f2 in Vi editor windows that split vertically. Then, press :diffupdate to highlight the differences found in f1 and f2. Press :set scrollbind will direct Vi editor to automatically refresh and highlight the differences found between f1 and f2. The :set scrollbind is particular useful when one of the files is editing and Vi automatically highlights the latest differences found between these 2 files.

General options to exit VI editor
  1. Press ZZ or :wq or :x to save and exit the edited file
  2. Press :w to save current file without exiting from the edited file.
  3. Press :w f2 to save the edited file to another file named f2 and continue editing the current file. If no save command given to the currently editing file, no changes made will be saved to this current file!
  4. Press :q! to quit the edited file without saving the changes made

Related information:
  • Search more related info with Google Search engine built-in

This article has 1 comments.

Rajiv said...

Hello. Thanks for the wonderful information on the most widely used linux editor(vi). Would recommend this to everyone. im sure even the experienced users of vi would find something new from this blog.

Best Regards,
Rajiv