Vim
Move cursor vim
Arrow key replacements
h
- Leftj
- Downk
- Upl
- Right
Move one Word
w
- move the cursor to the RIGHT one wordb
- move the cursor to the LEFT one word
Move to Start or End of LINE
^
or0
- move the cursor to the START of the line$
- move the cursor to the END of the line
Move to Start or End of FILE
gg
- move the cursor to the BEGINNING of the FileG
- move the cursor to the END of the File
Move to Start or End of SCREEN
H
- (High) move the cursor to the BEGINNING of the ScreenM
- (Middle) move the cursor to the MIDDLE of the ScreenL
- (Low) move the cursor to the END of the Screen
Scroll Screen vim
scroll down
Ctrl-f
scroll forward one screen (f for forward or down )Ctrl-d
scroll forward one half of a screen (d for down)
scroll up
Ctrl-b
scroll backward one screen (b for backward or up)Ctrl-u
scroll backward one half of screen (u for up)
Delete vim
delete word
dw
- to delete char from cursor to the beginning of the next wordPlace the cursor at the beginning to delete the whole word.
if the cursor is placed somewhere in the word, the command will delete from the cursor position to the end of the word.diw
- ignore the cursor position in the word and delete the whole worddt<char>
- delete the char's from the current cursor position till the first encounter of the char<char>
delete till end of line
d$
- Delete the char's from the current cursor position till the end of the line.
delete line
dd
- Delete the whole line.
delete number of line UP
2d-
- Delete 3 linesUP
from the current cursor position including the line of the cursor.
delete number of lines DOWN
3dd
- Delete 3 linesDOWN
from the current cursor position including the line of the cursor.
delete till End of file
dG
- Delete from the cursor position to theend of the file
.
Advanced Vim operations
process the txt file with shell commands like grep
, cut
, jq
etc without leaving vim
:%!grep succeeded
%
- current file
!
- execute the following command and input back into the file
Search and replace in vim
:%s/\/ope/**/g
:%s/\*\*/\/ope/g
delete in vim
Delete all the spaces
:%s/\s*//g
Delete the line with word
:g/searchword/d
Delete all the lines excluding
Delete all the line excluding the lines containing /bash
:g!/\/bash/d
replace in vim
replace all new line char with ,
:%s/\n/,/g ( Replace new line char with , )
if \n
not working try \r
Replace all comma with new line
:%s/,/\r/g
Append in vim
Append string at end of each line
:%norm Asomething"
%
apply on each line
norm
execute the commands followed afternorm
. i.e commandA
A
Append command in Vim
something
string to append
Insert in vim
Insert at the BEGINNING of each line
Insert hello at the beginning of each line
:%s/^/hello : /
Insert new line at the END of each line
Insert NEW LINE at the beginning of each line
:%s/$/\r/g
copy in vim
Copy all the lines to clipboard
gg"*yG
undo or redo in vim
u
- Undoctrl + r
- Redo
set line numbers
Numbers each line down the left side.
:set number
json format the file in vim
format json file in vim
:%!jq .
Append at the end of file
Go to bottom of the file and in append text mode. In other words, jump to last line and start writing code/text.
Esc + G + A
Record macro with @q
- In command mode hit
qa
- this starts recording for macros witha
as command - Do the changes needed to repeat with the command
a
- exit insert mode and then hit
q
to stop the recording - in command mode enter
@a
- This will repeat the macro recorded fora