一百個 programmer 應該知道的 vim 指令
http://www.catswhocode.com/blog/100-vim-commands-every-programmer-should-know
Since the 70′s, Vi is one of the programmer’s best friend. Nevermind you’re new to Vi or not, here’s a big list of 100 useful commands, organized by topic, which will make your coder life better.
原文錯誤很多 Orz
http://www.catswhocode.com/blog/100-vim-commands-every-programmer-should-know
Since the 70′s, Vi is one of the programmer’s best friend. Nevermind you’re new to Vi or not, here’s a big list of 100 useful commands, organized by topic, which will make your coder life better.
原文錯誤很多 Orz
我做了翻譯和更正
感謝 darkgerm 學長用心挑錯
====================================================
Basics 基本 :e filename 打開編輯(e) filename :w 存檔 :q 離開 :q! 離開但不存檔 Search 搜尋 /word 由上而下搜尋 word ?word 由下而上搜尋 word /jo[ha]n 搜尋 john 或 joan /\<the 搜尋以 the 開頭的字 /the\> 搜尋以 the 結尾的字 /\<the\> 搜尋 the 這個字 /\<....\> 找所有四個字母組成的字 /\<fred\> 找 fred 但不是 alfred 或 frederick /fred\|joe 找 fred 或 joe /\<\d\d\d\d\> 找四個數字 (長度為 4 的數字 )
/^\n\{\3\} 三個空行,有一個 magic 的寫法是 /\v^\n{3} (*)
:bufdo /searchstr/ 搜尋所有開啟的檔案
Replace 取代
:%s/old/new/g 在本檔案(%) 取代(s) 所有(g) old 改成 new
:%s/old/new/gc 取代所有時確認一下 c(Confirm)
:2,35s/old/new/g 第 2 到第 35 行之間取代
:5,$s/old/new/g 第五行到檔案結尾 (EOF) 之間取代
:%s/^/hello/g 把每一行的開頭取代成 hello
:%s/$/Harry/g 把每一行的結尾取代成 Harry
:%s/onward/forward/gi 取代 onward 成 forward ,不管大小寫(i)
:%s/ *$//g 刪除所有"trailing space"
:g/string/d 刪除所有包含 string 的行
:v/string/d 刪除所有不包含 string 的行
:s/Bill/Steve/ 取代本行第一個 Bill 成 Steve
:s/Bill/Steve/g 取代本行所有(g) Bill 成 Steve
:%s/Bill/Steve/g 取代本檔案(%) 所有(g) Bill 成 Steve
:%s/\r//g 刪除 DOS 換行 (^M)
:%s/\r/\n/g 把 DOS 換行改成一般換行 (*)
:%s#<[^>]\+>##g 刪除 HTML 標籤但保留文字 (*)
:%s/^\(.*\)\n\1$/\1/ 刪除重複兩次的行
Ctrl+a 游標所在的數字加一
Ctrl+x 游標所在的數字減一
ggVGg? 全文選取(ggVG) Rot13 加密 (g?)
Case 大小寫
Vu 整行選取(V)改小寫
VU 整行選取(V)改大寫
g~~ 整行大小寫轉換
vEU 把字改成大寫
vE~ 字的大小寫轉換
ggguG 全文改小寫
:set ignorecase 設定搜尋時忽略大小寫
:set smartcase 設定搜尋時忽略大小寫除非有使用大寫字
:%s/\<./\u&/g 每一個字的第一個字母改大寫
:%s/\<./\l&/g 每一個字的第一個字母改小寫
:%s/.*/\u& 每一行的第一個字母改大寫
:%s/.*/\l& 每一行的第一個字母改小寫
Read/Write files 讀寫檔案
:1,10 w outfile 在 outfile 存 1 到 10 行
:1,10 w >> outfile 附加 1 到 10 行去 outfile
:r infile 插入 infile 的內容
:23r infile 在 23 行插入 infile 的內容
File explorer 檔案瀏覽
:e . 打開檔案瀏覽器
:Sex 水平分割視窗並打開檔案瀏覽器 Sexplore (Split&Explore) (*)
:browse e 指定的目錄裏打開瀏覽器並編輯選擇的文件 {only in Win32, Athena, Motif, GTK and Mac GUI}
:ls 列出現在的 buffer
:cd .. 回到上一層資料夾
:args 列出參數 (正在編輯的檔案列表)
:args *.php php 檔
:grep expression *.php 回傳一個所有包含 expressoin2 的 .php 檔之列表
gf 開啟游標現在指向的檔案
Interact with Unix 跟 Unix 互動
:!pwd 執行 pwd unix 指令,然後回到 vi
!!pwd 執行 pwd unix 指令,把結果插入到檔案
:sh 暫時開一個 shell
$exit (在剛剛開的 shell 裡) 用 exit 指令回到 vim (其實用 Ctrl-D 也可以)
Alignment 對齊
:%!fmt 對齊所有行 (外部指令)
!}fmt 對齊所有在 {} 內的行
5!!fmt 對齊下五行
Tabs 分頁
:tabnew 開新分頁
gt 下一個分頁
gT 上一個分頁
:tabfirst 第一個分頁
:tablast 最後一個分頁
:tabm n tabmove 把當前分頁移到 n 的位置
:tabdo %s/foo/bar/g 在所有分頁執行指令
:tab ball 把所有有開的檔案放在一個 tab 裡
Window spliting 分割視窗
:e filename 在目前視窗編輯 filename
:split filename 分割視窗並開啟 filename
ctrl-w k 游標移到上面的視窗
ctrl-w ctrl-w 游標移到下一個視窗
ctrl-w_ 視窗最大化
ctrl-w= 視窗大小均等
10 ctrl-w+ 增加十行的大小
:vsplit file 垂直分割視窗
:sview file 分割視窗 (唯讀)
:hide 關閉目前視窗
:only 關閉其他所有視窗
:b 2 打開 buffer 2
Auto-completion 自動補完
Ctrl+n Ctrl+p (在 insert mode) 補完字
Ctrl+x Ctrl+l 補完行
:set dictionary=dict 將 dict 設成字典
Ctrl+x Ctrl+k 字典補完
Marks 標記
mk 將目前位置標記為 k
`k 把游標移動 k 標記
d'k 刪除所有 k 標記之前的東西
Abbreviations 縮寫
:ab mail mail@provider.org 定義 mail 是 mail@provider.org 的縮寫
Text indent 文字縮排
:set autoindent 設定自動縮排
:set smartindent 設定聰明縮排
:set shiftwidth=4 以四個 space 當作縮排大小
ctrl-t, ctrl-d 縮排/反縮排 (在 insert mode 下)
>> 縮排
<< 反縮排
Syntax highlighting 語法上色
:syntax on 打開語法上色
:syntax off 關閉語法上色
:set syntax=perl 設定特定語法
==================================================
**註**
Use of "\v" means that in the pattern after it all ASCII characters except '0'-'9', 'a'-'z', 'A'-'Z' and '_' have a special meaning. "very magic"
把 早期的 MAC 換行換成 windows 換行 windows 換行是 \r\n *nix 是 \n 早期的 MAC 是 \r 現在 MAC 底層是 BSD 所以也是 \n
有 dos2unix 可以轉
對這行 s/#/\//g XD
這個作法其實會有很多問題,例如如果 tag 中有換行就失效了
或是文字有 < > 也會出問題
引用 irc://irc.freenode.net/regex 的 title Do NOT use RegEx to parse HTML!
比較好的做法是用程式去 parse
Sexplore (Split&Explore)
還有 Vex 我覺的比較好用
離開但不存檔是 :q! 吧
ReplyDelete大大對不起QQ
Delete:x 等於 :wq
ReplyDelete蠻好用的
I know, but I'm used to use :wq instead of :x
Delete