Vim Cheatsheet

Press ESC to return to Normal mode

Movement

h j k l Move left, down, up, right
w b e Move to beginning/end of word
W B E Move by WORDS (ignore punctuation)
0 ^ $ Move to start, first non-blank, end of line
gg G Move to top/bottom of file
{ } Move to previous/next paragraph
( ) Move to previous/next sentence
Ctrl + f/b Page forward/backward
H M L Move to top/middle/bottom of screen

Modes

i I Insert mode / Insert at beginning of line
a A Append after cursor / Append at end of line
o O Open new line below/above
s S Substitute character / Substitute line
v V Visual mode / Line visual mode
Ctrl + v Visual block mode
ESC Return to Normal mode

Editing

x X Delete character under/before cursor
dd Delete entire line
dw d$ d0 Delete word, to end of line, to start of line
cc Change entire line
cw c$ c0 Change word, to end of line, to start of line
yy Copy (yank) line
yw y$ y0 Copy word, to end of line, to start of line
p P Paste after/before cursor
u Ctrl + r Undo / Redo
. (dot) Repeat last command

Search & Replace

/pattern Search forward
?pattern Search backward
n N Next/previous search result
* # Search word under cursor forward/backward
:%s/old/new/g Replace all in file
:%s/old/new/gc Replace with confirmation
:noh Clear search highlighting

File Operations

:w Save file
:w filename Save as filename
:q Quit
:wq Save and quit
:q! Quit without saving
:e filename Open file
:e! Revert to last saved version
:ls List buffers
:b n Switch to buffer n

Windows & Tabs

:sp Split window horizontally
:vsp Split window vertically
Ctrl + w w Switch between windows
Ctrl + w h/j/k/l Navigate windows in specific direction
Ctrl + w q Close current window
:tabnew Open new tab
gt gT Next/previous tab
:tabclose Close current tab

Text Objects

diw ciw yiw Delete/Change/Yank inner word
das cas yas Delete/Change/Yank a sentence
dip cip yip Delete/Change/Yank inner paragraph
da" ca" ya" Delete/Change/Yank around quotes
da( ca( ya( Delete/Change/Yank around parentheses

Marks & Jumps

mx Set mark x (a-z for local, A-Z for global)
`x Jump to mark x (at exact position)
'x Jump to mark x (at beginning of line)
'' Jump to previous position
:marks List all marks

Registers

"ay Yank to register a
"ap Paste from register a
"+y Yank to system clipboard
"+p Paste from system clipboard
:reg Show all registers

Essential Commands

>> << Indent/unindent line
=G Auto-indent entire file
:set number Show line numbers
:set nonumber Hide line numbers
:set relativenumber Relative line numbers (Vim 7.3+)
:set ignorecase Case insensitive search
:set smartcase Smart case search
:set wrap Wrap long lines
:set nowrap Don't wrap lines

Basic .vimrc Configuration

Place this in ~/.vimrc for a clean, basic Vim setup
" Basic Vim Configuration

" Enable essential settings
set number                               " Show line numbers
set relativenumber                       " Relative line numbers (Vim 7.3+)
set tabstop=4                           " Tab width
set shiftwidth=4                        " Indentation width
set expandtab                          " Use spaces instead of tabs
set smartindent                        " Smart indentation
set cindent                            " C-style indentation
set nowrap                             " Don't wrap lines
set mouse=a                            " Enable mouse support
set cursorline                         " Highlight current line
set background=dark                    " Dark background

" Search settings
set ignorecase                         " Case insensitive search
set smartcase                          " Case sensitive if uppercase used
set hlsearch                           " Highlight search results
set incsearch                          " Incremental search

" Split configurations
set splitright                         " Vertical splits to the right
set splitbelow                         " Horizontal splits below

" Backup and undo settings
set backupdir=~/.vim/backup             " Directory for backup files
set directory=~/.vim/tmp                " Directory for swap files
set undodir=~/.vim/undo                 " Directory for undo files
set undofile                            " Enable persistent undo

" Key mappings
let mapleader = "\\"                     " Backslash as leader key

" Save file with Ctrl+S
noremap <C-s> :w<CR>
inoremap <C-s> <Esc>:w<CR>a

" Exit insert mode with jj
inoremap jj <Esc>

" Clear search highlighting with Leader + /
noremap <Leader>/ :nohlsearch<CR>

" Better movement in wrap mode
noremap j gj
noremap k gk

" Visual mode indentation
vnoremap < <gv
vnoremap > >gv

" Set colorscheme (uncomment and install if needed)
" colorscheme desert
" colorscheme darkblue

" Plugin management with vim-plug (optional)
" Install vim-plug first:
" curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
"   https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

"call plug#begin('~/.vim/plugged')
"  Plug 'tpope/vim-surround'
"  Plug 'scrooloose/nerdtree'
"  Plug 'morhetz/gruvbox'
"  Plug 'ctrlpvim/ctrlp.vim'
"call plug#end()

" NERDTree toggle (if installed)
"noremap <Leader>n :NERDTreeToggle<CR>

" CtrlP fuzzy finder (if installed)
"noremap <Leader>p :CtrlP<CR>

" Create directories for backup/undo if they don't exist
if !isdirectory(expand('~/.vim/backup'))
    call mkdir(expand('~/.vim/backup'), 'p')
endif

if !isdirectory(expand('~/.vim/tmp'))
    call mkdir(expand('~/.vim/tmp'), 'p')
endif

if !isdirectory(expand('~/.vim/undo'))
    call mkdir(expand('~/.vim/undo'), 'p')
endif

Plugin Management (vim-plug)

Install vim-plug and add to your .vimrc for plugin management
" Install vim-plug (run once in terminal):
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
  https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

" Then add to .vimrc:
call plug#begin('~/.vim/plugged')

  " Essential plugins
  Plug 'tpope/vim-surround'           " Surround mappings
  Plug 'scrooloose/nerdtree'          " File tree
  Plug 'ctrlpvim/ctrlp.vim'           " Fuzzy file finder
  Plug 'morhetz/gruvbox'              " Colorscheme
  Plug 'vim-airline/vim-airline'      " Status line

call plug#end()

" Install plugins: :PlugInstall
" Update plugins: :PlugUpdate
" Remove plugins: :PlugClean