A Developer In The mountains having fun

VIM Documentation

— — The normal commands in vim are all stacking. Most of the commands can be combined to make a single operation.

The stacking is in the form of number action movement ie. 10kw moves 10 words down

Modes

VIM is a modal editor, meaning there are seprate modes of the editor.

input mode — in this mode the editor works like most people would expect, you type and what you type is input on screen (buffer) Go back to the narmal mode with the {ETC} key

  • normal mode (default) — this mode is the default and is used to input most commands and move around. This is the defualt mode because in practice programmers spend more time moving around in the document then typing.

  • command mode : collon enters command mode (you'll see the cursor and line at the bottom)

  • visual mode — v enters visual mode

Saving & Quiting

:w — to save

  • :wa — to save all files

:q — quit

  • :q! — force quit (when you dont want to save changes)

:wq — save & quit

Insert mode

i — start insert mode before the cursor

I — start insert mode at the begining of line (must be in block mode)

a — append after the cursor (and enter insert mode)

A — append at end of line (and enter insert mode)

o — append a new line below current (and enter insert mode)

O — append a new line above the currnt (and enter insert mode)

ea — insert at end of word

{ETC} — exit insert mode

control keys inside Insert Mode

ctl—o — temp leave insert mode for a single command

ctl—w — del word

ctl—t — indent line

ctl—d — unindent line

Movement ( in normal mode )

Basic

j — down

k — up

l — left

h — right

** the arrow keys will normally also work on modern terminals but getting used to the basic keys above will help because only these keys can be combined with other commands.

0 — moves to beginning of line

^ — moves to first non-blank char of the line

Movement by word

e — move to end of word

b — move to start of word (moves left by word)

w — moves forward but leaves cursor at start of word

W — moves forward (can contain punctuation)

Movement by text block

( & ) — sentence

{ & } — paragraph

% — moves to matching bracket, or other inclosing item

Movement by line

G — moves to last line in file

gg — moves to the start of the file

33G — moves to the 33rd line in the file

:33 — moves to the 33rd line in the file

33gg — moves to the 33rd line in the file

` ` — two back-quotes moves to the last position

Movement by char

f{char} — moves inside current line to next occurance of char

F{char} — moves inside current line to previous occurance of char

t{char} — moves inside the current line to after the next occurence of char

T{char} — moves inside the current line to after the previous occurence of char

Screen positioning

{ctl-u} — move screen up by 1/2

{ctl-d} — move screen down by 1/2

{ctl-f} — move screen up one

{ctl-b} — move screen down one

zz — center on the current line

z <enter> — move current line & cursor to top of screen

yanking (copy)

yy — yank the current line

3yy — yank the next 3 lines

y$ — yank from cursor to end of line

y^ — yank from cursor to start of line

yw — (yank word)yank to the start of next word

yiw — (yank inside word)yank the current word

y%{char} — yank to the matching char (),{},[]A

Deleting (cutting)

dd — delete current line

dw — delete word (from cursor to end of word)

daw — delete around word (deletes the word where the cursor is at)

3dd — delete the next 3 lines

d$ — delete to end of line (shift D)

x — delete the char under the cursor

putting (paste)

p — put the yanked text after cursor

P — put the yanked text before the cursor

Various Editing Commands

r — replace single char

J — Join line below the current

cc — replace line

cw — replace to end of word

c$ — replace to end of line

s — delete char and substiture text

xp — transpose two chars (delete and paste)

u — undo

{CTL-r} — redo

. — period — replace last command

Visual Mode

useful for cut,copy paste of blocks of text. Entering mode also sets start of selection point

v — enteres Visual mode

V — enter Visual mode and select by lines

{CTL-v} — enter Visual mode and select by blocks (retangle blocks of text)

> — shift text right

< — shift text left

~ — switch case

o — move to the opposite corner of selection

O — move to the opposite side of selection

it — inside tag selection

at — around tag select the tag and its contents

ii — select by indent level (inside indent)

ai — select by indent level (around indent)

ab — around block with ()

aB — around block with {}

ib — inside block with ()

iB — inside block with {}

aw — around word

~ — toggle case

U — change to uppercase

u — change to lowercase

A — append content at the end of every line selected after the highliged

$A — append content at end of every line

S-char — surround selection with char (requires the vim-surround plugin)

I — insert content at the beginning of every line selected

$ — end of line/s

g_ — last character on line

  • when using this with surround it'll surround the line not including the newline character and is probably what you want

CTL-a — Increment character or number

CTL-x — decrement character or number

yanking

  1. once selection is made (by moving cursor) press y to yank or d to cut

  2. use p or P to paste

Marks (see my marks document for more)

:marks — list all marks

ma — set current position for mark A

`a — jump to mark A

Search

/pattern — search for pattern

?pattern — search backwards for pattern

f{char} — finds next instance of char

F{char} — finds previous instance of char

, / ; — repeat previous/next for f, t, F, T action

n — next result

N — previous result

cgn — after a search this will change the result and change to the next occurance

dgn — deletes the current search result and changes to the next result.

* — search for word under cursor (forward)

# — search for word under cursor (backwards)

bufdo /pattern/ — search in all open files/buffers

[I] — shows lines with matching word under cursor

Search and Replace

all search and replace verbs are in standard REGEX form.

& (normal mode) — repeates last substitution

options

  • g — global
  • i — ignore case
  • c — confirmations

{CTL-R + 0} — paste from yanked register 0 (default)

:s/old/new/<options> — search and replace on current line only

:<length>s/old/new/<options> — search and replace on current line only

for example `:3,10s/old/new/` — will replace old with new on lines 3 - 10.

:%s/old/new/g replace old with new in file

:%s/old/new/gc replace old with new in file with comfirmations

:noh — remove all heighlighting from search

:bufdo %s/old/new/g — search for old in all open files/buffers and replace it with new

magic

\v — very magic (allows to type most chars without escaping)

ie: :%s/\v(emacs/\1 is bad/)

Folds

zC — closes folds recursivily

zE — eliminates all folds

zO — open folds recursivily

zc — close a fold

zd — delete a folds

setting fold method

by default the main fold method is normally "manual", it can also be set to "indent" amoung others.

set foldmethod=indent — to set fold method based on file indentation. I find this actually works well for many languages and requires no setup for languages etc.

Creating and deleting folds

zf — Create a fold. It can be used in VISUAL mode or with a motion.

zd — delete the fold under the cursor (but not the nested ones).

zD — Delete the fold under the cursor, including the nested ones.

zE — Eliminate every fold in the window.

Opening and Closing Folds

zo — open the fold under the cursor

zc — close the fold under the cursor

za — Toggle the fold under the cursor

zx — Undo opened and closed folds.

Uppercase variants of these keystrokes (zO for example) can be used to propagate the action to every nested fold.

Opening and Closing All Folds

zM — Close all folds

zR — Open all folds

zi — Toggle the use of folds (option foldenable).

Moving Through Folds

[z — Move to the start of the current fold.

]z — Move to the end of the current fold.

zj — Move downward to the start of the next fold.

zk — Move upward to the start of the next fold.

Registers

:reg — show registers

"{regName} — access register

ie. "ap — paste register a

ie. "0p — pastes register 0 (last yanked text)

ie. "ayy — yanks a line to register "a"

": is the most recently executed command. If you save the current buffer with :w, “w” will be in this register. A good way to use it is with @:, to execute this command again. For example, if you execute a substitute command in one line, like in :s/foo/bar, you can just to go another line and execute @: to run this substitution again.

Read only Registers

. — last inserted text

% — name of the current file

: — last command line executed

/ — last search

+ — sychronized with system clipboard

* — sychronized with selection clipboard (*nix systems only)

Macros

qw — record a macro (stored by default to register "w)

q — while recording a macro this ends recording.

@w — execute a macro ("w) (can be prceded by a number to play multiples)

Repeat

. — repeat last change

; — repeat last movement

@@ — repeat last @/marco

& — repeat last search

so <file> — read Ex commands from file

so! <file> — read Vim commands from file

g — :[range]g[lobal]/{pattern}/[cmd] execute Ex command [cmd] (default: ":p") on the lines within [range] where {pattern} matches

g NOT pattern — :[range]g[lobal]!/{pattern}/[cmd] execute Ex command [cmd] (default: ":p") on the lines within [range] where {pattern} does NOT match

Other

gd — goto definition

gf — goto file (that the cursor is over)

gx — open the file (that the cursor is over) with default app

gi — goto last insertation & enter Insert mode

Surround (common plugin)

ysiw — yank surround inside word

yss{tag} — yank

Special Characters

:dig — display avilable digraphs

ctl-K char char — insert digraph while in insert mode

ga — display the diagraph that is under the cursor.

commands while in insert mode

ctl-o — execute any normal command then return to insert mode ctl-h — delete one character ctl-w — delete one word ctl-u — delete line

ctl-k — insert digraph ctl-r — insert from register now is the

Don't forget the power of the command line works well with VIM

With VIM its trivial to pipe a buffer to an external command and/or bring in the output. :%!jq . for example takes the whole current buffer and pipes it to the jq command and replaces the buffer with its output.

  • jq is a standard program to format json data.
  • % is the vim shortcut in the command prompt to select the whole current buffer.
  • ! tells vim to bring in the out output of the command

Easy way to get the current date into vim is simple also with r!date, this will put the date where the cursor currently is and the date can be formated with the standard date command parameters if you want.

  • r is VIMs read command.
  • ! tells VIM to run the command.

Run an external filter on the current line

.!

More places to find me
follow me on Mastodon