一、什么是Vim?
Vim 是一個歷史悠久的文本編輯器,可以追溯到 qed。 Bram Moolenaar 于 1991 年發布初始版本。Vim 有著悠久的歷史;它起源于 Vi 編輯器(1976 年),至今仍在開發中。(Vim has a rich history; it originated from the Vi editor (1976), and it’s still being developed today.)?
編程時,您大部分時間都花在閱讀/編輯上,而不是寫作上。出于這個原因,Vim 是一個模態編輯器:它有不同的插入文本和操作文本的模式。Vim 是可編程的(使用 Vimscript 和 Python 等其他語言),而 Vim 的界面本身就是一種編程語言:擊鍵(帶有助記詞名稱)是命令,這些命令是可組合的。Vim 避免使用鼠標,因為它太慢了;Vim 甚至避免使用箭頭鍵,因為它需要太多的移動。 最終結果是一個可以匹配您思維速度的編輯器。?
(When programming, you spend most of your time reading/editing, not writing. For this reason, Vim is a?modal?editor: it has different modes for inserting text vs manipulating text. Vim is programmable (with Vimscript and also other languages like Python), and Vim’s interface itself is a programming language: keystrokes (with mnemonic names) are commands, and these commands are composable. Vim avoids the use of the mouse, because it’s too slow; Vim even avoids using the arrow keys because it requires too much movement.
The end result is an editor that can match the speed at which you think.)
二、Vim安裝
①在VScode上的擴展插件中有vim的插件,我們選擇install即可。
②Ubuntu虛擬中,打開終端terminal,鍵入
sudo apt install vim
這里我已經下載完成。(可以用vim -version檢驗一下)
三、vim的編輯模式
- Normal: for moving around a file and making edits
- Insert: for inserting text
- Replace: for replacing text
- Visual?(plain, line, or block): for selecting blocks of text
- Command-line: for running a command
擊鍵在不同的操作模式下有不同的含義。例如,在“插入”模式下,字母 x 只會插入一個文字字符“x”,但在“正常”模式下,它將刪除光標下的字符,而在“可視”模式下,它將刪除所選內容。
您可以通過按<ESC>(轉義鍵)從任何模式切換回正常模式來更改模式。從“正常模式”中,使用 i 輸入插入模式,使用 R 輸入替換模式,使用 v 輸入可視模式,使用 V 輸入可視線模式,使用 (<C-v>Ctrl-V,有時也寫入 ^V)進入可視塊模式,使用 : 輸入命令行模式。?
四、vim基礎?
4.1、Inserting text
從正常模式,按 i 進入插入模式。現在,Vim 的行為與任何其他文本編輯器一樣,直到您按<ESC>下返回正常模式。這些,以及上面解釋的基礎知識,就是你開始使用 Vim 編輯文件所需要的一切(盡管不是特別有效,如果你把所有時間都花在插入模式下編輯)。
現在是normal模式:
按i后進入插入模式:
4.2、Buffers, tabs, and windows
Vim 維護著一組打開的文件,稱為 “buffers”。一個 Vim 會話有許多選項卡,每個選項卡都有多個窗口(拆分窗格)。每個窗口顯示一個緩沖區。與您熟悉的其他程序(如 Web 瀏覽器)不同,緩沖區和 Windows 之間沒有一一對應關系;窗戶只是視圖。給定的緩沖區可以在多個窗口中打開,甚至在同一選項卡中也是如此。例如,這非常方便,可以同時查看文件的兩個不同部分。
默認情況下,Vim 打開時只有一個選項卡,其中包含一個窗口。?
(Vim maintains a set of open files, called “buffers”. A Vim session has a number of tabs, each of which has a number of windows (split panes). Each window shows a single buffer. Unlike other programs you are familiar with, like web browsers, there is not a 1-to-1 correspondence between buffers and windows; windows are merely views. A given buffer may be open in?multiple?windows, even within the same tab. This can be quite handy, for example, to view two different parts of a file at the same time.
By default, Vim opens with a single tab, which contains a single window.)
Vim 是一個文本編輯器。每次文本都是作為緩沖區的一部分顯示的。每一份文件都是在他們自己獨有的緩沖區打開的,插件顯示的內容也在它們自己的緩沖區中。
緩沖區有很多屬性,比如這個緩沖區的內容是否可以修改,或者這個緩沖區是否和文件相關聯,是否需要同步保存到磁盤上。
窗口 是緩沖區上一層的視窗。如果你想同時查看幾個文件或者查看同一文件的不同位置,那樣你會需要窗口。
請別把他們叫做 分屏 。你可以把一個窗口分割成兩個,但是這并沒有讓這兩個窗口完全 分離 。
窗口可以水平或者豎直分割并且現有窗口的高度和寬度都是可以被調節設置的,因此,如果你需要多種窗口布局,請考慮使用標簽。
標簽頁 (標簽)是窗口的集合。因此當你想使用多種窗口布局時候請使用標簽。
簡單的說,如果你啟動 Vim 的時候沒有附帶任何參數,你會得到一個包含著一個呈現一個緩沖區的窗口的標簽。
順帶提一下,緩沖區列表是全局可見的,你可以在任何標簽中訪問任何一個緩沖區。
4.3、Command-line
Command mode can be entered by typing?:
?in Normal mode. Your cursor will jump to the command line at the bottom of the screen upon pressing?:
. This mode has many functionalities, including opening, saving, and closing files, and?quitting Vim.
在正常模式下,可以通過鍵入":"來進入命令模式。按":",您的光標將跳轉到屏幕底部的命令行。此模式具有許多功能,包括打開、保存和關閉文件,以及退出 Vim。
:q
?quit (close window) - 退出(關閉窗口):w
?save (“write”) - 保存("寫入"):wq
?save and quit - 保存并退出:e {name of file}
?open file for editing - {文件名}打開文件進行編輯:ls
?show open buffers - 顯示打開的緩沖區:help {topic}
?open help:help :w
?opens help for the?:w
?command:help w
?opens help for the?w
?movement
五、vim的界面是一種編程語言
Vim 中最重要的思想是 Vim 的界面本身就是一種編程語言。擊鍵(帶有助記詞名稱)是命令,這些命令組成。這樣可以進行有效的移動和編輯,尤其是當命令成為肌肉記憶時。
5.1、Movement
- Basic movement:?
hjkl
?(left, down, up, right) - Words:?
w
?(next word),?b
?(beginning of word),?e
?(end of word) - Lines:?
0
?(beginning of line),?^
?(first non-blank character),?$
?(end of line) - Screen:?
H
?(top of screen),?M
?(middle of screen),?L
?(bottom of screen) - Scroll:?
Ctrl-u
?(up),?Ctrl-d
?(down) - File:?
gg
?(beginning of file),?G
?(end of file) - Line numbers:?
:{number}<CR>
?or?{number}G
?(line {number}) - Misc:?
%
?(corresponding item) - Find:?
f{character}
,?t{character}
,?F{character}
,?T{character}
- find/to forward/backward {character} on the current line
,
?/?;
?for navigating matches
- Search:?
/{regex}
,?n
?/?N
?for navigating matches
5.2、Selection
Visual modes:
- Visual:?
v
- Visual Line:?
V
- Visual Block:?
Ctrl-v
5.3、
Edits
i
?enter Insert mode- but for manipulating/deleting text, want to use something more than backspace
o
?/?O
?insert line below / aboved{motion}
?delete {motion}- e.g.?
dw
?is delete word,?d$
?is delete to end of line,?d0
?is delete to beginning of line
- e.g.?
c{motion}
?change {motion}- e.g.?
cw
?is change word - like?
d{motion}
?followed by?i
- e.g.?
x
?delete character (equal do?dl
)s
?substitute character (equal to?cl
)- Visual mode + manipulation
- select text,?
d
?to delete it or?c
?to change it
- select text,?
u
?to undo,?<C-r>
?to redoy
?to copy / “yank” (some other commands like?d
?also copy)p
?to paste- Lots more to learn: e.g.?
~
?flips the case of a character
5.4、Counts
您可以將名詞和動詞與計數組合在一起,計數將多次執行給定的動作。
3w
?move 3 words forward5j
?move 5 lines down7dw
?delete 7 words
5.5、Modifiers
您可以使用修飾符來更改名詞的含義。一些修飾詞是 i,意思是“內部”或“內部”,以及 a,意思是“周圍”。(You can use modifiers to change the meaning of a noun. Some modifiers are?i
, which means “inner” or “inside”, and?a
, which means “around”.)
ci(
?change the contents inside the current pair of parentheses -?更改當前括號對內的內容ci[
?change the contents inside the current pair of square brackets -?更改當前方括號對內的內容da'
?delete a single-quoted string, including the surrounding single quotes -?刪除單引號字符串,包括周圍的單引號
六、Advanced Vim
Here are a few examples to show you the power of the editor. We can’t teach you all of these kinds of things, but you’ll learn them as you go. A good heuristic: whenever you’re using your editor and you think “there must be a better way of doing this”, there probably is: look it up online.
6.1、Search and replace
:s
?(substitute) command (documentation).
%s/foo/bar/g
- replace foo with bar globally in file(全局替換)
%s/\[.*\](\(.*\))/\1/g
- replace named Markdown links with plain URLs
6.2、Multiple windows
:sp
?/?:vsp
?to split windows- Can have multiple views of the same buffer.
6.3、Macros(宏)
你可以在 Vim 中錄制一系列按鍵,并把他們存儲到寄存器中。對于一些需要臨時使用多次的一系列操作,把它們作為宏保存起來會顯著地提升效率。對于一些復雜的操作,建議使用 Vim 腳本來實現。
q{character}
?to start recording a macro in register?{character} -?開始記錄寄存器 {character} 中的宏
q
?to stop recording@{character}
?replays the macro- Macro execution stops on error
{number}@{character}
?executes a macro {number} times -?執行宏 {number} 次- Macros can be recursive
- first clear the macro with?
q{character}q -?首先用 q{character}q 清除宏
- record the macro, with?
@{character}
?to invoke the macro recursively (will be a no-op until recording is complete) -?記錄宏,使用 @{character} 遞歸調用宏(在錄制完成之前將是一個無操作)
- first clear the macro with?
- Example: convert xml to json (file)
- Array of objects with keys “name” / “email”
- Use a Python program?
- Use sed / regexes
g/people/d
%s/<person>/{/g
%s/<name>\(.*\)<\/name>/"name": "\1",/g
- …
- Vim commands / macros
Gdd
,?ggdd
?delete first and last lines -?刪除第一行和最后一行- Macro to format a single element (register?
e
)- Go to line with?
<name>
qe^r"f>s": "<ESC>f<C"<ESC>q
- Go to line with?
- Macro to format a person
- Go to line with?
<person>
qpS{<ESC>j@eA,<ESC>j@ejS},<ESC>q
- Go to line with?
- Macro to format a person and go to the next person
- Go to line with?
<person>
qq@pjq
- Go to line with?
- Execute macro until end of file
999@q
- Manually remove last?
,
?and add?[
?and?]
?delimiters
七、Resources
vimtutor
?is a tutorial that comes installed with Vim - if Vim is installed, you should be able to run?vimtutor
?from your shell- Vim Adventures?is a game to learn Vim
- Vim Tips Wiki
- Vim Advent Calendar?has various Vim tips
- Vim Golf?is?code golf, but where the programming language is Vim’s UI
- Vi/Vim Stack Exchange
- Vim Screencasts
- Practical Vim?(book)
八、Exercises
- Complete?
vimtutor
. Note: it looks best in a?80x24?(80 columns by 24 lines) terminal window. - Download our?basic vimrc?and save it to?
~/.vimrc
. Read through the well-commented file (using Vim!), and observe how Vim looks and behaves slightly differently with the new config. - Install and configure a plugin:?ctrlp.vim.
- Create the plugins directory with?
mkdir -p ~/.vim/pack/vendor/start
- Download the plugin:?
cd ~/.vim/pack/vendor/start; git clone https://github.com/ctrlpvim/ctrlp.vim
- Read the?documentation?for the plugin. Try using CtrlP to locate a file by navigating to a project directory, opening Vim, and using the Vim command-line to start?
:CtrlP
. - Customize CtrlP by adding?configuration?to your?
~/.vimrc
?to open CtrlP by pressing Ctrl-P.
- Create the plugins directory with?
- To practice using Vim, re-do the?Demo?from lecture on your own machine.
- Use Vim for?all?your text editing for the next month. Whenever something seems inefficient, or when you think “there must be a better way”, try Googling it, there probably is. If you get stuck, come to office hours or send us an email.
- Configure your other tools to use Vim bindings (see instructions above).
- Further customize your?
~/.vimrc
?and install more plugins. - (Advanced) Convert XML to JSON (example file) using Vim macros. Try to do this on your own, but you can look at the?macros?section above if you get stuck.