Vim Cheat Sheet | ahampriyanshu

Table of Contents

What is Vim ?

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is an improved fork of vi.

Installation

sudo apt install vim      [ Debian/Ubuntu ]
sudo yum install vim      [ RedHat/CentOS ]
sudo pacman -S vim        [ Arch/Manjaro ]
sudo dnf install vim      [ Fedora ]
sudo zypper install vim   [ OpenSUSE ]

Keybindings

Basics

:qExit
:q! / ZQForceful exit
:wq / :x / ZZSave and exit
:qaExit all
:wSave
Esc / Ctrl + [Enter command mode
.Repeat last command
uUndo
Ctrl + rRedo
:n / nGJump to $n^{th}$ line

Movement

jMove cursor down
kMove cursor up
hMove cursor left
lMove cursor right
HMove to top of screen
MMove to middle of sreen
LMove to end of screen
zzMove to center of the screen
ztScroll current line to top of the page
zbScroll current line to bottom of the page
GJump to end of the file
ggJump to top of the file
g_Jump to the last non-blank line of the file
0Jump to the start of the line
^Jump to the first non-blank character of the line
$Jump to the end of the line
%Jump to matching parenthesis
wJump to beginning of the next word
eJump to end of the current word
wJump to beginning of the current word

Editing

iInsert before the cursor
aInsert after the cursor
IInsert at the beginning of the line
AInsert at the end of the line
oInsert new line below
OInsert new line above
rReplace single character
REnter replace mode
biInsert at the beginning of the word
eaInsert at the end of the word
~Toggle case of the current character
g~iwToggle case of the current word
g~$Toggle case of all characters to end of line
g~~Toggle case of the current line

Copy & Paste

yCopy(yank) selected text
yy / YCopy current line
Y$Copy from cursor till end of the end
ywCopy from cursor till the next word
y + i or a + wCopy word under the cursor excluding/including whitespaces
y + t or f + #Copy from cursor till # (excluding/including #)
y + { or }Copy till next/previous block
pPaste after cursor
PPaste before the cursor
gpPaste after the cursor and move to next block
xDelete(cut) single character
dDelete(cut) mark text
DDelete(cut) from cursor till the end of the line
ddDelete(cut) current line
dwDelete(cut) from cursor till the next word
d + i or a + wDelete(cut) word under the cursor excluding/including whitespaces
d + t or f + #Delete(cut) from cursor till # (excluding/including #)
d + { or }Delete(cut) till next/previous block
cDelete(cut) mark text in insert mode
CDelete(cut) from cursor till the end of the line in insert mode
ccDelete(cut) current line in insert mode
cwDelete(cut) from cursor till the next word in insert mode
c + i or a + wDelete(cut) word under the cursor excluding/including whitespaces in insert mode
c + t or f + #Delete(cut) from cursor till # (excluding/including #) in insert mode
c + { or }Delete(cut) till next/previous block in insert mode

Search & Patterns

*Next instance of the word under cursor
#Previous instance of the word under cursor
nNext matching search pattern
NPrevious matching search pattern

Macros

qStart recording macro
qStop recording marcro
@Run recorded macro
@@Rerun last macro

Tabs

:tabeOpen file in a new tab
:tabpGo to previous tab
:tabnGo to next tab
:tabsList all the tabs
:tabfirstGo to the fist tab
:tablastGo to the last tab
:tabcloseClose current tab

Plugins

To install vim plugins, I prefer vim-plug.

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
iwr -useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim |`
    ni $HOME/vimfiles/autoload/plug.vim -Force

Then append the following section to your .vimrc file.

call plug#begin('~/.vim/plugged')

Plug 'user/repo'
OR
Plug 'git URL'

call plug#end()
CommandUsage
:PlugInstallInstalls a plugin
:PlugUninstallUninstalls a plugin
:PlugUpdateUpdates all the plugins
:PlugUpgradeUpdates vim-plug

Some Awesome Plugins

Resources