Linux Commands | Shell Script | Vi Editor Commands
A quick reference for starting vi, moving the cursor, editing text, copying, deleting, searching, replacing, saving, and exiting
Vi, VIM (Vi IMproved)
Vi is a representative Linux editor known for fast editing.
vi is short for Visual.
Starting vi
| Command |
Description |
Example |
| vi {file name} |
Open or create a file |
vi test.txt |
| vi +{line number} {file name} |
Open a file and move the cursor to the specified line |
vi -100 test.txt |
| vi +/"{search string}" {file name} |
Open from the first occurrence of the string |
vi -/“adc” test.txt |
| vi -r {file name} |
Recover a damaged file |
vi -r test.txt |
| view {file name} |
Open as read-only |
view test.txt |
Opening an existing file
When you run vi {file name} and the specified file already exists, vi opens it for editing.
Creating a new file
When you run vi {file name} and the specified file does not exist, vi creates it.
Moving the Cursor in vi
| Key |
Description |
| h (left arrow) |
Move the cursor left |
| j (down arrow) |
Move the cursor down |
| k (up arrow) |
Move the cursor up |
| l (right arrow) |
Move the cursor right |
| w |
Move to the end of the next word on the right |
| e |
Move to the beginning of the next word on the right |
| b |
Move to the beginning of the previous word on the left |
| Enter |
Move down one line |
| Backspace |
Move left one character |
| Space Bar |
Move right one character |
| ^ |
Move to the far left of the line |
| $ |
Move to the far right of the line |
| H |
Move to the top of the screen |
| M |
Move to the middle of the screen |
| L |
Move to the bottom of the screen |
| numberG |
Move to the specified line number |
| Ctrl + i |
Move up one screen |
| Ctrl + b |
Move down one screen |
| Ctrl + d |
Move up half a screen |
| Ctrl + u |
Move down half a screen |
| Ctrl + e |
Scroll up one line |
| Ctrl + y |
Scroll down one line |
Character and Line Insertion Commands
| Key |
Description |
| a |
Insert text to the right of the cursor |
| A |
Insert text at the end of the current line |
| i |
Insert text to the left of the cursor |
| I |
Insert text at the beginning of the current line |
| o |
Insert a line below the cursor |
| O |
Insert a line above the cursor |
| ESC |
Exit insert mode |
Text Change Commands
| Command |
Description |
| cw |
Change a word |
| cc |
Change a line |
| C |
Change the line to the right of the cursor |
| s |
Replace the character at the cursor |
| S |
Replace the current line |
| r |
Replace the character at the cursor with another character |
| r-Enter |
Split a line |
| J |
Join the current line with the next line |
| xp |
Swap the character at the cursor with the character to its right |
| ~ |
Toggle character case |
| u |
Undo the previous command |
| U |
Undo changes to the line, restoring the previous final state of the line |
| . |
Repeat the previous command |
Text Deletion Commands
| Command |
Description |
| x |
Delete the character at the cursor |
| nx |
Delete n characters from the cursor position |
| dw |
Delete one word at the current cursor position |
| dd |
Delete the line at the cursor |
| ndd |
Delete n lines from the cursor line |
| db |
Delete one word backward from the cursor position |
| D |
Delete the line to the right of the cursor |
| :5,10d |
Delete lines 5 through 10 |
Copy and Move Commands
| Command |
Description |
| yy |
Copy the line at the cursor |
| Y |
Yank or copy a line |
| yh |
Copy the character to the left of the cursor |
| yl |
Copy the character at the cursor |
| yi |
Copy the current line and the line below it |
| yk |
Copy the current line and the line above it |
| p |
Insert a yanked or deleted line above the current line |
| P |
Insert a yanked or deleted line below the current line |
| :1,2 co 3 |
Copy lines 1 through 2 after line 3 |
| :4,5 m 6 |
Move lines 4 through 5 above line 6 |
- yank: to pull or copy text into a buffer.
Copying One Line
Press yy, that is, press y twice on the target line, to store it in the buffer.
Move to the place where you want to paste it and press p; the line is pasted after the cursor line.
Copying a Block
Press v, then move the cursor to select a block.
In PuTTY, the selected block is visible. In some SSH environments, the block may not be shown visually, but it is still selected.
After selecting the target block, press y to copy it into the buffer.
Move to the desired location in the same way and press p; the copied text is pasted after the cursor line.
Line Number Commands
| Command |
Description |
| :set nu or :set number |
Show line numbers on the left side of each editor line |
| :set nonu |
Hide line numbers on the left side of each editor line |
Finding Lines
| Command |
Description |
| G |
Go to the last line of the file |
| 21G |
Go to line 21 of the file |
| Ctrl + G |
Show the current file name and cursor line information |
Search and Replace Commands
| Command |
Description |
| /{search string} |
Search for a string downward and to the right |
| ?{search string} |
Search for a string upward and to the left |
| n |
Continue searching for the next occurrence |
| N |
Continue searching for the previous occurrence |
| :g/search-string/s/ |
Search each occurrence, confirm it, and replace it |
| :s/string/rep |
Replace str in the current line with rep |
| :1,.s/string/rep/ |
Replace str from line 1 through the current line with rep |
| :%s/string/rep/g |
Replace every str in the whole file with rep |
| :.$/aaa/bbb |
Replace every aaa from the cursor position to the end of the file with bbb |
Screen Cleanup Command
| Command |
Description |
| Ctrl + l |
Clear unnecessary screen content and redraw the screen |
File Commands
| Command |
Description |
Example |
| :r {file name} |
Insert a file after the cursor |
:r test.txt |
| :{line number} r {file name} |
Insert the specified file after the specified line number |
:10 r test.txt |
Save and Exit Commands
| Command |
Description |
| :w |
Save changes |
| :w {file name} |
Save changes with the specified file name |
| :wq |
Save changes and exit vi. Same as ZZ. Runs :w (write) and :q (quit) in sequence |
| ZZ |
Save changes and exit vi. Writes the temporary buffer to the file used when vi was opened, then exits vi |
| :q! |
Exit without saving changes |
| q |
Exit vi without saving the modified file |
| e! |
Discard changes and return to the editing state |
Other Symbols
| Symbol |
Description |
| . |
Current line |
| % |
All lines |
| $ |
Last line of the file |
| 1,$ |
Same as % |
| 2,3 |
Lines 2 through 3 |
Reference
http://blog.naver.com/youngrimi/50086851943