Vim memo

Vim modes

When you launch the Vim editor, you’re in normal mode.

To type a text, you need to enter the insert mode by pressing the i key. This mode allows you to insert and delete characters in the same way you do in a regular text editor.

To go back to the normal mode from any other mode, just press the Esc key.

Opens a file in Vim / Vi

1
vim file.text

Saves the file without exiting Vim / Vi

  • Press Esc
  • Type :w
  • Press Enter

To save the file under a different name, type :w new_filename and hit Enter.

Saves a file and quits Vim / Vi

:wq

Quits Vim / Vi without saving the file

:q!

Navigating

Moves the cursor within the whole file

Places the cursor at the start of the file

gg

Places the cursor at the end of the file

G

Moves one page forward

Ctrl + f

Moves one page backward

Ctrl + b

Place the cursor at the line 14 (go to line 14)

:14

Moves the cursor within one line

Places the cursor at the beginning of a line

0

Places the cursor at the end of a line

$

Moves the cursor forward to the beginning of a word

w

Move forward three words

3w

Move forward a WORD (any non-whitespace characters)

W

Move backward to the beginning of a word

b

Move backward three words

3b

Move backward a WORD (any non-whitespace characters)

B

Jump to the 42nd column of the current line

42|

Moves the cursor within the screen

Places the cursor at the top of the screen

H

Places the cursor at the middle of the screen

M

Places the cursor at the bottom of the screen

L

Searches for text in the document

Searches for text in the document where keyword is whatever keyword, phrase or string of characters you’re looking for.

/[keyword]

Searches the text again in whatever direction the last search was.

n

Searches your text again in the opposite direction.

N

To turn off highlighting (of text search) until the next search.

:noh

Makes the vi/vim text editor show or hide line numbers

:set number

:set nonumber

delete one line

Press ESC to go to Normal mode, place the cursor on the line you need to delete, press dd

Clear all the document (select all and then delete)

ggdG

Vim 中结构化展示 jsonl

:%!jq .

字符逐个解释

  • : 进入 Vim 的命令模式。在这种模式下,你可以输入和执行 Vim 的命令。
  • % 选中整个缓冲区。在 Vim 中,% 是一个范围选定符,表示从文件的第一行到最后一行。
  • ! 过滤器操作符,表示将当前选定的文本通过指定的外部命令进行过滤处理。
  • jq 外部命令的名称。在此处,jq 是一个轻量级且功能强大的命令行 JSON 处理工具。
  • . jq 的过滤表达式。. 代表当前对象或数据流中的顶层元素。

用 vim 比较两个文件的不同

1
vim -d file1 file2

在 vim 的比较模式中:

  • ]c 跳到下一个差异(不用按 esc:
  • [c 跳到上一个差异(不用按 esc:

Turn off auto indent when pasting text into vim (vim中粘贴需要用)

To turn off autoindent when you paste code, there’s a special “paste” mode:

1
:set paste

Then paste your code. Note that the text in the tooltip now says -- INSERT (paste) --.

After you pasted your code, turn off the paste-mode, so that auto-indenting when you type works correctly again.

1
:set nopaste