---
cid: 218
title: 自用的vim小笔记(1)
slug: 218
date: 2020/02/16 23:13:00
updated: 2020/05/05 16:08:54
status: publish
author: harumonia
categories:
  - 源流清泉
tags:
  - vim
noThumbInfoStyle: default
outdatedNotice: yes
reprint: standard
thumbChoice: default
thumbStyle: default
hidden: false
---

## 前言

随着开发的深入，使用 vim 的频率渐次提升，但是技巧却始终停留在初见阶段（一些技巧虽然学过但是都忘了。。。），极大地影响了开发的效率，所以，再次学习 vim 势在必行。

<!-- more -->

## 1. 基础

| Arguments                    | Description                                                                                                        |
| :--------------------------- | :----------------------------------------------------------------------------------------------------------------- |
| +[num]                       | Open editor with cursor on line "num". If "num" is not specified, the cursor will be on the last line of the file. |
| +/{pat}                      | Open editor with cursor on the first occurrence of {pat}.                                                          |
| -c {command} --cmd {command} | A "ex" command in dowble quotes will be processed against the file specified.                                      |
| -b                           | Binary file mode.                                                                                                  |
| -C -v                        | VI compatibility mode. Loses the more advanced vim features.                                                       |
| -d                           | Diff file mode. Must list all files to perform a diff upon (list 2, 3 or 4 files). Same as vimdiff.                |
| -g                           | GUI gvim mode (if compiled in and available).                                                                      |
| -h --help                    | Print help messages. Also see `vimtutor`                                                                           |
| -i _filename_                | Specify viminfo file. Default is `~/.viminfo`                                                                      |
| -r -L                        | Recovery mode. Used after a crash. The ".swp" file is used. See ":help recovery".                                  |
| -M -R                        | File modifications and write not allowed.                                                                          |
| -n                           | Prohibit ".swp" file generation. Required for special devices of limited space.                                    |
| -x                           | Use encryption when writing files. Will prompt for a crypt key.                                                    |
| --noplugin                   | Skip loading plugins.                                                                                              |
| --version                    | Print vim version.                                                                                                 |

## 2. 常用命令的复习与强化

### 2.1 移动

#### 单词级

- `w` or `W` Move cursor a **word** at a time
- `e` or `E` Move cursor to **end** of word
- `b` or `B` Move cursor **back** a word at a time

> 对于中文和英文，区分单词的界限不同

#### 块级

- `gg` 到文档第一行
- `G` 到文档最后一行
- `0` 到行首（第 1 列）
- `^` 到第一个非空白字符
- `$` 到行尾
- `H` 移动到屏幕顶端
- `M` 移动到屏幕中间
- `L` 移动到屏幕底部
- `Ctrl-d` 向下移动半页
- `Ctrl-u` 向上移动半页
- `Ctrl-f` 向下移动一页
- `Ctrl-b` 向上移动一页
- `:<N>` or `<N>gg` 跳转到第 N 行
- `:+<N>` or `<N>j` 向下跳 N 行
- `:-<N>` or `<N>k` 向上跳 N 行

### 2.2 查找

- `*` 向后查找光标当前所在单词
- `#` 向前查找光标当前所在单词
- `/<search>` 向后查找指定字符串
- `?<search>` 向前查找指定字符串
- `n` 继续查找下一个
- `N` 继续查找上一个

_注意： `n` 和 `N` 是有方向性的，若你之前通过 `*` 查找，则 `n` 会继续向文档尾方向查找，`N`
向文档首方向；反之，若你通过 `#` 查找，则 `n` 指向文档首，`N` 指向文档尾_

### 2.3 插入

- i 当前字符前插入 insert
- a 当前字符后插入 append
- I 行首插入
- A 行尾插入
- o 在下一行插入
- O 在上一行插入

### 2.4 删除

- dd 删除一行并保存到剪切板 delete line (stored in local buffer)
  - 3dd 删除三行
  - dw delete word
  - d) Delete to end of sentence
  - d\$ Delete all characters from cursor to end of line
  - d- Delete current and previous line
  - dfx Delete from cursor to first occurance of the letter "x"
  - d'x Delete from the current line to the line marked with the identifier "x"
  - 'ad'b Delete from the line of mark "a" to the line marked "b".
  - d/cat Delete all characters from the cursor to the next occurance of (but not including) "cat"
- cc 删除一行并保存到剪切板，同时进入 insert 模式 **copy** line (stored in local buffer)
  - 拓展可参照 dd
- s 删除当前字符 **Substitute** one character under cursor continue to insert

#### 2.5 复制

