Linux vi/vim

三种模式、够用的按键、查找替换、多文件与常用配置。

Beginner
Key Points记住模式切换(i 插入、Esc 命令、: 命令行)和十来个按键就能干活;不会用时 :q! 退出。

三种模式

Text
普通模式 ──i / a / o──▶ 插入模式
   ▲                      │
   └──────── Esc ─────────┘
   │
   └──: / / / ?──▶ 命令行模式(:w :q :%s/../..)

够用的按键

KeyAction
i / a / oInsert before / after / new line below
EscBack to normal mode
dd / yy / pDelete line / copy line / paste
u / Ctrl+rUndo / redo
gg / G / :42Top / bottom / line 42
/word / nSearch forward / next hit
:w / :q / :wq / :q!Save / quit / save+quit / force quit

替换

Text
:s/old/new/           当前行第一个
:s/old/new/g          当前行全部
:%s/old/new/g         全文全部
:%s/old/new/gc        全文替换并逐个确认
:10,20s/old/new/g     指定行范围

多文件与分屏

Bash
vim a.txt b.txt        # :n / :prev 切换
vim -p a.txt b.txt     # 标签页
:sp / :vsp             # 水平/垂直分屏

常用配置

Text
" ~/.vimrc
set number
set tabstop=4 shiftwidth=4 expandtab
set hlsearch incsearch
set ignorecase smartcase
syntax on
Inside minimal containers vi may be vim-tiny, where arrow keys print letters. Install vim or use nano — do not fight the editor during an incident.

Friends & Partners