Basic vim movements
jk
-> down and uphl
-> left and right
wb
-> hop forward by a word and back by a worde
-> hop forward to the end of a wordge
-> hope backward to the end of a word
Using the capitalized version moves according to code blocks considered as WORDS
W,B,E,gE
A word in vim is either:
- A sequence of letters, digits and numbers
- A sequence of other non-blank characters
here are 4 words
and these below are words as well
,,, .... ((((())))) ,.(
Vim also has the concept of special kinds of words (with letters, digits and numbers) that also include special characters like ., (, {, etc. They are called WORDs in Vim jargon.
word
|----------|
helloVimUser(){}
|--------------|
WORD
Iam_A_WORD(WORD)
sum(2, 3)
[1, 2, 3, 4, 5, 6] -- with non blank characters is considered a word
yy
-> yanks a linep
-> pastes one line below the current line
dd
-> deletes that linecw
-> deletes a word and enters insert modeu
-> undo the last action
shift + v -> visual mode for the entire line
When pressing either y or d while in visual mode, copy or delete, it will be saved to the register.
v
-> will highlight the current place
i
-> insert modex
-> deletes a single character
o
inserts below that lineO
inserts on topa
moves over and insertsSHIFT + a
inserts at the end of a lineSHIFT + P
inserts one line aboveSHIFT + i
inserts at the beginning of a lineSHIFT + d
deletes the rest of the lineSHIFT + c
deletes the rest of the line and enter insert modeSHIFT + s
deletes the entire line and enters insert mode
Move to a specific Character
f{character}
-> (find) to move to the next occurrence of a character in a line. f" sends you to the next double quote.
F{character}
finds the previous occurrence of a character.
t{character}
moves the cursor just before the next occurrence of a character.
T{character}
does the same, but backwards.
t
is useful when combined to perform text changes.
;, are useful to repeat the last character search.
Move horizontally extremely
0
Moves to the first character of a line^
Moves to the first non-blank character of a line$
Moves to the end of a lineg_
Moves to the non-blank character at the end of a line
Moving vertically
}
jumps entire paragraphs downwards{
similarly but upwardsCTRL-D
let's you move down half a page by scrolling the pageCTRL-U
let's you move up half a page also by scrollinggg
Goes to the topG
goes to the bottomdiw
delete word if in the middle of the word
High precision vertical motions with search pattern
/{pattern}
to search forward inside a file?{pattern}
to search backwards
{pattern}
can be a regular expression
hit ENTER. You can do some editing if you want and later
use n to jump to the next match (N for the previous one)
/ or ? to run the last search
- Does a search for the word under the cursor
Does the same backwards
{count}{motion}
does motion {count} times
Moving Semantically
gd
to jump to definition of whatever is under your cursorgf
to jump to a file in an import
gg
to go to the top of the file{line}gg
to go to a specific lineG
to go to the end of the file%
jump to matching ({[]})
Select a paragraph and append to end of line
Ctrl + V -> } -> :norm A;
visually select the line, use paragraph movements to select the block, type : This should bring up :'<,'> in the command line
:'<,'> normal A;
Vim Grammar
dw
(delete word)
Combos
d$
Delete till the end of the linec^
Change till the beginning of the linedj
Delete the current line and the one below
Composite motions
t<char>
till character (excluding character)f<char>
find character
Can be combined with a command
dtl
Delete till the lcfe
Change up to (and including) e
Modifiers
--
Where you need more than oned3j
Delete the current line and 3 lines below itd4t)
Delete till the fourth parenthesis
Text objects
-- they represent text conceptsiw/aw
inner/around wordis/as
inner/around sentenceip/ap
inner/around paragraphit/at
inner/around tag
Combining with commands and modifiers
dap
delete around paragraph: delete paragraph including
surrounding white spaced2as
delete around two sentencesci(
change innter parenthesisci"
change inner double quotes
Going full VIM
:e src/**/ju**
to open a file. Fuzzy file search, Tab completionCTRL ^
to jump between open filesCTRL O
jumps back in your historyCTRL I
jumps forward in your history:Ex
to open the file explorer
splitsCTRL + w + v
opens a vertical splitCTRL + w + s
opens a horizontal splitCTRL + w + o
closes all buffers except the current one
To resize the splits:resize 10
- rows:vertical
resize 20CTRL + w
= resizes all splits equallyCTRL + w r
rotates the buffers
:h
find help on a command
{OPERATOR}{COUNT}{MOTION}
d 4 j -> delete 4 lines down
d f ' -> delete everything until first ocurrence of '
d t ' -> same as above but doesn't include the character
d / hello -> delete everything until the first ocurrence of hello
=
formats code
Text objects
To specify a text object (word, sentence, paragraph, [HTML] tags, blocks)
you can either use a
(a text object plus whitespace) or i
(inner object
without whitespace) along with a character that represents a text object
itself
{i|a}{TEXT-OBJECT-ID}
inner | around
{operator}{i|a}{text-object}
Some text object identifiers
w
- words
- sentencep
- paragraphb
( - block surrounded by ()B
{ - block surrounded by {}" '
- quoted textt
- tag
(hello-user)
{delete all words inside}
The .
command repeats the last change
Split layouts
:sp
-> opening the file vertically - opens below.:vs
-> horizontal split - opens to the rightControl+w q
to close each window at a timeCtrl-w
to move between splitsCtrl-w h Ctrl-w ←
Shift focus to split on left of currentCtrl-w l Ctrl-w →
Shift focus to split on right of currentCtrl-w j Ctrl-w ↓
Shift focus to split below the currentCtrl-w k Ctrl-w ↑
Shift focus to split above the current
resize splits
:resize 60 changes the vim info area
:res +5 for incremental resizing
:vertical resize 80
:vertical resize + 5
:Ctrl-w + and Ctrl-w - to resize the height of the current window
:Ctrl-w = to resize all windows to equal dimensions
Block selection / insert at the beginning of a line
- Ctrl + V
- Select lines
- Shift I - places editor in INSERT, press # which will add to the first line.
- Press ESC, wait a second and it will insert in all other lines
beginning that doesn't work, in step three do:
: s/^/#
Misspelled words
]s
go to the next misspelled word[s
go to the last misspelled wordz=
when on a misspelled word, get some suggestionszg
mark as correctzw
mark a good word as incorrect
Combining search with the dot command
/search for word
A[DO NOT PAY][Esc]
n.
The dot command lets us do the last command while we have the search saved.
CSS Autocomplete
start typing the name, press <Ctrl + x> and <Ctrl + o>
autocomplete css namds (ID's, classes, etc)
<Ctrl + n>
Netrw
vim .
opens a file tree
:Ex
opens an explore view
:Vex
vertical explore
Ctrl + w s
opens a split with the same file Ctrl + w v
opens vertically
Ctrl w
goes into windows mode
Ctrl w <h/l/j/k>
navigating between buffers
Ctrl w o
closses all buffers except for the current buffer
:so %
source current file
Marking a file
m <Capital letter>
it allows you to go to another file you can mark that file then:
' <Capital letter>
takes you back to the file that was marked / bookmarked
Using lowercase letters is a file level mark while capital letters are Global
Ctrl 6
Jumps to the previous file
Installing a plug in after sourcing the file and adding the plugin in the vimrc file
:PlugInstall
Macros
they allow you to record movements and replay them.
qa
q
to record followed by a letter
Then you can start recording a series of movements to let the macro do the work.
to play the macro
@a
or as how many times it ought to do it.
10@a
to see what is recorded in the register
:reg
you'll notice they all start with a double quote "
you can record to a register
<Shift>V "by
highlight a line, yanked into the b register
You can edit your macros
to paste in a macro:
"ap
you can now do something to the macro
highlight it all and paste it in the register