You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5.0 KiB

This is a reference written by my as I'm using VIM A good reference to check for more would be: This

Vim Reference

This is intended as a brief reference for beginners.

Vim does have a built in reference for each key, that can be accessed via

:h <key>

Basics

Inserting Text

i - Insert Text at the cursor

a - Append Text after the cursor

I - Insert text at the beginning of the line

A - Insert text at the end of the line

o - Insert a new line below the current line

O - Inserts a new line above the current line

Saving and Quitting

esc - Pressing escape will put you in normal mode, then pressing : will put you into command mode

:w - Write to file
:q - Quit file

ZZ - Write and quit (:wq)
ZQ - Force quit (:q!)

Motions for Movement

Motions can also be used with other commands, such as dw to delete to next word or while selecting a visual block.

Vim reference :h motion.txt

h,l - Left, Right
j,k - Down, Up

0 - Jump to the beginning of the line

$ - Jump up the end of the line

gg - Jump to the top of the file

G - Jump to the bottom of the file

:5 - Jump to line 5

{ } - Jump up and down paragraphs

w - Beginning of next word

e - End of next word

b - Beginning of last word

5w - Move forwards 5 words, this also works with most commands

f<character> - Moves to the next occurance of that character, good for quotes

% - Jump to matching parenthesis, either forwards or back

Visual Selection

Visually highlight the selection being made

v - Select characters

V - Select entire lines

Ctrl + v - Visual block selection

Visual mode, can also be used with motions

vi( - Visually select the inside of some parenthesis

va{ - Visual select the the {} and their contents

Deleting and Clipboard

Deleting, yanking, and pasting. Like other commands, can use motions too!

x - Delete/cut character under cursor

d - Delete/cut selection
dd - Delete the line

d5d - Delete 5 lines
d$ - Delete to end of the line

y - Yank/copy selection
yy - Yank the line

p - Put/Paste at cursor
P - Put/Paste after cursor

Editor Readibility(?)

zz - Center the current line in the editor

zt - Align the current line to the top of VIM

zb - Align the current line to the bottom of VIM

Search

/<search> - Search forwards

?<search> - Search backwards

n - Next search result

N - Previous search result

Replace/Change/Substitute

Replace

r<character> - Replaces the whatever is at the cursor with the character

R - Replace mode. This enters an insert mode that writes over everything.

Change

The more powerful alternative of replace

c - Change. This will delete the selection, and put you into insert mode

Yet again, this command can make use of, and gets its power from motions!

cw - Change word
c$ - Change contents to the end of the line
ci( - Change inside ( { " caw - Change around word. Alternative to b cw

Substitute

This is the "change and replace" of vim. It makes use of regex.

:s/x/y - Replace the first instance of x to y on the selected line

:s/x/y/g - Replace globally all instances of x to y on the current line.

:%s/x/y/g - Replace all instances of x to y on each line

:s/x/y/gc - Replace all instances on the line, but prompt for each change

Cool stuff

. - Repeats the last command. Eg. ci( will occur again

Tabs, Buffers, Splits Multitasking

Vim can open numerous files/buffers at the same time.
These can be in tabs, buffers, or in a splitscreen view.

Tabs

Tabs are used like workspaces. To keep tasks associated, from there you have multiple buffers.

:tabedit <file> - Open a file in a new tab :tabe new - Open a blank tab gt - Switch to next tab gT - Switch to previous tab <x>gt - Switch to tab x

CTRL+W T - Breakout current window/split into a new tab CTRL+w gf - Open new tab to file under cursor

Buffers

Buffers are used as file proxies. These are to keep the files accessable.

:r :e filenam :bn :bp :bd :ls - Shows the buffers :b1..9 - Switch to buffer number Ctrl+6 - Switches between the buffers #Ctrl+6 - Switches to the buffer number

bd - Delete current buffer, fails if there are any changes bd! - Delete current buffer, discarding changes

Splits/Windows

Windows are used to compare files, or work on one file while referencing another.

:split <file> - Horizontally split the window :vs - Vertically split the window

Ctrl + ww - Cycle between active splits Ctrl+W hjkl - Cycle between active splits with vimkeys Ctrl + wr - Switch the splits around

Setting variables in Vim

Vim has extra features disabled by default, that can be very useful!

:set lines - This adds line numbers

:syntax on - Turns syntax highlighting on

:set hidden - Allows you to switch between buffers without writing.

These variables can be added into a file called ~/.vimrc to automatically run every time vim opens.