Vi Editor Quick Reference
Read this brief introduction of Vi editor cheat sheet for better understanding on the guides follow:
- 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! - Bold font used to denote Vi editor's command code.
- To execute Vi command code, press ESC to enter command mode.
- On start up, Vi editor is in neutral mode. Press ESC follow by i to start editing (insert) mode.
- 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.
- 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
- 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
- :set bg=dark
- Press V to mark the whole line under the cursor, continuous as cursor move. Press v to start highlight as cursor move.
- To get "online" help on any of the Vi editor command codes, press :help command code in the editor.
- Move the cursor to the first line of the block of sentences and press mm
- Move the cursor to the last line of the block of sentences
- 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
- 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
- Move the cursor to a position of file where it is intended to paste the copied block of sentences
- Press p to paste the copied block of sentences.
- Move the cursor to the first line of the block of sentences and press mm
- Move the cursor to the last line of the block of sentences
- 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
- 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.
- Use command code :!e fileB instead of :e fileB if there is a need to continue editing fileA after exiting fileB.
- Move the cursor to a position of file where it is intended to paste the copied block of sentences
- Press "bp to paste the copied block of sentences from fileA to fileB.
- Move the cursor to the first character of the intended portion of text to be copied and press mm
- Move the cursor to the last character of the intended portion of text to be copied
- 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.
- Move the cursor to the intended position of the file
- 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!
- Press G to move the cursor to the last line of file
- Press 1G or gg to move the cursor to the first line of file
- Press nG to move the cursor to the n-th line of file
- Press CTRL+b to move the cursor backward in 1 full screen
- Press CTRL+f to move the cursor forward in 1 full screen
- Press 0 (numeric zero) to move the cursor to the beginning of line
- Press $ to move the cursor to the end of line
- Press h to move the cursor to the left or press CTRL+h while in editing / inserting mode
- Press j to move the cursor down or press CTRL+j while in editing / inserting mode
- Press k to move the cursor up or press CTRL+k while in editing / inserting mode
- 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.
- Press :set nu to turn on display line numbering feature
- Press :set nonu to turn off line numbering feature
- Press CTRL+G to show current file name, current line number, total lines, and percentage of cursor location in the file.
- Press :set ignorecase to search in case insensitive mode
- Press :set hlsearch to turn on feature of highlighting matched search term
- 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.
- Press /apple to search for "apple" keyword in forward position.
- Press ?apple to search for "apple" keyword in backward position.
- 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.
- 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.
- Press & while in next line to tell Vi editor repeating the last used :s command (substitution)
- Press :1,$s/apple/orange/g or :%s/apple/orange/g to instruct Vi editor to replace all apple found in file to orange
- Replace "apple" with "^" in the previous example to tell the Vi editor to insert orange in every beginning of lines in the file.
- The . means any single character except newline
- The * means zero or more occurrences of any character
- The [] means any single character specified inside the bracket
- The [^] means any single character not specified inside the bracket
- The \< means match at the beginning of the word
- The \> means match at the end of the word
- The \<ABC\> means match the word "ABC" exactly (and not "deABCfg")
- The ^ means match at the beginning of the line
- The $ means match at the end of the line
- The ^ABC$ means to search for ABC within the line.
- Press A to indicate start of appending at the end of line
- Press a to indicate start of appending after the cursor
- Press I to indicate start of inserting at the end of line
- Press i to indicate start of inserting after the cursor
- Press O to insert one blank line before the current line
- Press o to insert one blank line after the current line
- Press J to join the next line into the end of the current line
- Press 3,5J to join the lines of 3rd to 5th into the end of the current line
- Press R to indicate start of replacing characters continuously from the current cursor until the start of next command mode (by pressing ESC key)
- Press r to replace one single character
- Press ~ to toggle character to uppercase or lowercase
- Press u to undo one at a time the series of changes made in backward sequence
- Press U to undo the latest change made
- Press CTRL+R to redo changes made by undo
- Press x to delete character to the right of the cursor
- Press X to delete character to the left of the cursor
- Press dd to delete the whole line where the cursor rest
- Press ndd to delete n lines from the line where the cursor rest
- Press d$ or D to delete all characters from the cursor to the end of line.
- Press N,Md to delete all lines from line N to line M, inclusively.
- Press :! top to execute a shell command called "top" (in this example), and return to the edited file when existing from "top" command.
- Press : top to execute a shell command called "top" (in this example), and exit to the shell command prompt when existing from "top" command.
- Press :!! to repeat the latest executed shell command inside the vi editor
- Press CTRL+l to refresh the editing screen
- 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"
- 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.
- Press :split to split horizontally the currently edited file into two Vi editor windows.
- 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.
- 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.
- Press :set all to print all current VI setting to screen
- Press :set nooption to turn off option (e.g. :set nonu to turn off line number printing)
- Press :set tabstop=2 to specify each tab equal to 2 spaces
- Press :set autoindent to tell Vi perform auto-indention as per previous indented line in the file
- Press :set noautoindent to turn off auto-indention
- Press :set nu to turn on line number display.
- Press :set nohls or :set nohlsearch to turn off feature of highlighting matched search term.
- Press :set hls or :set hlsearch to turn on feature of highlighting matched search term.
- Press :syntax enable or :syntax on or :syn on to turn on Vi syntax color scheme
- Press :syntax off or :syn off to turn off Vi syntax color scheme
- 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.
- :set bg=dark
- 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
- Type vi + filename to direct Vi editor open the file at the last line of file
- Type vi +n filename to direct Vi editor open the file at the n-th line of file
- Type vi -r filename to direct Vi editor to edit the last saved version of the file after crash
- 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.
- Press ZZ or :wq or :x to save and exit the edited file
- Press :w to save current file without exiting from the edited file.
- 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!
- Press :q! to quit the edited file without saving the changes made
- Search more related info with Google Search engine built-in
This article has 1 comments.
You may have your comments on this article too...
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
So, feel free to write your comments for this article...