Introduction
Learn about the default text editor, vi, pronounced as ”vee eye”, in Unix, Linux and other Unix like systems. A short, concise vi quick reference lets you get started with vi today. Explains vi editor mode & vi commands with examples for editing in vi. Click to download and print
vi editor Cheat Sheet
in PDF format.
In order to work correctly the vi need correct terminal type (TERM) setting depending on the type of terminal you have. Commonly used TERM types are vt100 , vt220 and ansi. In most cases vt100 will work fine. In case vi is not able to understand the TERM you have given, it starts in open mode giving you a line by line display .
TERM’s value is taken from .profile or /etc/profile but can be set at the command line as :
$TERM=vt100
$export TERM
echo $TERM will display the current TERM set.
Modes in vi
vi operates in following two modes :
i.)
Command Mod
e: After a file is opened it is opened in command mode and input from the keyboard will be treated as vi commands, you will not see the words you are typing on the screen
ii.)
Insert Mode
: vi enters in insert mode by pressing letter ‘i’ or ‘a’ and it will accept letter typed as input
iii)
Switch between vi mode
: Press Esc key to swich modes, Esc -> i (text mode) -> Esc (command mode)
- File Open/Save/Quit
- Moving Around
- Editing
- Operations
- Search/Replace
File Open/Save/Quit
| Command | Description |
|---|
| vi filename | Open or create file |
| vi | Open new file to be named later |
| vi -r filename | Recover crashed file |
| view filename | Open file read-only |
| Saving and Quitting |
|---|
| :w |
| :w filename |
| :wq |
| ZZ |
| :q! |
Moving Around
| Moving Cursor in file |
|---|
| h |
| j |
| k |
| l |
| w |
| W |
| b |
| B |
| e |
| Moving Cursor with keys |
|---|
| Return/Enter key |
| Backspace |
| Spacebar |
| Moving on Screen using ctrl key combination |
|---|
| Ctrl-f |
| Ctrl-d |
| Ctrl-b |
| Ctrl-u |
Editing
| Insert Characters and Lines |
|---|
| a |
| A |
| i |
| I |
| o |
| O |
| Text Change Operations |
|---|
| cc |
| cw |
| C |
| s |
| r |
| r Return |
| J |
| xp |
| ~ |
| u |
| U |
| :u |
| Delete Text in file |
|---|
| x |
| X |
| dw |
| dd |
| D |
| dG |
| d1G |
| :1,4 d |
| Copy and Move lines |
|---|
| yy |
| 4yy |
| Y |
| p |
| P |
| :1,2 co 3 |
| :4,6 m 7 |
| Inserting a File Into a File |
|---|
| :r filename |
| :34 r filename |
Operations
| Show/hide Line Numbers |
|---|
| :set nu |
| :set nonu |
| set/ignore case for search |
|---|
| :set ic |
| :set noic |
| Clearing the Screen |
|---|
| Ctrl-L |
Search and Replace
| Search and Replace |
|---|
| /string |
| ?string |
| n |
| N |
| :%s/search/s/replace/g |