Repetitive tasks should be done using as many shortcuts as possible. You are not doing anything new and hence not even an extra minute should be spent on doing the same. This post refers to the shortcuts that come in handy when working on the vi/vim editor.
This is not an exhaustive list. These are the ones I use frequently. Feel free to comment down your favorite shortcuts.
Navigation keys
- 0 Moves cursor to the start of the line
- $ Moves cursor to the end of the line
- w Moves forward one word
-
b Moves backward one word
-
G Moves to the end of the file
- 1 + G Moves to the beginning of file
Delete text
- dw Deletes a word ahead of the cursor
- db Deletes a word behind the cursor
- d0 Deletes the complete line till beginning
- d$ Deletes the complete line till the end
- dd Deletes the complete line
- 10dd Deletes the following 5 lines
- dG Deletes till the end of the file
Undo/Redo operation
- u Undo the last operation
- Ctrl + r Redo the last undo
Search and Replace keys
- /search_text Finds 'search_text' in file going forward
- ?search_text Finds 'search_text' in file going backward
- n Finds the next occurrence of 'search_text'
-
N Finds the previous occurrence of 'search_text'
-
:%s/replace_what/replace_with Replaces first occurrence
- :%s/replace_what/replace_with/g Replaces all occurrences globally
- :%s/replace_what/replace_with/c Asks for confirmation
Save and quit
- :q! Force quit without saving
- :wq Saves the changes made to the document
- :wq! Forcefully saves the changes made to the document
- :w new_file_name Saves the file to a new file named new_file_name
Command line shortcuts
- Ctrl + a Brings to the beginning of the line
- Ctrl + e Brings to the end of the line