Post

Vim basic movements for text editing

Text Editing

Mastering Vim is the most efficient way for editing text files. Here are some critical Vim commands that will get you editing text efficiently with Vim.

Basic movement

h l k j character left, right; line up, down

b w word/token left, right

ge e end of word/token left, right

{ } beginning of previous, next paragraph

( ) beginning of previous, next sentence

0 gm beginning, middle of line

^ $ first, last character 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

nH nL line n from start, bottom of window

M middle line of window

% go to closing html tag

Change next

  1. Search for the text: /pattern
  2. Press cgn to change the next occurrence
  3. Type the replacement text
  4. Press
  5. Press . (dot) to repeat for each next occurrence Or use n to skip and . to change

Increment and decrement numbers

In normal mode, typing Ctrl-A will increment the next number, and typing Ctrl-X will decrement the next number.

Insertion & replace → insert mode

i a Insert before, after cursor

I A insert at beginning, end of line

gI insert text in first column

o O open a new line below, above the current line

rc replace character under cursor with c

grc like r, but without affecting layout

R replace characters starting at the cursor

gR like R, but without affecting layout

cm change text of movement command m

cc or S change current line

C change to the end of line

s change one character and insert

~ switch case and advance cursor

g~m switch case of movement command m

gum gUm lowercase, uppercase text of movement m

<m >m shift left, right text of movement m

n<< n>> shift n lines left, right

Deletion

x X delete character under, before cursor

dm delete text of movement command m

dd D delete current line, to the end of line

J gJ join current line with next, without space

:rd↵ delete range r lines

:rdx↵ delete range r lines into register x

Insert mode

^Vc ^Vn insert char c literally, decimal value n

^A insert previously inserted text

^@ same as ^A and stop insert → command mode

^Rx ^R^Rx insert content of register x, literally

^N ^P text completion before, after cursor

^W delete word before cursor

^U delete all inserted character in current line

^D ^T shift left, right one shift width

^Kc1c2 or c1←c2 enter digraph \c1,c2\

^Oc execute c in temporary command mode

^X^E ^X^Y scroll up, down

<esc> or ^[ abandon edition → command mode

Copying

"x use register x for next delete, yank, put

:reg↵ show the content of all registers

:reg x↵ show the content of registers x

ym yank the text of movement command m

yy or Y yank current line into register

p P put register after, before cursor position

]p [p like p, P with indent adjusted

gp gP like p, P leaving cursor after new text

Visual mode

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 { }

v35G Select everything from the cursor up to line 35.

Undoing, repeating & registers

u U undo last command, restore last changed line

. ^R repeat last changes, redo last undo

n. repeat last changes with count replaced by n

qc qC record, append typed characters in register c

q stop recording

@c execute the content of register c

@@ repeat previous @ command

:@c↵ execute register c as an Ex command

:rg/p/c↵ execute Ex command c on range r where pattern p matches

Complex movement

- + line up, down on first non-blank character

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

[[ ]] start of section backward, 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

Marks and motions

mc mark current position with mark c ∈[a..Z]

c C go to mark c in current, C in any file

0..9 go to last exit position

[ ] 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

Key mapping & abbreviations

:map c e↵ map c ↦ e in normal & visual mode

:map! c e↵ map c ↦ e in insert & cmd-line mode

:unmap c↵ :unmap! c↵ remove mapping c

:mk f↵ write current mappings, settings… to file f

:ab c e↵ add abbreviation for c ↦ e

:ab c↵ show abbreviations starting with c

:una c↵ remove abbreviation c

Scrolling & multi-windowing

^E ^Y scroll line up, down

^D ^U scroll half a page up, down

^F ^B scroll page up, down

zt or z↵ set current line at top of window

zz or z. set current line at center of window

zb or z- set current line at bottom of window

zh zl scroll one character to the right, left

zH zL scroll half a screen to the right, left

^Ws or :split↵ split window in two

^Wn or :new↵ create new empty window

^Wo or :on↵ make current window one on screen

^Wj ^Wk move to window below, above

^Ww ^W^W move to window below, above (wrap)

Folding

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

Miscellaneous

:sh↵ :!c↵ start shell, execute command c in shell

K lookup keyword under cursor with man

:make↵ start make, read errors and jump to first

:cn↵ :cp↵ display the next, previous error

:cl↵ :cf↵ list all errors, read errors from file

^L ^G redraw screen, show filename and position

g^G show cursor column, line, and character position

ga show ASCII value of character under cursor

gf open file which filename is under cursor

:redir>f↵ redirect output to file f

:mkview [f] save view configuration [to file f]

:loadview [f] load view configuration [from file f]

^@ ^K ^_ \ Fn ^Fn unmapped keys

This post is licensed under CC BY 4.0 by the author.