VIM Basic Usage:
Insert Mode / Command mode:
Enter Insert Mode:
i
Return to Command mode:
esc
Move around in Command mode:
h – left
j – down
k – up
l – right
Word Movement in Command mode:
w – start of next word
e – moves to end of word
b – beginning of the word
Combine numbers with word movement:
3w – pressing w three times
9l – moce nine space to the right
Insert Text repeatedly:
30i- ESC – prints 30 –
Find Characters using f and F / (Combine it with numbers):
fo – find the first occurance of o
3fg – find the 3rd occurance of g
Move in between sets of Parentheses with %:
% – Moves back and fourth between () or {} or []
Move to the beginning of the line and the end of the line:
0 – Move to the beginning of the line
$ – Move to the end of the line
Find the Next/previous occurrence of the current highlighted word:
* – Next occurrence
# – previous occurrence
Move to Beginning/End/Specific Line of the file:
gg – takes you to the beginning of the file
G – takes you to the end of the file
5G – takes you to the 5th line
Search text for a specific pattern then find subsequent/previous matches:
/gum ENTER – find the first occurrance of gum
n – find the next occurrance of what was searched with /
SHIFT+n – go to previous occurrance of what was searched
Inserting text into a new line:
o – creates a new line below current line and brings you to insert mode
O – create a new line above current line and bring you to insert mode
Remove the highlighted character or the character to the left:
x – remove a single character thats highlighted
X – remove a single character before the highligted character
Replace a single character without entering insert mode:
rs – replace one character thats highlighted with s
d is used for deleting:
dw – deletes the first word to the right of the cursor
db – deletes to the beginning of the previous word
de – deletes to the end of the word
d2e – deletes two words
Paste deleted characters or words with p:
p – paste the previously deleted content
Repeat the previous command
. – repeat the previous command
Visual Mode: allows you to select text using movement before you decide what to do with it
v – Enter visual mode
veld – enter visual mode select to the end of the word and delete it
Visual Block Mode: Allows to inset text on many lines at once
ctrl+v – enter visual block mode
Select blocks that you wish to insert into then “L” to prepend the blocks with which text you would like to insert
Saving/Quitting/Qutting without saving:
:w – save/write changes
:q – quit
:q! – quit and don’t save changes
Undo and redo:
u – undo
ctrl+R – redo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
VIM Powerful Navigation:
Single Line Navigation-
^ – go to first non blank character of the line
g_ – go to last non blank character of the line
Screen Navigation-
H – go to first line of the current screen
M – go to the middle line of the current screen
L – go to last line of current screen
ctrl+f – Go down one screen
ctrl+b – Go Up one screen
ctrl+d – Go up half a screen
ctrl+u – Go down half a screen
Special Navigation-
N% – go to the nth% of the file
”’ – go to the position where you were in command mode when last closing file
‘^ -go to the position where you were in insert mode when last closing file
Word/Paragraph Navigation-
Example:
192.168.1.1 – single WORD(Non-blank Characters)
192.168.1.1 – 7 words(sequence of letters, digits, underscores)
e – go to end of current word
E – go to the end of the current WORD
b – go to previous word
B – go to previous WORD
w – go to next word
W – go to next WORD
{ – beginning of current paragraph
{{ – navigate up two paragraphs
} – end of current paragraph
}} – move down two paragraphs
Command Line Navigation options:
vim +N [/path/to/file]
– go to the Nth line of a file after opening
vim +/pattern [file]
– go to the first occurrance of the pattern once the file is opened
vim +?pattern [file]
– go to last occurrance of the pattern once the file is opened
——————————————————————————————————————————————
Find and Replace Usage:
Syntax – :[range]s/[pattern]/[replacevalue]/[flags] [count]
Flags:
c – Confirm each substitution
g – Replace all occurrences in the line
i – ignore case for pattern
Replace all instances of pattern throughout a whole file:
:%s/[pattern]/[new-value]/g
Replace all words in a line with another word and ignore case:
:s/[pattern]/[new-vaule]/gi
Replace all instances of a word with another word in a range of lines:
:number1,number2/[pattern]/[new-vaule]/g
Use Visual Block to Select what to search and replace through:
ctrl+v select block, then :’<,'>s/[pattern]/new_value]/g
Replace all instances of a word with another in a specified amount of lines:
:s/[pattern]/[new_value]/g [number]
Force Whole word Substitution:
:s/\<[pattern] \>/[new_vaule]/
Using an or statement:
:%s/\ ([pattern1]\|[pattern2]\)/[new_value]/g
Using an or statement with Regex:
:%s/\<\ ([pattern1]\|[pattern2]\) \>/[new_vaule]/g
Using Interactive Option:
:%s/[pattern]/[new_vaule]/gc
y – replace current highlighted word
n – don’t replace curent highlighted word
a – replace all highlighted occurrances
l – replace only current highlighted words and terminate find replace
==============================================================================
Automagic Word Completion in Vim:
Word Completion – Move forward – ctrl+x, then ctrl+n
Word Completion – Move backward – ctrl+x, then ctrl+p