2009年10月20日 星期二

Linux Command - vi with various tips

快速的搜尋字串

/〔pattern〕<return>



?〔pattern〕<return>

其中 / 表示前向搜尋,?表示背向搜尋。

我們可以用”n”來繼續同向搜尋,若用”N”,表示反向動

作,而於現在列搜尋字元,可使用下列方式:

f<char> 正向搜尋

F<char> 背向搜尋

t<char> 正向

T<char> 背向

F、f 指令表示移動游標到<char>上

T、t指令表示移動游標到<char>前


調整游標位置(重要)

z[Enter] position line with cursor at top
z. position line with cursor at middle
z- position line with cursor at
% goto matching parenthesis () {} []


vim 才有顏色 (syntax on in the $HOME/.vimrc, setting after finish install vim) 並且delete /sbin/vi ln -s /usr/local/sbin/vim /sbin/vi or set "vi" command alias
vi 並沒有顏色

安裝vi 6.3 時,需同時download 下面四個檔案(很重要,不要只有download 一個檔案,我曾經因此找了很久才發現原因)
-rw-r--r-- 1 vincent vincent 1423701 Dec 9 16:38 vim-6.3-rt1.tar.gz
-rw-r--r-- 1 vincent vincent 1132075 Dec 9 16:38 vim-6.3-rt2.tar.gz
-rw-r--r-- 1 vincent vincent 1397231 Dec 9 16:38 vim-6.3-src1.tar.gz
-rw-r--r-- 1 vincent vincent 560845 Dec 9 16:38 vim-6.3-src2.tar.gz

將四個檔案都解壓縮後
drwxr-xr-x 5 1000 1000 4096 Dec 10 11:48 vim63

再利用
1. make run configure, compile and link
2. make install installation in /usr/local
即可完成安裝

如果是安裝vim 7.2版就不用download 4個files, 只要download 1個file即可
ftp://ftp.vim.org/pub/vim/unix/
ftp://ftp.vim.org/pub/vim/unix/vim-7.2.tar.bz2
# vi --version
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Mar 30 2010 10:37:54)



vi search / replace regular expression
vi 不support "+" metacharacter

下面這些方法都可以work
:%s/\(--l7proto.*\)\(-j RETURN$\)/\1${IPGROUP}${SRC}${DST}\2/c
:%s/\(--l7proto[ a-z]*\)\(-j RETURN$\)/\1${IPGROUP}${SRC}${DST}\2/c
:%s/\(--l7proto \w* \)\(-j RETURN$\)/\1${IPGROUP}${SRC}${DST}\2/c

:%s/ \(${\w*}\)\(-j\)/\1 \2/c

下面這些方法不work (vi 不support unicode?)
:%s/\(--l7proto[\w ]*\)\(-j RETURN$\)/\1${IPGROUP}${SRC}${DST}\2/c
:%s/\(--l7proto[\w\x20]*\)\(-j RETURN$\)/\1${IPGROUP}${SRC}${DST}\2/c
:%s/\(--l7proto\x20\w*\x20\)\(-j RETURN$\)/\1${IPGROUP}${SRC}${DST}\2/c

search / replace 從特定行數到檔案結尾
:244,$s/MSN/FASTTRACK/g

對某一特定行做字串替換
:79,79s/MSN/SOULSEEK/g

將空白行(empty line) 刪除
:g/^[ ^I]*$/d

command mode
"#" 會自動搜尋目前游標的字串, 並且把該字串標示為反白


How to copy data from vi and paste the buffer into another file
Solution1:
1. Put the cursor on the first line you want to copy out.
2. Press "ma", i.e., "m" followed by "a"
3. Go to the last line you want to copy out.
4. Press ":'a,.w filename"
In step 2, you are marking the first line you want to write out with the "a" marker. In step 4, you are selecting all the lines from the "a" marker to the current line, inclusive, and writing them out to "filename".

Solution2:
If you know the starting and ending line numbers, say 1 to 100...
:1,100w newfile

沒有留言: