vim : vi Improved.
vi refers to visual.
-
Normal mode - Where you enter commands. You'll be in normal mode when you open a file.
-
Insert mode - Where you type in the content of the file, need to press i to enter insert mode after opening a file.
-
Line/command mode - Where you enter command to either save/quit or do some quick operation. Press
Esc and :
to enter this mode.
wq
write and quit
q quit when there is no changes to the file during that session, if there is it give a error saying
no write since last change
. Which means you havn't saved after last change that was being done.
q! exclamation acts as a forced option,
q!
forced quit.
j : move down a line
k : move up a line
h : move left one character
l : move right one character
SIDENOTE:
- Arrow keys works just fine.
Ctrl + f : forward one page
Ctrl + b : backward one page
EXTRA:
z + enter
move the view, keeping your cursor position fixed.
w : move one word forward
W : move one word forward ignoring the punctuations.
b : move one word backward
B : move one word forward ignoring the punctuations.
^ or 0 : move to start of the line
$ : move to end of the line
gg : start of the file
G : end of the file
2gg : go to second line of the file
2G : go to second line of the file
ngg/nG : go to nth line. (n can be any number )
ctrl + g : Show more info about the file, filename line number etc.
:10 - go to 10th line of the file.
:$ - last line of the file.
x : delete the next letter.
X : delete the previous letter.
dw : delete a word.
-
pattern is
d
for delete andw
for move by word.d3w : delete 3 words 3dw : delete a word and repeat 3 times.
-
Generic pattern ( count{operation}count{motion} )
count operation count motion 3 d 3 w delete 3 words and repeat it 3 times.
which is similar to 9dw: delete word 9 times.
dd : delete an entire line. d$ or D : delete till end of the line. dG : delete till end of the file. dgg : delete from cursor position to start of the file.
NOTE:
.
is used to repeat the previous command.
> :help or :h
to get help for particular command.
ex:
:h dd
Give information about dd
command.
Note: Whatever use you inside square bracket in vim help system is optional. ex:
:h dd
dd Delete [count] lines [into register x] |linewise|.
specifying count and into register x is optional.
Ctrl + o : to go previous help search
Ctrl + i : to go next help search
Ctrl + w + w : toggle between your file and help file.
-
Comparison between standard editors vs vim
Standard Vim cut delete d copy yank y paste put p undo : u Redo : ctrl + R
Same pattern + motion works here as well.
3yy - copy 3 lines.
Registers are storage locations.
Types of registers:
* Unnamed registers : ""
* Numbered registers : "0 "1 .... "9
* named registers : "a "b .... "z
Notes about Numbered registers:
* "0 : stores the last yanked data.
* "1 : stores the last deleted data.
* "" : stores the data of last operation
Notes about named registers
* "a : stores the data in register named a.
* "A : Append to the data which is already present in a register.
To know the content of register
:reg
i : to enter insert mode.
I : to start entering from the beginning of the line.
f[any character] : move to that character in the line.
a : insert after the cursor postion.
A : start appending text at the end of the line.
o : enter insert mode on the next line.
O : enter insert mode on the previous line.
J : to join to lines.
R : to enter replace mode
r : to replace only only character without leaving the normal mode.
c : change word.
EX: I love black girls.
Point your cursor at black and then press cw (change word)
and enter your text as you'll be in insert mode and then press escape.
cW : change word with punctuations.
~ : to change the case of a letter.
g~w : change the case of a word.
g~~ : Negate the case of entire line.
gUw : change the entire word after cursor position to uppercase.
guw : change the entire word from cursor position to uppercase.
Create a line of asterisks below:
- 80i --> you'll enter insert mode, enter * and press escape.
Create 3 lines that begin with "-" below:
- 3o --> insert mode, enter - and press escape
f{char} : forward search that character in a line.
F{char} : reverse search that character in a line.
t{char} : forward search till (before that character) that character in a line.
T{char} : reverse search till character in a line.
; : repeat in forward direction.
, : repeat in reverse direction.
/{word} : forward search in the file.
?{word} : reverse search in the file.
n : go to next match.
N : go to prevous match.
* : to highlight all occurance forward.
# : to highlight all occurance backward.
:[range]s/old/new/[flags]
- range can be:
$ : last line.
. : current line.
1s : first line.
1,5s/old/new/g : 1st to 5th line.
1,$ : 1st to last line.
% : Entire file.
OR it can also be pattern
/Global/,/Local/s/old/new/g : from text GLobal till Local do that subtitution.
/Local/,$s/old/new/g : from text Local till end of the file do that
substitution.
Global substitution
:%s/old/new/g
- set is : incremental search
- set hls/hlsearch : highlight search
- set nu : show number
To unset use no{configname}
To toggle conginame!
a : around / all
i : inner/ inside
Ex: To delete a word when a cursor is somewhere in the middle of the text.
- daw : deletes the following delimiter as well like a space, comma etc.
- diw : deletes only the word.
- ciw : change inner word.
Sentences:
- das : delete the sentence including the delimiter.
- cis : change the inner sentence.
-
Operations with text objects
Action explanation p paragraph t tag " double quotes ' single quotes [ , ( , { self explanatory q {name of the register}`: to start recording the macro. @{name of the register} to play that macro. @@ to play the last macro. q{capital of the register} to append to the macro.
-
There are three type of visual modes:
- Characterwise : v to enter characterwise visual mode
- Line wise : V to enter linewise visual mode
- Block wise : Ctrl + v to enter block wise visual mode
-
-
Just some of commands you can use in visual mode include:
~ - Switch case c - Change d - Delete y - Yank r - Replace x - Delete I - Insert A - Append J - Join u - Make lowercase U - Make uppercase > - Shift right < - Shift left
Use to open mutiple files in vim.
:e | :edit : open file from vim
:ls | :buffers : show list of files in buffer
:b | :buffer : switch to other buffer. Ex b{buffer-no} or b {filename}
:bn | :bnext : go to next buffer
:bp | :bprevious : go to previous buffer
:bl | :blast : go to last buffer
:bf | :bfirst : go to first buffer
:badd : add a file to the buffer without moving from your current file.
:bd | :bdelete : delete the file from the buffer.
:bufdo : execute a command on all the buffers.
set hidden - It's a config to say I will move between the buffers without saving and save all at
once if maybe.
:E - Explore directory like ls.
:sp : to split a window horizontally.
:vs : to split a window verically.
ctrl + w w : navigate between the windows.
Use ctrl + w {j/k/l/h} : for moving between the windows.
windo %s/old/new/g : to perform some substitution on all open windows.