VI/Vim Editor Tutorial for Linux
The best way to learn Vi is to create a new file and try it out for yourself. Vi is a powerful, ubiquitous text editor found on all Unix/Linux systems. Vim (Vi IMproved) is an enhanced version with more features. Both are modal editors with different modes for different operations.
Starting vi Editor:
| 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 |
| vi +num filename | Open file at line number |
| vi +/pattern filename | Open file at first pattern match |
Operation Modes:
- Command Mode: Where you give commands (delete, copy, save, etc.) – Press ESC to return to this mode
- Insert Mode: Where you actually type text – Enter with: i, I, a, A, o, O
- Visual Mode: For selecting text – Enter with: v (character), V (line), Ctrl+v (block)
Cursor Movement Commands:
| Command | Description |
| h | Move left one character |
| j | Move down one line |
| k | Move up one line |
| l | Move right one character |
| Spacebar | Move right one character |
| Return | Move down one line |
| Backspace | Move left one character |
| 0 or | | Move to beginning of line |
| $ | Move to end of line |
| w | Move to next word beginning |
| W | Move to next word (past punctuation) |
| b | Move to previous word beginning |
| B | Move to previous word (past punctuation) |
| e | Move to end of current word |
| E | Move to end of word (past punctuation) |
| H | Move to top of screen |
| M | Move to middle of screen |
| L | Move to bottom of screen |
| G | Go to last line of file |
| 1G | Go to first line of file |
| 21G | Go to line 21 |
| nG or :n | Go to line n |
| gg | Go to first line in file |
| % | Move to matching bracket |
| ) | Move to next sentence |
| ( | Move to previous sentence |
| } | Move to next paragraph |
| { | Move to current paragraph |
| ]] | Move to next section |
| [[ | Move to current section |
Screen Movement Commands:
| Command | Description |
| Ctrl+f | Page forward one screen |
| Ctrl+b | Page backward one screen |
| Ctrl+d | Scroll forward one-half screen |
| Ctrl+u | Scroll backward one-half screen |
| Ctrl+e | Scroll down one line |
| Ctrl+y | Scroll up one line |
| Ctrl+l | Clear (refresh) scrambled screen |
Inserting Characters and Lines:
| Command | Description |
| i | Insert text before cursor |
| I | Insert text at beginning of line |
| a | Insert text after cursor |
| A | Insert text at end of line |
| o | Open new line below cursor |
| O | Open new line above cursor |
Deleting Text:
| Command | Description |
| x | Delete character at cursor |
| X | Delete character before cursor |
| dw | Delete word from cursor position |
| d^ | Delete from cursor to beginning of line |
| d$ | Delete from cursor to end of line |
| D | Delete from cursor to end of line |
| dd | Delete entire line |
| Xdd | Delete X number of lines |
| dG | Delete to end of file |
| d1G | Delete from beginning to cursor |
| :5,10 d | Delete lines 5-10 |
Changing Text:
| Command | Description |
| cw | Change word from cursor position |
| cc | Change entire line |
| C | Change from cursor to end of line |
| s | Substitute character |
| S | Substitute entire line |
| r | Replace character at cursor |
| R | Replace multiple characters (overwrite) |
| r Return | Break line |
| J | Join current line with next line |
| xp | Transpose character at cursor |
| ~ | Change case of letter |
| Ctrl+a | Increment number under cursor |
| Ctrl+x | Decrement number under cursor |
Undo/Redo Commands:
| Command | Description |
| u | Undo last change |
| U | Undo all changes on current line |
| Ctrl+r | Redo last change |
| :u | Undo previous command-line command |
Copying and Moving Text:
| Command | Description |
| yy or Y | Yank (copy) current line |
| yw | Yank current word |
| Xyy | Yank X number of lines |
| p | Put yanked text below current line |
| P | Put yanked text above current line |
| :1,2 co 3 | Copy lines 1-2 and put after line 3 |
| :4,5 m 6 | Move lines 4-5 and put after line 6 |
Searching Commands:
| Command | Description |
| /string | Search forward for string |
| ?string | Search backward for string |
| n | Find next occurrence |
| N | Find previous occurrence |
| * | Search for word under cursor |
| /\ | Search for exact word |
| fx | Find character x in current line |
| Fx | Find character x backward in line |
| tx | Find before character x in line |
| Tx | Find before character x backward |
Search Patterns:
| Pattern | Description |
| ^ | Beginning of line |
| $ | End of line |
| . | Any single character |
| * | Zero or more of previous character |
| [abc] | Any character a, b, or c |
| \d | Any digit (0-9) |
| + | One or more of previous character |
| ? | Zero or one of previous character |
| string1|string2 | Either string1 or string2 |
Replacing Text:
| Command | Description |
| :s/old/new | Replace first occurrence in line |
| :s/old/new/g | Replace all in current line |
| :%s/old/new/g | Replace all in file |
| :%s/old/new/gc | Replace with confirmation |
| :5,10s/old/new/g | Replace in lines 5-10 |
| :g/search/s//replace/g | Search and replace globally |
File Operations:
| Command | Description |
| :w | Save file |
| :w filename | Save as filename |
| :w! | Save (force) |
| :wq or ZZ | Save and quit |
| :q | Quit (if no changes) |
| :q! | Quit without saving |
| :qa | Quit all files |
| :e filename | Edit another file |
| :r filename | Insert file after cursor |
| :34 r filename | Insert file after line 34 |
| :f | Show file info |
| :f filename | Rename current file |
| :cd dirname | Change directory |
Multiple Files:
| Command | Description |
| :n | Next file |
| :p | Previous file |
| :N | Previous file |
| :args | Show file list |
| :e# | Toggle between two files |
Advanced Commands:
| Command | Description |
| << | Shift line left |
| >> | Shift line right |
| :20,40> | Shift lines 20-40 right |
| :20,40< | Shift lines 20-40 left |
| Ctrl+g | Show file status |
| . | Repeat last command |
| ma | Mark line as ‘a’ |
| ‘a | Jump to mark ‘a’ |
| ]’ | Next mark |
| [‘ | Previous mark |
| q[register] | Start recording macro |
| @[register] | Execute macro |
| @@ | Repeat last macro |
Set Commands (Configuration):
| Command | Description |
| :set nu | Show line numbers |
| :set nonu | Hide line numbers |
| :set ic | Ignore case in searches |
| :set noic | Case sensitive searches |
| :set ai | Autoindent |
| :set noai | No autoindent |
| :set sw=4 | Set shift width to 4 |
| :set ts=8 | Set tab stop to 8 |
| :set ws | Wrap scan |
| :set wm=2 | Wrap margin 2 chars |
| :set list | Show tabs/ends |
| :set nolist | Hide tabs/ends |
| :set ro | Read-only mode |
| :set bg=dark | Dark background |
| :set bg=light | Light background |
| :set syntax on | Syntax highlighting |
| :set all | Show all settings |
Vim Command Line Arguments:
| Argument | Description |
| vim +num file | Open at line num |
| vim +/pattern file | Open at pattern |
| vim -b file | Binary mode |
| vim -d file1 file2 | Diff mode |
| vim -R file | Read-only mode |
| vim -x file | Encryption mode |
| vim -r file | Recovery mode |
Running Commands:
| Command | Description |
| :!command | Run shell command |
| :!ls | List files |
| :!pwd | Show directory |
| :sh | Start shell |
| Ctrl+Z | Suspend vim |
| fg | Resume vim |
Buffer Operations:
| Command | Description |
| :ls | List buffers |
| :bn | Next buffer |
| :bp | Previous buffer |
| :bd | Delete buffer |
| “ayy | Yank to buffer a |
| “ap | Paste from buffer a |
| “a5dd | Delete 5 lines to buffer a |
Visual Mode:
| Command | Description |
| v | Visual character mode |
| V | Visual line mode |
| Ctrl+v | Visual block mode |
| y | Yank selected |
| d | Delete selected |
| > | Indent right |
| < | Indent left |
| :'<,’>w file | Write selection to file |
Getting Help:
| Command | Description |
| :help | Open help |
| :help topic | Help on topic |
| vimtutor | Interactive tutorial |
| :version | Show version |
Practice Exercises:
- Create a new file:
vi practice.txt - Enter insert mode (i) and type several lines
- Practice navigation (h,j,k,l, w,b,$,0,G)
- Delete a line (dd) and undo it (u)
- Copy a line (yy) and paste it (p)
- Search for a word (/word)
- Replace text (:s/old/new/g)
- Save and exit (:wq or ZZ)
Quick Reference (Most Essential):
- i – Enter insert mode
- ESC – Return to command mode
- :wq – Save and quit
- :q! – Quit without saving
- dd – Delete line
- yy – Copy line
- p – Paste
- u – Undo
- /text – Search
- :s/old/new/g – Replace
Tip: Start with vimtutor – it’s a 30-minute interactive tutorial that comes with Vim. The best way to learn Vi/Vim is through practice.
Remember: Vim has a steep learning curve but pays off with increased productivity once mastered. Start with basics and gradually incorporate advanced features!