Page Contents
Opening File
| Command | Purpose |
| vi | Open new file to be named later |
| vi file_name | Open an existing file or create a new file |
| vi -r file_name | Recover crashed file |
| view filename | Open file in read-only mode |
Savings & Quitting
| Command | Purpose |
| :q | Quit |
| :q! | Quit without saving the changes |
| :w | Save |
| :wq or :wq! | Save and Quit |
| :w filen_name | Save to file “file_name” |
| :e! | Discard unsaved changes, and reopen the file. |
| :sh | Switch to shell temporarily, and return back using Ctrl+d |
Switching to Shell & Run Commands
| Command | Purpose |
| :sh | Switch to shell temporarily, and return back using Ctrl+d |
| :!cmd1 | Execute the command cmd1 |
Modes – Command, Insert/Append & Exit
| Command | Purpose |
| i | Insert characters left of cursor |
| I | Insert characters beginning of line |
| a | Insert characters right of cursor |
| A | Insert characters end of line |
| o | Insert line below cursor |
| O | Insert line above cursor |
| Esc | Insert Mode to Command Mode Exit Mode to Command Mode |
Deleting Text
| Command | Purpose |
| x | Delete character at the cursor |
| X | Delete character to the left of the cursor |
| dw | Delete word or part of word, to right of cursor |
| dd | Delete current line |
| D | Delete part of line to right of cursor |
| dG | Delete to end of file |
| d1G | Delete – beginning of file to cursor |
| :1,5d | Delete lines 1 to 5 |
Navigation – Moving Cursor in file
| Command | Purpose |
| h | Move one character left |
| j | Move one line down |
| k | Move one line up |
| l | Move one character to right |
| w | Move one word right |
| W | Move one word (past punctuation) right |
| b | Move one word left |
| B | Move one word (past punctuation) left |
| e | Move to the end of the current word |
Navigation – Moving on Screen
| Command | Purpose |
| H | Move to top of screen (1st line) |
| M | Move to middle of screen |
| L | Move to bottom of screen (last line on screen) |
Navigation – Moving on Screen using Ctrl key
| Command | Purpose |
| Ctrl+f | Page forward one screen forward |
| Ctrl+d | Scroll forward one half screen |
| Ctrl+b | Page backward one screen |
| Ctrl+u | Scroll backward one-half screen |
Navigation – Go to desired line by line number
| Command | Purpose |
| G | Go to the last line |
| 1G | Go to the 1st line |
| 24G | Go to the 24th line |
Changing Text
| Command | Purpose |
| cc | Change line |
| cw | Change characters in a word or entire word right of cursor |
| C | Change from cursor to end of line |
| s | Substitute string for character(s) from cursor forward |
| r | Replace character with other character at cursor |
| R | Replace characters from the cursor position |
| r Return | Break line at cursor |
| J | Join to current line line below |
| xp | Transpose character cursor and character to right |
| ~ | Change case of letter to uppercase or lowercase at cursor |
| u | Undo previous command |
| U | Undo all changes to current line |
Copying, Pasting and Moving lines
| Command | Purpose |
| yy | Yank or copy current line |
| 5yy | Copy 5 lines |
| Y | Yank or copy line |
| p | Put yanked or deleted line below current line |
| P | Put yanked or deleted line above current line |
| :1,5 co 9 | Copy lines 1-5 and put after line 9 |
| :2,5 m 9 | Move lines 4-5 and put after line 9 |
Show/Hide Line Numbers
| Command | Purpose |
| :set nu | Show line numbers for all lines |
| :set nonu | Hide numbers for all line |
Searching – Ignore/Set case
| Command | Purpose |
| :set ic | Ignore case while searching |
| :set noic | Case sensitive searching |
Search and Replace
| Command | Purpose |
| /string | Search for string anywhere in file |
| ?string | string Search backward for string |
| n | Find next occurrence of string in search direction |
| N | Find previous occurrence of string in search direction |
| :s/old/new/ | Replace the first occurrence of the word “old” with “new” on the current line |
| :s/old/new/g | Replace all occurrences of the word “old” with “new” on the current line. |
| :.,+50s/old/new/ | Replace all occurrence of the word “old” with “new” starting at the current line for the next 50 lines. |
| :1,50s/old/new/ | Replace all occurrence of the word “old” with “new” on lines 1 thru 50. |
| :%s/old/new/ | Replace all occurrences of the word “old” with “new” in the entire file.:e! |
