|$ curl https://forge-ai.dev/api/markdown?path=docs/linux/vim
$cat docs/vim.md
updated Recently·20 min read·published
vim
Introduction
vim is a modal text editor found on virtually every Unix-like system. Learning the basics lets you edit config files, write code, and fix issues on remote servers where graphical editors are unavailable.
Modes
vim is modal. The same key does different things depending on the current mode.
| Mode | Enter With | Purpose |
|---|---|---|
| Normal | Esc | Navigation and commands |
| Insert | i, a, o | Typing text |
| Visual | v or V | Selecting text |
| Command-line | : | Ex commands like save and quit |
Editing
vim-editing.txt
TEXT
| 1 | i # insert before cursor |
| 2 | a # append after cursor |
| 3 | o # open new line below |
| 4 | O # open new line above |
| 5 | x # delete character under cursor |
| 6 | dd # delete line |
| 7 | yy # yank (copy) line |
| 8 | p # paste after cursor |
| 9 | P # paste before cursor |
| 10 | u # undo |
| 11 | Ctrl + r # redo |
| 12 | r # replace single character |
| 13 | cw # change word |
| 14 | ci" # change inside quotes |
Saving & Quitting
vim-save.txt
TEXT
| 1 | :w # save |
| 2 | :wq # save and quit |
| 3 | :x # save and quit |
| 4 | :q # quit |
| 5 | :q! # quit without saving |
| 6 | :w !sudo tee % # save with sudo |
Search & Replace
vim-search.txt
TEXT
| 1 | /pattern # search forward |
| 2 | ?pattern # search backward |
| 3 | n N # next / previous match |
| 4 | * # # search word under cursor |
| 5 | |
| 6 | :%s/old/new/g # replace all in file |
| 7 | :%s/old/new/gc # replace with confirmation |
| 8 | :5,10s/old/new/g # replace in lines 5-10 |
A Practical .vimrc
.vimrc
VIM
| 1 | " ~/.vimrc |
| 2 | set number " show line numbers |
| 3 | set relativenumber " relative line numbers |
| 4 | set tabstop=2 |
| 5 | set shiftwidth=2 |
| 6 | set expandtab " use spaces instead of tabs |
| 7 | set smartindent |
| 8 | set wrap |
| 9 | set hlsearch " highlight search matches |
| 10 | set incsearch " incremental search |
| 11 | set ignorecase " case-insensitive search |
| 12 | set smartcase " case-sensitive if uppercase used |
| 13 | set clipboard=unnamedplus " use system clipboard |
| 14 | set mouse=a " enable mouse in all modes |
| 15 | syntax on |
| 16 | filetype plugin indent on |
ℹ
info
Start with vimtutor in your terminal for a 30-minute hands-on introduction.