- 工信部備案號 滇ICP備05000110號-1
- 滇公安備案 滇53010302000111
- 增值電信業務經營許可證 B1.B2-20181647、滇B1.B2-20190004
- 云南互聯網協會理事單位
- 安全聯盟認證網站身份V標記
- 域名注冊服務機構許可:滇D3-20230001
- 代理域名注冊服務機構:新網數碼
基礎
:e filename | Open filename for edition |
:w | Save file |
:q | Exit Vim |
:q! | Quit without saving |
:x | Write file (if changes has been made) and exit |
:sav filename | Saves file as filename |
. | Repeats the last change made in normal mode |
5. | Repeats 5 times the last change made in normal mode |
在文件中移動
k or Up Arrow | move the cursor up one line |
j or Down Arrow | move the cursor down one line |
e | move the cursor to the end of the word |
b | move the cursor to the begining of the word |
0 | move the cursor to the begining of the line |
G | move the cursor to the end of the line |
gg | move the cursor to the begining of the file |
L | move the cursor to the end of the file |
:59 | move cursor to line 59. Replace 59 by the desired line number. |
20| | move cursor to column 20. |
% | Move cursor to matching parenthesis |
[[ | Jump to function start |
[{ | Jump to block start |
剪切、復制和粘貼
y | Copy the selected text to clipboard |
p | Paste clipboard contents |
dd | Cut current line |
yy | Copy current line |
y$ | Copy to end of line |
D | Cut to end of line |
搜索
/word | Search word from top to bottom |
?word | Search word from bottom to top |
* | Search the word under cursor |
/cstring | Search STRING or string, case insensitive |
/jo[ha]n | Search john or joan |
/< the | Search the, theatre or then |
/the> | Search the or breathe |
/< the> | Search the |
/< ?.> | Search all words of 4 letters |
// | Search fred but not alfred or frederick |
/fred|joe | Search fred or joe |
/<dddd> | Search exactly 4 digits |
/^ {3} | Find 3 empty lines |
:bufdo /searchstr/ | Search in all open files |
bufdo %s/something/somethingelse/g | Search something in all the open buffers and replace it with somethingelse |
替換
:%s/old/new/g | replace all occurences of old by new in file |
:%s/onward/forward/gi | replace onward by forward, case unsensitive |
:%s/old/new/gc | replace all occurences with confirmation |
:2,35s/old/new/g | replace all occurences between lines 2 and 35 |
:5,$s/old/new/g | replace all occurences from line 5 to EOF |
:%s/^/hello/g | replace the begining of each line by hello |
:%s/$/Harry/g | replace the end of each line by Harry |
:%s/onward/forward/gi | Replace onward by forward, case unsensitive |
:%s/ *$//g | delete all white spaces |
:g/string/d | delete all lines containing string |
:v/string/d | delete all lines containing which didn’t contain string |
:s/Bill/Steve/ | replace the first occurence of Bill by Steve in current line |
:s/Bill/Steve/g | Replace Bill by Steve in current line |
:%s/Bill/Steve/g | Replace Bill by Steve in all the file |
:%s/^M//g | delete DOS carriage returns (^M) |
:%s/ / /g | Transform DOS carriage returns in returns |
:%s#<[^>]+>##g | delete HTML tags but keeps text |
:%s/^(.*) 1$/1/ | delete lines which appears twice |
Ctrl+a | Increment number under the cursor |
Ctrl+x | Decrement number under cursor |
ggVGg? | Change text to Rot13 |
大小寫
Vu | Lowercase line |
VU | Uppercase line |
g~~ | Invert case |
vEU | Switch word to uppercase |
vE~ | Modify word case |
ggguG | Set all text to lowercase |
gggUG | Set all text to uppercase |
:set ignorecase | Ignore case in searches |
:set smartcase | Ignore case in searches excepted if an uppercase letter is used |
:%s/<./u&/g | Sets first letter of each word to uppercase |
:%s/<./l&/g | Sets first letter of each word to lowercase |
:%s/.*/u& | Sets first letter of each line to uppercase |
:%s/.*/l& | Sets first letter of each line to lowercase |
讀寫文件
:1,10 w outfile | Saves lines 1 to 10 in outfile |
:1,10 w >> outfile | Appends lines 1 to 10 to outfile |
:r infile | Insert the content of infile |
:23r infile | Insert the content of infile under line 23 |
文件瀏覽器
:e . | Open integrated file explorer |
:Sex | Split window and open integrated file explorer |
:Sex! | Same as :Sex but split window vertically |
:browse e | Graphical file explorer |
:ls | List buffers |
:cd .. | Move to parent directory |
:args | List files |
:args *.php | Open file list |
:grep expression *.php | Returns a list of .php files contening expression |
gf | Open file name under cursor |
和 Unix 系統交互
:!pwd | Execute the pwd unix command, then returns to Vi |
!!pwd | Execute the pwd unix command and insert output in file |
:sh | Temporary returns to Unix |
$exit | Retourns to Vi |
對齊
:%!fmt | Align all lines |
!}fmt | Align all lines at the current position |
5!!fmt | Align the next 5 lines |
Tabs/Windows
:tabnew | Creates a new tab |
gt | Show next tab |
:tabfirst | Show first tab |
:tablast | Show last tab |
:tabm n(position) | Rearrange tabs |
:tabdo %s/foo/bar/g | Execute a command in all tabs |
:tab ball | Puts all open files in tabs |
:new abc.txt | Edit abc.txt in new window |
分屏顯示
:e filename | Edit filename in current window |
:split filename | Split the window and open filename |
ctrl-w up arrow | Puts cursor in top window |
ctrl-w ctrl-w | Puts cursor in next window |
ctrl-w_ | Maximize current window vertically |
ctrl-w| | Maximize current window horizontally |
ctrl-w= | Gives the same size to all windows |
10 ctrl-w+ | Add 10 lines to current window |
:vsplit file | Split window vertically |
:sview file | Same as :split in readonly mode |
:hide | Close current window |
:?nly | Close all windows, excepted current |
:b 2 | Open #2 in this window |
自動完成
Ctrl+n Ctrl+p (in insert mode) | Complete word |
Ctrl+x Ctrl+l | Complete line |
:set dictionary=dict | Define dict as a dictionnary |
Ctrl+x Ctrl+k | Complete with dictionnary |
Marks
m {a-z} | Marks current position as {a-z} |
' {a-z} | Move to position {a-z} |
'' | Move to previous position |
縮寫
:ab mail mail@provider.org | Define mail as abbreviation of mail@provider.org |
文本縮進
:set autoindent | Turn on auto-indent |
:set smartindent | Turn on intelligent auto-indent |
:set shiftwidth=4 | Defines 4 spaces as indent size |
ctrl-t, ctrl-d | Indent/un-indent in insert mode |
>> | Indent |
<< | Un-indent |
=% | Indent the code between parenthesis |
1GVG= | Indent the whole file |
語法高亮
:syntax on | Turn on syntax highlighting |
:syntax off | Turn off syntax highlighting |
:set syntax=perl | Force syntax highlighting |
三.命令行模式操作
1.進入插入模式
按“i”:從光標當前位置開始輸入文件。
按“I”:在光標所在行的行首插入。
按“a”:從目前光標所在位置的下一個位置開始輸入文字。
按“A”:在光標所在行的行末插入。
按“o”:在下面插入一行
按“O”:在上面插入一行。
按“s”:刪除光標后的一個字符,然后進入插入模式。
按“S”:刪除光標所在的行,然后進入插入模式。
3.移動光標
“h”、“j”、“k”、“l”分別控制光標左、下、上、右移一格。
“w”:光標跳到下個字的開頭。
“e”:光標跳到下個字的字尾。
“b”:光標回到上個字的開頭。
“nl”:光標移動該行的第n個位置,例如:“5l”表示移動到該行的第5個字符。
“ctrl+b”:屏幕往后移動一頁。
“ctrl+f”:屏幕往前移動一頁。
“ctrl+u”:屏幕往后移動半頁。
“ctrl+d”:屏幕往前移動半頁。
—數字“0”:移動到文本的開頭。
“G”:移動到文件的最后。
—“$”:移動到光標所在行的行尾。
“^”:移動到光標所在行的行首。
4.刪除文字
“x”:每按一次,刪除光?所在位置的后面一個字符。
“nx”:例如:“6x”表示刪除光標所在位置后面6個字符。
“X”:每按一次,刪除光標所在位置的前面一個字符。
“dd”:刪除光標所在行。
“ndd”:從光標所在行開始刪除n行。例如:“4dd”表示刪除從光標所在行開始的4行字符。
5.復制
“yw”:將光標所在之處到字尾的字符復制到緩沖區中。
“nyw”:復制n個字到緩沖區。
“yy”:復制光標所在行到緩沖區。
“nyy”:例如:“6yy”表示復制從光標所在行開始6行字符。
“p”:將緩沖區內的字符寫到光標所在位置。
8.更改
“cw”:更改光標所在處的字到字尾處。
“cnw”:例如:“c3w”表示更改3個字。
9.跳至指定的行
“ctrl+g”:列出光標所在行的行號。
“nG”:例如:“15G”,表示移動光標到該文件的第15行行首。
其他:
“ZZ”:存盤退出
“ZQ”:不存盤退出
“r”:替換光標所在處的字符。
“R”:替換光標所到處的字符,直到按下“ESC”鍵為止。
“u”:撤銷上一次操作
四.末行模式操作
在使用末行模式之前,請記住先按“ESC”鍵確定已經處于命令行模式后,再按冒號“:”即可進入末行模式。
“set nu”:列出行號
“set nonu”:取消列出行號
“set ic”:搜索時忽略大小寫
“set noic”:取消在搜索時忽略大小寫。
“n”:跳到文件中的某一行,“n”表示一個數字,如輸入數字15,再回車就會跳到文本的第15行。
“!cmd”:運行shell命令cmd。
查找字符:
“/關鍵字”:先按“/”,再輸入想查找的字符,如果第一次查找的關鍵字不是想要的,可?一直按“n”,往后查找一個關鍵字。
“? 關鍵字”:先按“?”鍵,再輸入想查找的字符,如果第一次查找的關鍵字不是想要的,可以一直按“?”,往后查找一個關鍵字。
替換字符:
“s /SPARCH/replace/g”:把當前光標所處的行中的SEARCH單詞替換成REPLACE,并把所有SEARCH高亮顯示。
“%s /SPARCH/REPLACE”:把文檔中所有SEARCH替換成REPLACE。
“n1,n2 s /SPARCH/replace/g”:n1、n2表示數字,表示從n1行到n2行,把SEARCH替換成REPLACE。
五.命令行內容說明
命令行模式:移動光標的方法 | |
h或向左方向鍵(←) | 光標向左移動一個字符 |
j或向下方向鍵(↓) | 光標向下移動一個字符 |
k或向上方向鍵(↑) | 光標向上移動一個字符 |
l或向右方向鍵(→) | 光標向右移動一個字符 |
如果想要進行多次移動的話,例如;向下移動30行,可以使用“30j”或“30↓”的組合鍵,即加上想要進行的次數(數字)后,操作即可。 | |
[Ctrl]+[f] | 屏幕“向下”移動一頁,相當于[Page Down]按鍵 |
[Ctrl]+[b] | 屏幕“向上”移動一頁,相當于[Page Up]按鍵 |
[Ctrl]+[d] | 屏幕“向下”移動半頁 |
[Ctrl]+[u] | 屏幕“向上”移動半頁 |
命令行模式:移動光標的方法 | |
+ | 光標移動到非空格符的下一行 |
- | 光標移動到非空格符的上一行 |
n<space> | n表示“數字”,例如20.按下數字后再按空格鍵,光標會向右移動這一行n個字符。例如20<space>則光標會向后移動20個字符距離 |
0 | 這是數字“0”:移動到這一行的最前面字符處(常用) |
$ | 移動到這一行的最后面字符處(常用) |
H | 光標移動到這個屏幕的最上方哪一行 |
M | 光標移動到這個屏幕的中央哪一行 |
L | 光標移動到這個屏幕的最下方哪一行 |
G | 移動到這個文件的最后一行(常用) |
nG | n為?字。移動到這個文件的第n行。例如20G則會移動到這個文件的第20行(可配合:set nu) |
gg | 移動到這個文件的第一行,相當于1G(常用) |
n<Enter> | n為數字。光標向下移動n行(常用) |
命令行模式:搜索與替換 | |
/word | 從光標位置開始,向下尋找一個名為word的字符串。例如要在文件內搜索vbird這個字符串,就輸入/vbird即可(常用) |
?word | 從光標位置開始,向上尋找一個名為word的字符串 |
n | n是英文按鍵。表示“重復前一個搜索的動作”。舉例來說,如果剛剛執行/vbird去向下搜索vbird字符串,則按下n后,會向下繼續搜索下?個名稱為vbird的字符串。如果是執行?vbird的話,那么按下n,則會向上繼續搜索名稱為vbird的字符串 |
N | 這個N是英文按鍵。與n剛好相反,為“反向”進行前一個搜索操作。例如/vbird后,按下N則表示“向上”搜索vbird |
命令行模式:搜索與替換 | |
:n1、n2s/word1/word2/g | n1與n2為數字。在第n1與n2行之間尋找word1這個字符串,并將該字符串替換為word2。舉例來說,在100到200行之間搜索vbird并替換為VBIRD則:“:100、200s/vbird/VBIRD/g”(常用) |
:1、$s/word1/word2/g | 從第一行到最后一行尋找word1字符串,并將該字符串替換為word2(常用) |
:1、$s/word1/word2/gc | 從第一行到最后一行尋找word1字符串,并將該字符串替換為word2。且在替換前顯示提示符給用戶確認(conform)是否需要替換(常用) |
命令行模式:刪除、復制與粘貼 | |
p,P | p為將已復制的數據粘貼到光標的下一行,P則為貼在光標上一行。舉例來說,當前光標在第20行,且已經復制了10行數據。則按下p后,那10行數據會粘在原來的20行之后,?由21行開始貼。但如果是按下P,那么原來的第20行會被變成30行(常用) |
J | 將光標所在行與下一列的數據結合成同一行 |
c | 重復刪除多個數據,例如向下刪除10行,[10cj] |
u | 復原前一個操作(常用) |
[Ctrl]+r | 重做上一個操作(常用) |
U與[Ctrl]+r是很常用的命令。一個是復原,另一個則是重做一次。利用這兩個功能按鍵,編輯起來就得心應手。 | |
命令行模式:刪除、復制與粘貼 | |
. | 這就是不數點。意思是重復前一個動作。如果想重復刪除、重復粘貼,按下小數點“.”就可以(常用) |
插入模式 | |
i、I | 插入:在當前光標所在處插入輸入文字,已存在的文字會向后退;其中,i為“從當前光標所在處插入”,I為“在當前所在行的第一個非空格符處開始插入”(常用) |
a、A | a為“從當前光標所在的下一個字符處開始插入”,A為“從光標所在行的最?一個字符處開始插入”(常用) |
o、O | 這是英文字母o的大小寫。o為“在當前光標所在的下一行處插入新的一行”,O為“在當前光標所在處的上一行插入新的一行”(常用) |
r、R | 替換:r會替換光標所在的那一個字符;R會一直替換光標所在的文字,直到按下Esc鍵為止(常用) |
使用上面這些按鍵時,在vi畫面的左下角處會出現“—INSERT--”或“—replace--”的字樣。通過名稱就知道是什么操作。特別注意,上面也提過了,想在文件中輸入字符時,一定要在左下角處看到INSERT/ REPLACE才能輸入。 | |
Esc | 退出插入模式,回到命令行模式中(常用) |
末行命令模式 | |
:w | 將編輯的數據寫入硬盤文件中(常用) |
:w! | 若文件屬性為“只讀”時,強制寫入該文件。不過,到底能不能寫入,與文件權限有關 |
:q | 離開vi(常用) |
:q! | 若曾修改過文件,又不想存儲,使用!為強制離開不存儲文件 |
注意一下,那個感嘆號(!)在vi當中,常常具有“強制”的意思。 | |
:wq | 存儲后離開,若為:wq!則為強制存儲后離開(常用) |
:e! | 將文件還原到最原始的狀態 |
ZZ | 若文件沒有更改,則不存儲離開,若文件已經更改,則存儲后離開 |
:w[filename] | 將編輯的數據存儲成另一個文件(類似另存新文件) |
:r[filename] | 在編輯的數據中,讀入另一個文件的數據。即將“filename”這個文件內容加到光標所在行的后面 |
:n1、n2 w[filename] | 將n1到n2的內容存儲成filename文件 |
:!command | 暫時離開vi到命令模式下執行command的顯示結果。例如,“:! ls /home”,即可在vi中查看/home中以ls輸出的文件信息 |
:set nu | 顯示行號,設置之后,會在每一行的前綴顯示該行的行號 |
:set nonu | 與set nu相反,為取消行號 |
特別注意,在vi中,“數字”是很有意義的。數字通常表示重復做幾次的意思。也有可能表示要去哪里的意思。舉例來說,要刪除50行,則是用“50dd”。數字加在動作之前。要向下移動20行,使用“20j”或“20↓”即可。
掌握這些命令就很不錯了,因為常用到的命令也可能只有一半。通常vi的命令除了上面筆者注明的常用的幾個外,其他不用背,可以做一張簡單的命令表,當有問題時就可以馬上查詢。
售前咨詢
售后咨詢
備案咨詢
二維碼
TOP