Jan 26, 2009

我的 VIM 整理

Search 搜尋功能
「 * 」 #vim 就會幫你尋找出這個字串的邊界,並且搜尋該字串。
「 \< 」 #表示的是一個字的頭 「 \> 」 : 代表的是一個字的尾,當你寫\就是以link結尾的字,
「 g* 」 #如果我不想打那麼多字,可是我想要的又不要完全是一樣的


Regular Expression
/blue119$ #找「blue119」結尾的字串
/^blue119 #找「blue119」開頭的字串
/blue[119] #找出 blue加上任何119的組合
/lemon tree\|lemon soup\|lemon juice #「 \| 」這個「或(or)」運算
/.*[^。] #找出忘了在句末補上句號的地方 (中括號出現「 ^ 」就表示除了 "。" 以外的字元都是想要比對的字元)
\{1,} #表示至少要出現一次(至少選一次)
\{,1} #表示最多出現一次(最多選一次)
「 * 」, \{0,} #0個到任意個都可以的
/1234[-]\{,1}[0-9]\{1,} && /1234-[0-9]\{1,}\|1234[0-9]* #1234- 1234-5 1234-56 1234-567 1234-789 1234-2399 1234349898

更詳細的資料
:h regular
:h pattern
:h regular-expression


游標移動
#跳下一頁
#跳上一頁
「zz」 #游標目前位置放置螢幕中間
:行數 #跳到你指定行數
m + (大小寫英文字母) #Mark完後,利用 ['(大小寫英文字母)]回到剛剛Mark的位置. ex.如我在第10行換下 [ma],移到二十行後在換下 ['a]就可回到剛剛的第十行位置
「`」連按兩次 #回到上一次跳到的地方

更詳細的資料
:h mark


取代置換功能「s」

:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]

:'<,'>s/xxx/XXXX/g #利用 Visual mode 設定 [range], 按下大寫「V」然後移動游標反白到你想要的位置之後按「:」會跳到輸入指令的狀態
:1,300s/vim/VIM/g #設定[range]行數
:.,$s/vim/VIM/g #從現在游標所在行之後的所有行都置換
:.,1s/vim/VIM/g #從現在所在行之前的都要置換

範例:
aaaaaaaaa 111111111 111111111 aaaaaaaaa
bbbbbbbbb 222222222 222222222 bbbbbbbbb
ccccccccc 333333333 -> 333333333 ccccccccc
ddddddddd 444444444 444444444 ddddddddd
eeeeeeeee 555555555 555555555 eeeeeeeee
:%s/\([^0-9]\{1,}\)\([0-9]\{1,}\)/\2 \1/g

\( \) #括起來的,表示這是一個的單元, 依照它出現的順序而使用 \1, \2,…
[^0-9] #就表示非數字的部分
\{1,} #代表出現至少一次或用「\+」來代表至少出現一次

:h /multi #看到更多這些替代符號。


全域指令「:g」( global )

:[range]g[lobal]/{pattern}/[cmd]

:g/^[ \t]*$/d #砍掉空白行
:g/^$/d #砍掉空白行
:g/blue119/ #尋找
:g/小柴胡湯/nu #尋找+行數
:g!/[red123]/ #尋找但不包含特定字

:{range1}g/pattern1/{offset1};/pattern2/{offset2}{command}
:g/^.\{1,}$/,/^$/join! #段落只是用連續的分開,而你想要把所有的這些段落都合在一起
「join!」就是告訴 vim,在 /^.\{1,}$/ 和 /^$/ 之間的段落,通通都要合在一起(join)
「!」表示合併時不使用空白


vim 小技巧

計算有多少個搜尋關鍵字
:%s/pattern/&/g #&代表的意思就是用來表示前面比對的字串

整份文件全部合成一行
ggVGgJ (:%j!)(中文模式), ggVGJ (:%j)(英文模式)

移除文章中的^M
:%s/^M//g #^M = Ctrl+V & Ctrl+M or tr -d "\015" < filename1 > filename2


vim 的選擇模式 ( visual mode )

"ax #把整段程式碼剪下並貼到到暫存區 a
"ay #y 代表 yank,「拖拉」進指暫存器 a
"ap #貼上暫存器a的內容 (put)

更詳細的資料
:h visual-index


vim 的暫存區 register 功用

registers(暫存器)有下列幾種:
1. The unnamed register “”
2. 10 numbered registers “0 to “9
3. The small delete register “-
4. 26 named registers “a to “z or “A to “Z
5. four read-only registers “:, “., “% and “#
6. the expression register “=
7. The selection and drop registers “*, “+ and “~
8. The black hole register “_
9. Last search pattern register “/

Register 0 #register 0 就是幫你把最近一次做 yank 動作所存進去的東西。
Registers [1-9] #當你刪了某些字行會先進入 “1,當你又刪別的字行時,原先在 “1 的東西就會被放到 “2,這次刪的東西會進入 “1。
Registers [a-z] & [A-Z] #當你指定才會使用
"* #指的就是系統「剪貼簿」裡面的內容
": #就是指打的命令的暫存器
"/ #就是放搜尋的字串
". #放最近 insert 插入的文字
"- #放砍掉但不超過一行的文字
"% #指現在編輯的檔名
"_ #(黑洞暫存器),如果你刪除的動作不想要牽涉到任何暫存器

:let @a="Hello, world" #於"a 這個 register 放入 Hello, world 字串。
:let @a="" #清除某個暫存器
"[A-Z] #是附加方式而不是覆蓋
:di #看 register 裡有什麼東西
:g/pattern/y a #把每次比對到的那行都 yank 到 "a
:g/pattern/y A #指定的內容是要做附加到 “A
:g/pattern/. w >> filename #寫到某個檔案

更詳細的資料
:h registers


行數的顯示與利用

cat -n input_filename > output_filename #使用bash時

:%s/^.*$/\=line(".") . " " . submatch(0)/g #於行首加入行號
:%s/^/\=line(".")." "/ #於行首加入行號
line() #用來代表行數的
line()裡面的 "." #用來表示目前游標所在的地方
submatch(0) #用來表示前面所尋找的整個字串(pattern)
submatch(1) #用來表示第一個以 \(…\) 夾起來的子字串
開頭用 "\=" #可以作一些運算
「.」 #用來將字串與運作的結果相連

:set nu #看行號

更詳細的資料
:h sub-replace-expression


寫程式相關

「%」的游標移動方式。這個功能會選擇這些符號「 { [ ( 」的配對組合,也就是說當你在這些符號上面,比方說是「 { 」這個符號上按「 % 」游標會從「 { 」跳到它所對應「 } 」


Ctags 相關使用說明
Sigle folder

只需要 ctags -R 建立好索引後,進入 source code 就可利用 Ctrl+] 來找,回到原處按 Ctrl+t
臨時想到有個 function # :tag curve_extraction_algo
變數名字取得都一樣,在這個時候就會有多個選擇 #:tn & :tp

:h tags 得到更詳細的資訊


multi folder

1. making a shell script, call it "dirtags.sh"
#!/bin/sh
cd $1
ctags *

2. Now execute the following command:
find * -type d -exec dirtags.sh {} \;
to rebuild while making changes within a directory. The following vim key mapping :nmap ,t :!(cd %:p:h;ctags *.[ch])&

3. Build the global tag file:
cd ~/project
ctags --file-scope=no -R

4.
:set tags=./tags,tags,~/iEmbeddedSystem/QuantaSwitchSystem/LB4M-DIAG/tags

5. 推薦使用的 key map
imap :ls:bu
map :ls:bu
map :tn
map :tp


taglist 相關使用說明

:Tlist


cscope 相關使用說明

If you want, you can start it with a C symbol (ex: 'vim -t main')

Type "CTRL-\ s" (Control-backslash, then just 's') in quick succession, and you should see a menu at the bottom of your vim window showing you all the uses of the symbol in the program.
you can nest searches and CTRL-t will unwind them one at a time
via "CTRL-spacebar s". This time, your vim window will split in two horizontally , and the Cscope search result will be put in the new window.

:cs find {querytype} {name}
0 or s: Find this C symbol
1 or g: Find this definition
2 or d: Find functions called by this function
3 or c: Find functions calling this function
4 or t: Find this text string
6 or e: Find this egrep pattern
7 or f: Find this file
8 or i: Find files #including this file

setting the $CSCOPE_DB environment variable to point to a Cscope database you create, so you won't always need to launch vim in the same directory as the database.
find `pwd` -name '*.[sch]' > /tmp/cscope.files
cd /tmp
cscope -b -q
CSCOPE_DB=`pwd`/cscope.out; export CSCOPE_DB; cd -


Using Cscope on large projects (example: the Linux kernel)
For many projects, your find command may be as as simple as
cd /
find /my/project/dir -name '*.java' >/my/cscope/dir/cscope.files
For the Linux kernel
LNX=/home/jru/linux-2.4.18
cd /
find $LNX \
-path "$LNX/arch/*" ! -path "$LNX/arch/i386*" -prune -o \
-path "$LNX/include/asm-*" ! -path "$LNX/include/asm-i386*" -prune -o \
-path "$LNX/tmp*" -prune -o \
-path "$LNX/Documentation*" -prune -o \
-path "$LNX/scripts*" -prune -o \
-path "$LNX/drivers*" -prune -o \
-name "*.[chxsS]" -print >/tmp/cscope/cscope.files
Generate the Cscope database
cd /tmp/cscope # the directory with 'cscope.files'
cscope -b -q -k
Using the database.
CSCOPE_DB=/tmp/cscope/cscope.out; export CSCOPE_DB


視窗操作

Ctrl+w n #即 :new。開一空的新視窗。
Ctrl+w v #開一空的垂直新視窗。
Ctrl+w s #即 :sp(lit),會開一新視窗,且原檔分屬兩個視窗
Ctrl+w f #開一新視窗,並編輯游標所在處之 word 為檔名的檔案
Ctrl+w q #即 :q 結束分割出來的視窗
Ctrl+w o #即 :only! 使游標所在之視窗,成為目前唯一顯示的視窗其它視窗會隱藏起來
Ctrl+w j #移至下視窗
Ctrl+w k #移至上視窗
Ctrl+w _ #將此視視窗變大
Ctrl+w = #視窗同大
Ctrl+w 上、下、左、右 #移動curosr到其它視窗
:sp 檔名 #開另一新視窗來編輯檔案。


tabpage

:tabnew #開一個新的tab
:tabedit #filename 在新的tab編輯filename
:tabclose #關掉一個tab
gt #跳到下一個tab
gT #跳到前一個tab


[new undo] 用時間來做undo的依據

g- Go to older text state. With a count repeat that many
times. {not in Vi}

*:ea* *:earlier*
:earlier {count} Go to older text state {count} times.
:earlier {N}s Go to older text state about {N} seconds before.
:earlier {N}m Go to older text state about {N} minutes before.
:earlier {N}h Go to older text state about {N} hours before.

*g+*
g+ Go to newer text state. With a count repeat that many
times. {not in Vi}


*:lat* *:later*
:later {count} Go to newer text state {count} times.
:later {N}s Go to newer text state about {N} seconds later.
:later {N}m Go to newer text state about {N} minutes later.
:later {N}h Go to newer text state about {N} hours later.



拼字

]s #移至下一個拼錯或罕用的字。
[s #移至上一個拼錯或罕用的字。
]S #同 ]s,但只認完全拼錯的字。
[S #同 [s,但只認完全拼錯的字。
z= #檢查游標所在處的建議 words。
zg #加字於家目錄的字典檔。undo 鍵:zug,移除該字。
zw #同 zg,但標示此字為完全錯誤的字。undo 鍵 zuw,移除該字。

No comments: