最好的方法就是在使用中學(xué)習(xí)逞力,即 :help
善茎。
vim 升級(jí)到 9 版本
-
yum list installed | grep -i vim
查看當(dāng)前已安裝的 vim 版本
查看當(dāng)前已安裝的 vim 版本 -
sudo yum remove vim-enhanced vim-common vim-filesystem
移除老的安裝 yum install gcc make ncurses ncurses-devel
- github 下載、編譯、安裝
https://github.com/vim/vim/archive/refs/tags/v9.0.1940.tar.gz
make distclean # if you build Vim before
make
sudo make install
vim --version 即可看到版本為 9 了。
參考
Vim Cheat Sheet:基礎(chǔ)經(jīng)典;
vimdoc.sourceforge:非常完整锥咸;
Book - Practical Vim: Edit Text at the Speed of Thought 2nd Edition
www.vim.org(請(qǐng)翻墻)
Vim Tips Wiki @ vim.wikia.com
vim --help
-
vim -o2
- 上下兩個(gè)窗口 -
vim -O2
- 左右兩個(gè)窗口 - vim 已經(jīng)打開(kāi)文件,切分窗口使用
press <Ctrl>+<w> then press <v>
則會(huì)在右側(cè)開(kāi)窗细移。 - 左右切換(在 vim 中
h
左移搏予,l
右移)
切到左邊:pressing <Ctrl>+<w> and then pressing <h>
切到右邊:pressing <Ctrl>+<w> and then pressing <l>
-
diff -E -Z -b -y --suppress-common-lines old new
左右窗口對(duì)比兩個(gè)文件。如何安裝 diff 見(jiàn) 這里弧轧。
基本
- 按 "i"或"I" 進(jìn)入編輯模式雪侥,編輯后按 "esc" 退出編輯模式,然后按 ":" 進(jìn)入命令行模式精绎,":wq" 保存退出 vim.
- R - 進(jìn)入替換模式速缨,和 i 模式相對(duì)應(yīng);
Enter overtype (replace) mode, where you destructively retype everything until you press ESC; - r - 替換當(dāng)前字符代乃;
- 使用 vim 打開(kāi)文件旬牲,編輯了半天仿粹,要保存時(shí)發(fā)現(xiàn) readonly 打開(kāi)的,如何辦原茅?
Using vim to force edit a file when you opened without permissions
有 sudo 權(quán)限時(shí):Issue:
:w !sudo tee %
. This will write the buffer to tee, a command that receives pipe information and can write to files. And as tee is run with sudo powers, tee can modify the file.`
定位
-
<num>G [enter]
- Go To that line -
: <num>[enter]
- Go To that line -
:set nu
或者: set number
設(shè)置顯示行號(hào)吭历。 -
gg
- go to the top,相當(dāng)于 1 G [enter] -
G
- go the bottom擂橘,相當(dāng)于 0 G [enter] -
0
- 行頭 -
$
- 行尾 -
%
- {} 花括號(hào)對(duì)的查找切換晌区,找對(duì)應(yīng)的花括號(hào)(匹配花括號(hào)); -
<num>h, <num>l
:左右移動(dòng)光標(biāo) num 字符通贞; -
<num>j, <num>k
:下上移動(dòng)光標(biāo) num 行朗若;
Undo and Redo
- u:undo
- Ctrl + r:redo
Copy, cut and paste
y
- yankyy
- yank (copy) a line。<num>yy
則 yank <num> lines昌罩,如 4yy捡偏。y0
,y$
峡迷,yG
- yank to the begin of line, to the end of line,to the end of file.進(jìn)入
v
模式后你虹,選擇绘搞,然后y
;
對(duì)于不太好確定邊界的復(fù)制傅物,v模式非常方便夯辖。p
- put (paste) the clipboard after cursor;
P 則插在當(dāng)前光標(biāo)位置(P 即 shift+p)董饰;x
- delete (cut) current character蒿褂;
<num>x
:刪除光標(biāo)右側(cè)的 num 字符;
<num>X
:刪除光標(biāo)左側(cè)的 num 字符卒暂;d
- 刪除dd
- delete (cut) a line啄栓,5dd - 刪除5行。d0
也祠,d$
昙楚,dgg
,dG
- 刪除到行頭诈嘿、行尾堪旧、文件頭、文件尾奖亚。
*i_0_CTRL-D*:0 CTRL-D Delete all indent in the current line. {Vi: CTRL-D works only when used after autoindent}
<num>dw, <num>db
:光標(biāo)右側(cè)淳梦、左側(cè)刪 num 個(gè) word(/-#=空白分隔,何為 word 不太好講昔字,不常用爆袍;包括 yw, yb 在內(nèi)都不常用);
df
(d f space),這是一個(gè) 自定義分隔符的簡(jiǎn)單做法螃宙;v - visual selection
visual selection, cut-and-paste or copy-and-paste
v, d
v進(jìn)入visual蛮瞄,然后選擇,然后 d 即可刪除谆扎。
v, y
v, p
Working with multiple files
:e filename - Edit a file
:tabe - make a new tab (tabedit)
:r <file> 把文件 file 的內(nèi)容追加到當(dāng)前光標(biāo)行之后挂捅;
gt - go to the next tab
gT - go to the previous tab
ctrl+wv - Split windows verticall(:vsp - vertically split windows)
ctrl+ww - switch between windows
分窗口后,使用 :e 切換文件堂湖。
:split 橫向分割為上下窗口闲先,:close,:10split 則新窗口10行无蜂。
:vsplit 縱向分割為左右窗口
Search
search forwards
/<pattern>
伺糠,Then press n to search forwards for the next occurrence, or N to search backwards, or # to search backwards./\c<pattern>
:search, case insensitive matching(不區(qū)分大小寫(xiě))。/\C<pattern>
區(qū)分大小寫(xiě)搜索斥季;Search backwards
?<pattern>训桶,Pressing n searches in the same direction (backwards), while N searches in the opposite direction (forwards).Searching for the current word
Press*
to search forwards for the next occurrence of that word, or press#
to search backwards.n
next forwards,N
backwards.搜索 only whole words
the pattern begins with\<
and ends with\>
, so only whole words are found.*
和#
就是典型的 word 搜索。
Search and replace 搜索和替換
g
flag
The g flag means global酣倾,即:Replace all matches舵揭,而不是 replace the first occurrence.c
flag
The c flag means asking for confirmation.%
:in all the lines (on each line), 即搜索每一行,而不是僅僅當(dāng)前行躁锡。:%s/foo/bar/gc
Change each 'foo' to 'bar', but ask for confirmation first.
/
分隔符可以改為@
或者#
等字符午绳,但不能是|
字符,這在替換含有/
字符的字符串時(shí)很好映之。
:%s/\<foo\>/bar/gc
Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation.`
-
:3,8 s/foo/bar/g
搜索替換第3到第8行的所有foo并替換為bar拦焚。
v 進(jìn)入 -- VISUAL -- 模式
:'<,'>s/foo/bar/g
v 進(jìn)入 visual 模式,選中區(qū)域后杠输,: 進(jìn)入命令行赎败,輸入s/foo/bar/g
即可。
When compiled with +visual, change each 'foo' to 'bar' for all lines within a visual selection. Vim automatically appends the visual selection range ('<,'>) for any ex command when you select an area and enter :.:'<,'>s/ /#/
visual 選中若干行蠢甲,以 # 注釋若干行螟够,僅將最前面的一個(gè)空格替換為#(replace the first occurrence)。直接在行頭插入 # 則 :'<,'>s/^/#/ 即可峡钓。
縮進(jìn) indent
- In command mode
you can use >> to indent a single line. 4>> will indent the current and next three lines. 可結(jié)合 . 重復(fù)操作妓笙。 - visual mode
先選中若干行,然后 >能岩,可結(jié)合 . 重復(fù)操作寞宫。 - 使用 < 則相反方向。
- Shifting blocks visually
tab 字符轉(zhuǎn) 空格 space
- set expandtab
-
set noexpandtab
:則關(guān)閉轉(zhuǎn)換拉鹃。 -
對(duì)文件中已經(jīng)存在的 tab 字符如何轉(zhuǎn)為空格 Converting tabs to spaces辈赋?
將當(dāng)前文件里已有的 tab 字符全部轉(zhuǎn)為 spaces::retab
鲫忍;
只轉(zhuǎn)當(dāng)前行::.retab
;
~/.vimrc 配置文件
vim 個(gè)性設(shè)置钥屈,里面是各個(gè) vim 命令悟民。The vimrc file contains optional runtime configuration settings to initialize Vim when it starts.
使用 " 標(biāo)識(shí)注釋?zhuān)╟omments),可以使用中文篷就;
set number " display line number
set laststatus=2 " status line: display filename
set ignorecase " Do case insensitive matching
set tabstop=4 " ts
set shiftwidth=4 " sw
set expandtab " tab to spaces,當(dāng)你鍵入tab時(shí)竭业,自動(dòng)轉(zhuǎn)為空格智润。
set cursorline " highlight the cursor line
set hlsearch " highlight search: hlsearch/hls/nohlsearch/nohls
set nowrap
- Syntax highlighting in vim,通常默認(rèn)支持未辆;
:syntax enable
or
:sy enable
or
:syn enable
- c2a0 問(wèn)題窟绷;
在頁(yè)面上出現(xiàn)的代碼,由于 頁(yè)面字符處理轉(zhuǎn)換 的緣故咐柜,經(jīng)常會(huì)出現(xiàn)一些奇奇怪怪的東西兼蜈,比如 c2a0,看起來(lái)也是一個(gè)空格拙友,但實(shí)際上不是(空格 Space 的十六進(jìn)制20)饭尝,網(wǎng)頁(yè)上使用;
奇怪字符可以使用 xxd 或者 hexdump 觀察献宫;錯(cuò)誤提示和語(yǔ)法著色也能看出問(wèn)題所在; - 查看當(dāng)下設(shè)置
:verbose set
:verbose set ts
:verbose set shiftwidth
:verbose set all
- 查看 highlight 設(shè)置
:hi
:hi search
vim 下查看ASCII 碼
:%!xxd 十六進(jìn)制
:%!xxd -b 二進(jìn)制/etc/vimrc
System wide Vim initializations.-
vim
vim.png vim --version