Basic vim movements

Basic vim movements

jk -> down and up
hl -> left and right

wb -> hop forward by a word and back by a word
e -> hop forward to the end of a word
ge -> 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 line
p -> pastes one line below the current line

dd -> deletes that line
cw -> deletes a word and enters insert mode
u -> 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 mode
x  -> deletes a single character

o inserts below that line
O inserts on top
a moves over and inserts
SHIFT + a inserts at the end of a line
SHIFT + P inserts one line above
SHIFT + i inserts at the beginning of a line
SHIFT + d deletes the rest of the line
SHIFT + c deletes the rest of the line and enter insert mode
SHIFT + 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 line
g_ Moves to the non-blank character at the end of a line

Moving vertically

} jumps entire paragraphs downwards
{ similarly but upwards
CTRL-D let's you move down half a page by scrolling the page
CTRL-U let's you move up half a page also by scrolling
gg Goes to the top
G goes to the bottom
diw 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 cursor
gf to jump to a file in an import

gg to go to the top of the file
{line}gg to go to a specific line
G 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 line
c^ Change till the beginning of the line
dj 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 l
cfe Change up to (and including) e

Modifiers

-- Where you need more than one

d3j Delete the current line and 3 lines below it
d4t) Delete till the fourth parenthesis

Text objects

-- they represent text concepts
iw/aw inner/around word
is/as inner/around sentence
ip/ap inner/around paragraph
it/at inner/around tag

Combining with commands and modifiers


dap delete around paragraph: delete paragraph including
surrounding white space
d2as delete around two sentences
ci(  change innter parenthesis
ci" change inner double quotes

Going full VIM

:e src/**/ju** to open a file. Fuzzy file search, Tab completion
CTRL ^ to jump between open files
CTRL O jumps back in your history
CTRL I jumps forward in your history
:Ex to open the file explorer

splits
CTRL + w + v opens a vertical split
CTRL + w + s opens a horizontal split
CTRL + w + o closes all buffers except the current one

To resize the splits
:resize 10 - rows
:vertical resize 20
CTRL + w = resizes all splits equally
CTRL + 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   - word
s   - sentence
p   - paragraph
b ( - block surrounded by ()
B { - block surrounded by {}
" ' - quoted text
t   - 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 right
Control+w q to close each window at a time
Ctrl-w to move between splits
Ctrl-w h Ctrl-w ←   Shift focus to split on left of current
Ctrl-w l Ctrl-w →   Shift focus to split on right of current
Ctrl-w j Ctrl-w ↓   Shift focus to split below the current
Ctrl-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 word
z=          when on a misspelled word, get some suggestions
zg          mark as correct
zw          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