- yy **Yank** (copy) current line into "unnamed" storage buffer.
  - 扩展看参照 dd

#### 2.6 粘贴

- p **Paste** unnamed storage buffer after current line.
- P **Paste** unnamed storage buffer before current line.

#### 2.7 替换

- `r<X>` 将当前字符替换为 X **Replace** character
- `gu<X>` 将指定的文本转换为小写
- `gU<X>` 将指定的文本转换为大写
- `:%s/<search>/<replace>/` 查找 search 内容并替换为 replace 内容

#### 2.8 撤销、重做

- `u` 撤销 **Undo** last change
- U **Undo all changes** to entire line
- `Ctrl-c` 重做

#### 2.9 保存文件

- `:w` 保存当前文件
- `:wa` 保存全部文件
- `:wq` or `ZZ` 保存并退出
- `:q!` or `ZQ` 强制退出，不保存
- `:saveas <new filename>` 文件另存为
- `:w <new filename>` 文件另存一份名为 `<new filename>` 的副本并继续编辑原文件

### 3. 进阶操作

#### 3.1 缩进

- `>>` 向右缩进当前行
- `<<` 向左缩进当前行

#### 3.2 自动排版

- `==` 自动排版当前行
- `gg=G` 当前文档全文自动排版
- `<N>==` 对从当前行开始的 N 行进行自动排版
- `=<N>j` 对当前行以及向下 N 行进行自动排版
- `=<N>k` 对当前行以及向上 N 行进行自动排版

#### 3.3 分屏

- `:split` 缩写 `:sp` or `Ctrl-w s` 上下分屏
- `:vsplit` 缩写 `:vs` or `Ctrl-w v` 左右分屏
- `:diffsplit` 缩写 `:diffs` diff 模式打开一个分屏，后面可以加上 {filename}

##### 窗口跳转

- `Ctrl-w w` 激活下一个窗口
- `Ctrl-w j` 激活下方窗口
- `Ctrl-w k` 激活上方窗口
- `Ctrl-w h` 激活左侧窗口
- `Ctrl-w l` 激活右侧窗口

##### 屏幕缩放

- `Ctrl-w =` 平均窗口尺寸
- `Ctrl-w +` 增加高度
- `Ctrl-w -` 缩减高度
- `Ctrl-w _` 最大高度
- `Ctrl-w >` 增加宽度
- `Ctrl-w <` 缩减宽度
- `Ctrl-w |` 最大宽度

#### 3.4 标签页

##### 创建标签页

- `:tabnew` or `:tabedit` 缩写 `:tabe` 打开新标签页
- `Ctrl-w gf` 在新标签页中打开当前光标所在位置的文件名

_注意：`:tabnew` 和 `:tabedit` 后面都可以跟一个 <空格><文件名> 用以在新标签页中
打开指定文件，还可以在 `:` 后面加一个数字，指出新标签页在列表中的位置（从 0 开始）。_

##### 切换标签页

- `gt` or `:tabnext` 缩写 `:tabn` 下一个标签页（最后一个会循环到第一个）
- `gT` or `:tabprevious` 缩写 `:tabp` 上一个标签页（第一个会循环到最后一个）
- `:tabrewind` 缩写 `:tabr` or `:tabfirst` 缩写 `:tabfir` 到第一个
- `:tablast` 缩写 `:tabl` 到最后一个标签页

##### 关闭标签页

- `:tabclose` 缩写 `:tabc` 关闭当前标签页
- `:-tabc` 关闭上一个标签页
- `:+tabc` 关闭下一个标签页
- `:tabonly` 缩写 `:tabo` 关闭其他标签页

## 4. Other

### 在 vim 中打开其他文件的办法

- **Hyper-Linking to include files:**

  - Place cursor over the file name (i.e. `#include "fileABC.h"`)
  - Enter the letter combination: **gf**
    (go _to_ file)

- **New session:**
  - Use command: "`:e filename`"
    Start new edit session on specified file name without closing current vi / vim editor process.
  - :Explore
    List files in your current direct

### Buffer 的使用

buffer 就是当前 Vim session 的文件历史记录.

- 查看：

  - `:buffers`
  - `:ls`

- 在 Buffer 之间跳转
  - `:bn` 打开缓存中下一个文件
  - `:bp` 打开缓存中上一个文件
  - `:b<N>` 打开缓存中第 N 个文件

> 你也可以使用 `:bdelete<N>` 来删除所要关闭的缓冲区，缩写 `:bd<N>`。

### **g**命令的使用

参考博客[VIm 全局命令 g](http://einverne.github.io/post/2017/10/vim-global.html)

### 重复上一次命令

- `.` 重复执行上一个命令。
