vim - movement within a file

Movement

Moving the cursor gracefully without moving the page:

Basic Movement of the cursor:

command description
h l k j character left, right; line up, down
b w word/token left, right
ge e end of word/token left, right
gm middle of line
nG ngg line n, default the last, first
n% percentage n of the file (n must be provided)
n column n of current line
% match of next brace, bracket, comment, *define. move to the next or previous related item. Items include brackets, opening and closing c-style comments, and C preprocessor conditionals. I find myself using % a lot depending on the programming language.
nH line n from start of window
nL line n from bottom of window
M middle line of window
(minus) move the cursor to the first non-blank character on the previous line
(plus) move the cursor to the first non-blank character on the next line
M/H/L move cursor to middle/top/bottom of screen (Move the cursor only. Do not move the screen)
^ and g_ move the cursor to the first and last non blank characters in a line
0 and $ move the cursor to the first and last characters in the current line
( ) move the cursor to the beginning of previous, next sentence
{ and } Move the cursor to the beginning of previous or next paragraph. The definition of a paragraph is based on nroff macros (read man nroff if you want to figure out what that means). This generally works quite well for navigating between large space-separated blocks in a programming language.

Complex movement of the cursor:

command description
B W space-separated word left, right
gE E end of space-separated word left, right
n_ down n-1 line on first non-blank character
g0 beginning of screen line
g^ g$ first, last character of screen line
gk gj screen line up, down
fc Fc next, previous occurence of character c
tc Tc before next, previous occurence of c
; , repeat last fFtT, in opposite direction
(Two square bracket openings) start of section backward
]] start of section forward
[] ][ end of section backward, forward
[( ]) unclosed (, ) backward, forward
[{ ]} unclosed {, } backward, forward
[m ]m start of backward, forward Java method
[* ]* unclosed *if, *else, *endif backward, forward
[* ]* start, end of * * backward, forward

Scrolling

Scroll up and down through a page gracefully using Vim the following keystrokes:

Move the page using these commands:

command description
zt or z↵ set current line at top of window. move current line to the top of the screen
zz or z. set current line at center of window. move current line to the middle of the screen (Careful with zz, if you happen to have Caps Lock on accidentally, you will save and exit vim)
zb or z- set current line at bottom of window. move current line to the bottom of the screen
zh zl scroll one character to the right, left
zH zL scroll half a screen to the right, left
Ctrl b Moves screen up (backward) one page, cursor to last line
Ctrl f Moves screen down (forward) one page, cursor to first line
Ctrl u Moves cursor & screen up ½ page (Half-page navigations)
Ctrl d Moves cursor & screen down ½ page (Half-page navigations)
Ctrl E move the page/screen up by one line (One-line movements)
Ctrl Y move the page/screen down by one line (One-line movements)
:N Go to line N
N% Go to the line N percent through the file
N(vertical pipe) Go to column N

I lose visual context every time for the former two, so I have developed the bad habit of hitting the latter (Ctrl-Y and Ctrl-E) repetitively. Ctrl-y and Ctrl-e only change the cursor position if it would be moved off screen.

To leave the cursor in the same column when you use Ctrl+D, Ctrl+F, Ctrl+B, Ctrl+U, G, H, M, L, gg you should define the following option:

:set nostartofline

Folding

command description
zfm create fold of movement m
:rfo create fold for range r
zd zE delete fold at cursor, all in window
zo zc zO zC open, close one fold; recursively
[z ]z move to start, end of current open fold
zj zk move down, up to start, end of next fold

Marks

https://vim.fandom.com/wiki/Using_marks

Through out this page - backtick refers to the key that is left of 1 in the numeral row.

Command Description
ma set mark a at current cursor location
‘a jump to line of mark a (first non-blank character in line)
(backtick)a jump to position (line and column) of mark a
d’a delete from current line to line of mark a
d(backtick)a delete from current cursor position to position of mark a
c’a change text from current line to line of mark a
y(backtick)a yank text to unnamed buffer from cursor to position of mark a
:marks list all the current marks
:marks aB list marks a, B

Marks And Motions

COMMAND description
mc mark current position with mark c ∈[a..Z]
(backtick)c (backtick)C go to mark c in current, C in any file
(backtick)0..9 go to last exit position
(backtick)" go to position before jump, at last edit
(backtick)[ (backtick)] go to start, end of previously operated text
:marks↵ print the active marks list
:jumps↵ print the jump list
n^O go to nth older position in jump list
n^I go to nth newer position in jump list

Visual Mode

command description
v V ^V start/stop highlighting characters, lines, block
o exchange cursor position with start of highlighting
gv start highlighting on previous visual area
aw as ap select a word, a sentence, a paragraph
ab aB select a block ( ), a block { }

Links to this note