vi editor shortcuts, cheat sheet
Invoking vi
| Shortcut/command | Description |
|---|---|
| vi file | Invoke vi editor on file |
| vi file1 file2 | Invoke vi editor on files sequentially |
| view file | Invoke vi editor on file in readonly mode |
| vi -R file | Invoke vi editor on file in readonly mode |
| vi -r file | Recover file and recent edits after system crash |
| vi + file | Open file at last line |
| vi +n file | Open file at line number n |
| vi +/pattern | file Open file at pattern |
Inserting in vi editor
| Shortcut/ command | Description |
|---|---|
| i | Insert before cursor |
| I | Insert before line |
| a | Append after cursor |
| A | Append after line |
| o | Open a new line after current line |
| O | Open a new line before current line |
| r | Replace one character |
| R | Replace many characters |
| [Ctrl]v char | While inserting, ignores special meaning of char (e.g., for inserting characters like ESC and CTRL) until ESC is used |
| [Ctrl]i or tab | While inserting, inserts one shift width |
Changes in inserting mode in vi editor
To delete/revert last inserted word/chars during inserting mode, without leaving insert mode, use these commands.
| Shortcut/ command | Description |
|---|---|
| [Ctrl]h | Back /Remove last one character |
| [Ctrl]w | Back one word |
| [Ctrl]u | Back /Remove last Back to beginning of insert |
Deleting in vi editor
| Shortcut/ command | Description |
|---|---|
| x | Delete character to the right of cursor |
| nx | Deletes n characters starting with current; omitting n deletes current character only |
| X | Delete character to the left of cursor |
| nX | Deletes previous n characters; omitting n deletes previous character only |
| D | Delete to the end of the line |
| dd or :d | Delete current line |
| :d | Delete current line |
| ndd | Delete n lines together |
| :n,md | Deletes lines n through m |
| dw | delete word |
| dnw | Delete n words |
| d) | Delete to end of sentence |
| db | Delete previous word |
| ndb | Deletes the previous n words starting with current |
Motion/ Moving in vi editor
| Shortcut/ command | Description |
|---|---|
| h | Move left |
| j | Move down |
| k | Move up |
| l | Move right |
| w | Move to next word |
| W | Move to next blank delimited word |
| b | Move to the beginning of the word |
| B | Move to the beginning of blank delimted word |
| e | Move to the end of the word |
| E | Move to the end of Blank delimited word |
| ( | Move a sentence back |
| ) | Move a sentence forward |
| { | Move a paragraph back |
| } | Move a paragraph forward |
| [[ | Move a section back |
| ]] | Move a section forward |
| 0 or | | Move to the begining of the line |
| n| | Moves to the column n in the current line |
| $ | Move to the end of the line |
| 1G | Move to the first line of the file |
| G | Move to the last line of the file |
| nG | Move to nth line of the file |
| :n | Move to nth line of the file |
| fc | Move forward to c |
| Fc | Move back to c |
| H | Move to top of screen |
| nH | Moves to nth line from the top of the screen |
| M | Move to middle of screen |
| L | Move to botton of screen |
| nL | Moves to nth line from the bottom of the screen |
| % | Move to associated ( ), { }, [ ] |
| Shows filename, current line number, total lines in file, and % of file location | |
| :.= | Display current line number |
| := | Shows number of lines in file |
| | | Displays tab (^l) backslash (\) backspace (^H) newline ($) bell (^G) formfeed (^L^) of current line |
Files, Exit and Save Commands in vi editor
| Shortcut/ command | Description |
|---|---|
| :w file | Write to file |
| :wq | Write to file and quit |
| :w %.new | Save current buffer named file as file.new |
| ZZ | Same as :wq |
| :x | Save and exit. |
| :q! | Quit without saving changes |
| :q | quit |
| :e file2 | Edit other file file2 without leaving vi |
| :e! file2 | Discard changes to current file, then edit file2 without leaving vi |
| :e! | Discard all changes since last save |
| :e# | Edit alternate file |
| :r file | Read file in after line |
| :r!command | Insert the output from a UNIX command into a file, immediately after the cursor |
| :n | Go to next file |
| :p | Go to previos file |
Shell commands in vi editor
| Shortcut/ command | Description |
|---|---|
| :! cmd | Executes shell command cmd; |
| !! cmd | Executes shell command cmd, places output in file starting at current line |
| :!! | Executes last shell command |
| :r! cmd | Reads and inserts output from cmd |
| :f file | Renames current file to file |
| :w !cmd | Sends currently edited file to cmd as standard input and execute cmd |
| :cd dir | Changes current working directory to dir |
| :sh | Starts a sub-shell (CTRL-d returns to editor) |
| :so file | Reads and executes commands in file (file is a shell script) |
| !}sort | Sorts from current position to end of paragraph and replaces text with sorted text |
Yanking Text/cut copy paste in vi editor
| Shortcut/ command | Description |
|---|---|
| yy or Y | Yank the current line |
| :y | Yank the current line |
| yw | Yank word to general buffer |
| “a9dd | Delete 9 lines to buffer a |
| “A9dd | Delete 9 lines; Append to buffer a |
| “ap | Put text from buffer a after cursor |
| p | paste yanked clipboard content after cursor |
| P | paste yanked clipboard content before cursor |
| J | Join lines |
| y$ | yanks to the end of the line |
Changing text in vi editor
| Shortcut/ command | Description |
|---|---|
| C | Change to the end of the line |
| CText | Changes rest of the current line to text until ESC is used |
| cc or s | Change the whole line |
| sText | Substitutes text for the current character until ESC is used |
| xp | Switches character at cursor with following character |
| cw | Change word (Esc) |
| cwText | Changes current word to text until ESC is used |
| c$ | Change to end of line |
| rc | Replace character with c |
| << or >> | Shifts the line left or right (respectively) by one shift width (a tab) |
| n<< or n>> | Shifts n lines left or right (respectively) by one shift width (a tab) |
Markers in vi editor
| Shortcut/ command | Description |
|---|---|
| mc | Set marker c on this line |
| `c | Go to beginning of marker c line. |
| 'c | Go to first non-blank character of marker c line. |
| ' | Return to begining of line containing previous mark |
| `` | Return to previous mark or context |
Search operations in vi editor
| Shortcut/ command | Description |
|---|---|
| /string | Search forward for string |
| ?string | Search back for string |
| n | Search for next instance of string |
| N | Search for previous instance of string |
| fx | search forward for character x in current line |
| Fx | search backward for character x in current line |
| tx | search forward for character before x in current line |
| Tx | search backward for character after x in current line |
| ; | Repeat previous currentline search |
| , | Repeat previous currentline search in opposite direction |
| & | Repeats last :s command |
| :set ic | Ignores case when searching |
| :set noic | Pays attention to case when searching |
| :n,ms/str1/str2/opt | Searches from n to m for str1; replaces str1 to str2; using opt-opt can be g for global change, c to confirm change (y to acknowledge, to suppress), and p to print changed lines |
| :g/str/cmd | Runs cmd on all lines that contain str |
| g/str1/s/str2/str3/ | Finds the line containing str1, replaces str2 with str3 |
| :v/str/cmd | Executes cmd on all lines that do not match str |
Replace operations in vi editor
The search and replace function is accomplished with the :s command. It is commonly used in combination with ranges or the :g command| Shortcut/ command | Description |
|---|---|
| :s/pattern/string/flags | Replace pattern with string according to flags. |
| g | Flag - Replace all occurences of pattern |
| c | Flag - Confirm replaces. |
| & | Repeat last :s command |
Regular expressions in vi editor
| Shortcut/ command | Description |
|---|---|
| . (dot) | Any single character except newline |
| * | zero or more occurances of any character |
| [...] | Any single character specified in the set |
| [^...] | Any single character not specified in the set |
| ^ | Anchor - beginning of the line |
| $ | Anchor - end of line |
| \< | Anchor - begining of word |
| \> | Anchor - end of word |
| \(...\) | Grouping - usually used to group conditions |
| \n | Contents of nth grouping |
Ranges in vi editor
| Shortcut/ command | Description |
|---|---|
| :n,m | Range - Lines n-m |
| :. | Range - Current line |
| :$ | Range - Last line |
| :'c | Range - Marker c |
| :% | Range - All lines in file |
| :g/pattern/ | Range - All lines that contain pattern |
Parameters in vi editor
| Shortcut/ command | Description |
|---|---|
| :set list | Show invisible characters |
| :set nolist | Don’t show invisible characters |
| :set number | Show line numbers |
| :set nonumber | Don’t show line numbers |
| :set autoindent | Indent after carriage return |
| :set noautoindent | Turn off autoindent |
| :set showmatch | Show matching sets of parentheses as they are typed |
| :set noshowmatch | Turn off showmatch |
| :set showmode | Display mode on last line of screen |
| :set noshowmode | Turn off showmode |
| :set all | Show values of all possible parameters |
Scrolling in vi editor
To scroll within vi editor, use following commands.| Shortcut/ command | Description |
|---|---|
| [Ctrl]F | Scroll forward one screen |
| [Ctrl]B | Scroll backward one screen |
| [Ctrl]D | Scroll down onehalf screen |
| [Ctrl]U | Scroll up onehalf screen |
| [Ctrl]E | Show one more line at bottom |
| Ctrl]Y | Show one more line at top of window |
| z | Reposition window: cursor at top |
| nz | Makes the line n the top line on the page |
| z. | Reposition window: cursor in middle |
| nz. | Makes the line n the middle line on the page |
| z- | Reposition window: cursor at bottom |
| nz- | Makes the line n the bottom line on the page |
Other operations in vi editor
| Shortcut/ command | Description |
|---|---|
| ~ | Toggle upp and lower case |
| . | Repeat last text-changing command |
| u | Undo last change |
| U | Undo all changes to line |
tags: vi editor shortcuts, vi editor cheat sheet, vi editor commands, how to navigate in vi editor