Linux vi/vim
三种模式、够用的按键、查找替换、多文件与常用配置。
Key Points记住模式切换(i 插入、Esc 命令、: 命令行)和十来个按键就能干活;不会用时 :q! 退出。
三种模式
Text
普通模式 ──i / a / o──▶ 插入模式
▲ │
└──────── Esc ─────────┘
│
└──: / / / ?──▶ 命令行模式(:w :q :%s/../..)够用的按键
| Key | Action |
|---|---|
i / a / o | Insert before / after / new line below |
Esc | Back to normal mode |
dd / yy / p | Delete line / copy line / paste |
u / Ctrl+r | Undo / redo |
gg / G / :42 | Top / bottom / line 42 |
/word / n | Search 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 onInside 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.三种模式
Text
普通模式 ──i / a / o──▶ 插入模式
▲ │
└──────── Esc ─────────┘
│
└──: / / / ?──▶ 命令行模式(:w :q :%s/../..)够用的按键
| 按键 | 作用 |
|---|---|
i/a/o | 前插 / 后插 / 下方新行 |
Esc | 回普通模式 |
dd/yy/p | 删行 / 复制行 / 粘贴 |
u/Ctrl+r | 撤销 / 重做 |
gg/G/:42 | 首 / 尾 / 第 42 行 |
/word | 向下搜索 / 下一个 |
:w | 保存 / 退出 / 保存退出 / 强退 |
替换
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在容器里
vi 可能是精简版(vim-tiny),方向键会输出字母。装 vim 或改用 nano,别在紧急排障时和编辑器较劲。