A collection of Vi commands in a cheat sheet, handy reference document for learning / remembering Vi commands. I refer to Vim / Vi as the same thing in this document, but in most modern Linux distros vi is often a symlink to vim.
It’s worth learning Vi as it’s installed on pretty much every Linux base system out there.
If you’re learning Vi, you might want to check out Vim Adventures - an online adventure game that uses Vi commands.
Vim Resources
Enjoy the cheat sheet :)
Vi Insert mode & Command Mode
Vi has two basic modes, insert mode - used for entering text and command mode, used for entering commands. See the tip section below for switching between each mode.
Vi Insert Mode
Enter vi insert mode, insert mode is used for inserting text.
Vi Command |
Description |
i |
Enter insert mode from command mode. |
Vi Command Mode - Vi Insert Mode
Vi has two modes, insert mode for inserting text and command mode a common mistake is attempting to edit in command mode. If you are unsure on what mode Vi is using double tap escape (enters command mode) and then hit “i” if you wish to enter insert mode.
Vi Command Mode
Vim Command |
Description |
Esc |
Hit escape to enter command mode. |
Vi File Navigvation
Basic file navigation, how to move up, down, left and right.
Arrow Keys
Modern Vi / Vim editors will allow you to use the arrow keys, but it’s worth learning the correct way to navigate vi without using the arrow keys in case you come across Vi command line or a shell that doesn’t like arrow keys.
Move up, down, left and right in Vim
You’ll need to be in command mode for these commands, navigation in vi uses h j k l
Vim Command |
Description |
h |
Move left - easy to remember h key is on the left |
j |
Move down - I remember it with j(d)own for down |
k |
Move up - k for up - I remember it with (k)up |
l |
Move right - l is on the right side of hjkl and moves you right |
Vi Page Down
Vim Command |
Description |
Ctrl+F |
Vi move forward a page |
Vi Half a Page Down
Vi Command |
Description |
Ctrl+D |
Vi move half a page down |
Vi Page Up
Vi Command |
Description |
Ctrl+B |
Vi move up a page |
Vi Half a Page Up
Vi Command |
Description |
Ctrl+U |
Vi move up half a page |
More Advanced Ways of Entering Insert Mode
Vi Insert Text at Start of the Line
Vi Command |
Description |
I |
Insert text at the beginning of the line |
Vi Insert Text at the end of the Line
Vi Command |
Description |
A |
Appends text at the end of the line |
Vi Append text to the right of the Cursor
Vi Command |
Description |
a |
Appends text to the right of the cursor |
Begin a new line below
Vi Command |
Description |
o |
Begin a new line, below the current line |
Vi replace line
Vi Command |
Description |
O |
Removes line, and allows you to type a new line in it’s place |
Vi Replace
Change a Word in Vi
Vi Command |
Description |
cw |
Replaces a single word, place cursor on first letter and hit cw (Change Word) |
Replace line, but not wrapped text
Vi Command |
Description |
c$ |
Replaces the current line but doesn’t extend to change the rest of a wrapped sentence on the screen |
Vi Replace Character
Vi Command |
Description |
r |
Replaces only the character under the cursor |
Vi Replace
Vi Command |
Description |
R |
Replaces over the top of existing text, until the user hits return. |
Vi Delete
Vi Delete Single Character After the Cursor
Vi Command |
Description |
x |
Vi deletes single character after the cursor |
Vi Delete Character before the Cursor
Vi Command |
Description |
X |
Vi deletes character before the cursor. |
Vi Delete Word
Vi Command |
Description |
dw |
Vi Delete Word, deleted the word under the cursor, from the curosr position onward |
Vi Delete Line commands
Vi Delete Line
Vi Command |
Description |
dd |
Delete the current line in Vi |
Vi Delete until end of Line
Vi Command |
Description |
D |
Deletes from cursor to end of line |
Delete to end of screen
Vi Command |
Description |
dL |
Deletes from cursor to end of screen |
Delete to end of file
Vi Command |
Description |
dG |
Deletes from cursor to end of file |
Vi Delete From Cursor To Start of Line
Vi Command |
Description |
d^ |
Deletes from cursor to start of line |
Vi Copy and Paste
Vi Copy Line
Vi Command |
Description |
yy |
Copies current line into unnamed buffer |
Vi Copy 3 Lines of Text
Vi Command |
Description |
3yy |
Copy 3 lines of text into unnamed buffer |
Vi Copy Word
Vi Command |
Description |
yw |
Copy word under cursor into unnamed buffer |
Vi Copy 3 Words
Vi Command |
Description |
3yw |
Copy 3 words into unnamed buffer |
Vi Paste Commands
Vi Command |
Description |
P |
Copy contents of unamed buffer to right of cursor |
Vi Command |
Description |
p |
Copy contents of unamed buffer to left of cursor |
Vi Search Commands
Vi Search forward
Vi Command |
Description |
N |
Vi search forward in file |
Vi Search Back
Vi Command |
Description |
SHIFT+N |
Vi search backward in file |
Vi Search and Replace Commands
Vi Search and Replace First Instance
Vi Command |
Description |
:s/find-string/replace-string/ |
Vi search and replace first instance of specified string |
Vi Search and Replace on a Single Line
Vi Command |
Description |
:s/find-string/replace-string/g |
Vi search and replace all instances of specified string on current line |
Vi Search and Replace Entire File
Vi Command |
Description |
:%s/find-string/replace-string/g |
Vi search and replace all instances of specified string for entire file |
Vi Search for part of a Word
A fuzzy search allows you to find something that you only know part of, for example if you wanted to find all instances of lines starting with the word “Picard” you would use the following:
Vi Command |
Description |
/^Picard |
Vi search within file words starting with Picard |
Vi Search for words ending with $string
Vi Command |
Description |
/worf$ |
Vi search within file for word engine with worf |
Vi Command |
Description |
/\* |
Vi search within file for metacharacters like, * |
Vi Exact Match Search Only
Vi Command |
Description |
/star\. |
Vi exact search only, will return instances of “star only”, not starfleet or star-trek |
Vi Search for a range of Strings
Helpful for finding version numbers in text files.
Vi Command |
Description |
/v2.[1-9] |
Vi search for a range, this example would return all v2.1-9 instances within the file, e.g. v2.4 v2.7 etc |
Vi Search for Upper and Lowercase
Search for upper and lowercase strings in Vi.
Vi Command |
Description |
/ [tT] [hH [eE] |
Vi search upper or lowercase strings, this example would return any instance of the word ‘the’. e.g. The, THE, tHE, tHe |
Advanced Vi commands
Vi View Options
Vi Command |
Description |
:set all |
Lists all Vi options |
Vi Run Shell Commands
Run shell commands from Vi.
Vi Command |
Description |
:! ls -l |
Run shell command from Vi, in this example ls -l is executed |
Vi Joining Lines
Backspace doesn’t always work…
Vi Command |
Description |
SHIFT+J |
Position the cursor in either line you wish to join, and press SHIFT+J |
Vi Split Windows
Useful for comparing files, to switch between windows press SHIFT+W
Vi Split Window Horizontally
Vi Command |
Description |
:split |
Split window Horizontally in Vi |
Vi Split Window Virtically
Vi Command |
Description |
:vsplit |
Split window Virtically in Vi |
Vi Close All Split Windows
Vi Command |
Description |
:only |
Closes all split windows and focuses on the primary window |
Vi Save commands
How to Save in Vi
Vi Command |
Description |
:w |
Writes the file to disk |
Vi Save and Exit
How to save and exit Vi, personally I use :wq
Vi Command |
Description |
:wq |
Save and exit Vi |
| :x
| Exit - Vi will prompt and ask if you wish to save |
| SHIFT+ZZ
| Another way to Save and Exit Vi |
| wq!
| Forces save on read only files, and exits |
Misc Vi Commands
Vi Undo Command
Vi Command |
Description |
U |
Press SHIFT+U to undo in Vi |
Vi Undo All
Vi undo all since last write.
Vi Command |
Description |
:+X+! |
Undo everything since last write |
Vi Show File Name
SHIFT+G
shows the file name, number of lines and current position.
Vi Multipliers
Almost every command in Vi can leverage multipliers, typically it’s a case of prefixing the command with a numnber. Example: 10W
would move 10 words to the right.
原文链接:HighOn.Coffee