2009年3月15日星期日

vim&gvim技巧大全(3)

vim&gvim技巧大全(3)
li3 发表于 2006-2-27 14:25:00
http://blog.21ic.org/user1/313/archives/2006/9670.html
0
推荐启动vi的时候自动加载.尤其是源命令(so),缩写(ab)和宏定义都可以用.在.exrc中不
允许有空行.以"开头的一行vi将把它当成注释.

7.1 - .exrc文件的样本
以下的.exrc文件是一个实际的例子, 我自己的. 因此,它就没有有些人想象的那样设置
很多的选项.即便如此,它还是给出一些基本的概念,而且它也相当易懂.

首先,记住你无须在命令前面加:号,因为它是缺省就添加了的.

"译注--以下是.exrc文件中的内容,为真实起见,而且事实上也相当浅显,
"我没有翻译其中的注
" set wordwrap 9 characters from the right
set wm=9
" show matching parenthesis. When },), or ] is hit, show the opening
" {,(, or [ that matches it.
set sm
" set the shell to be /usr/local/bin/tcsh
set sh=/usr/local/bin/tcsh
" a few abbreviations
" this makes -l automatically become -Larry, and zidlle to be my
" name and email address.
ab zidlle E. Larry Lidz - ellidz@midway.uchicago.edu
ab -l -Larry
" uppercase V will run ispell -x (-x tells it not to save backups) on current
" file
map V :w
" K will center the line... (this doesn't work on SunOS -- ??)
map K :s/^[ ]*//
" v will reformat the current paragraph
map v {0!}fmt
" q runs a program to lock my terminal
map q :!lock2
" Splits a line in two at current cursor location, puts a > at the
"beginning of the line - useful for Usenet/email
map g may0O`ay$:s/./ /g

8.0 - vi的bug
我很想说vi中没有bug,但很不幸,它不是这样的. 这些bug都是初始版本vi中的,
它其他的一些变种就不一定有. 例如 _宏有时就运行的有问题,某个宏允许我们只敲
一个键就可以执行好几个不同的命令.

开放模式 open mode
开始模式是说vi启动的时候不能确定你的终端类型,或者它肯定你的是一个硬的或
电子管的终端

正规表达式 regular expressions (RegEx)
正规表达式,通常缩写为"RegEx",是一些关键字的集合,它允许用户在查找的时候有更大的
控制力度. RegEx是在UNIX下面用的,但是在很多程序中也用到了它.

sed
流-行式的编辑器.它没有界面,所有命令都是通过命令行的形式给出的.

vi
就是这个FAQ讨论的编辑器

9.0 - 术语表
如果有人提议说某个术语他不明白,我们将更新这个术语表.如果你有什么建议,
请一定要告诉我.

命令模式 command mode

命令模式是说我们在此模式下面可以向vi发命令(包括光标移动,编辑命令等等)

ed
UNIX里面一个非可视化的编辑器


escape键. 键盘上标记为escape(ESC)的一个键.有时候^[可以起到相同的作用.
如果没有escape键,你可以试试Meta键
ex
UNIX下的一个编辑器.所有vi中的:命令都是送给它处理的

插入模式 insert mode
插入模式是指我们可以在此模式下面键入文本.

宏 macro
宏是一些键的组合起来的快捷方式.例如,某个宏允许我们只敲一个键就可以执行
好几个不同的命令.

开放模式 open mode
开始模式是说vi启动的时候不能确定你的终端类型,或者它肯定你的是一个硬的或
电子管的终端

正规表达式 regular expressions (RegEx)
正规表达式,通常缩写为"RegEx",是一些关键字的集合,它允许用户在查找的时候有更大的
控制力度. RegEx是在UNIX下面用的,但是在很多程序中也用到了它.

sed
流-行式的编辑器.它没有界面,所有命令都是通过命令行的形式给出的.

vi
就是这个FAQ讨论的编辑器

--
昨夜银河送落花,潺潺落谁家?
冰心素月,两相映无暇。

寒烟淡笼蒹葭,风吹别怨过天涯。
双星黯处,山瞑隐清笳。
規則表示式的運用

在本系列文章一開始就說明了學 vi(m) 可以順便學規則表示式(regular expression,以下簡稱 regexp),那為什麼到現在才來講呢?因為 regexp 說簡單也算不很難,但您要深入去使用的話,有時會馬上看不出一個複雜的 regexp 在說些什麼的,就曾有人形容 regexp 為「有字天書」!而且在 vi(m) 整體都還沒一個概念就加入 regexp 的話,那後面的單元恐怕就沒人看了!而 regexp 各家有各家的 extensions,這也是大家視為畏途的原因之一,不過總是大同小異,只需注意一下就可以了。目前先不必管別家怎麼說,就讓 vim 暫時先成為我們的「標準」,以後碰到其它程式的 regexp 應該就可以觸類旁通。以下我們盡量由實例去瞭解。當然,小小的一篇文章是沒有辦法詳盡介紹,只能撿重點來說明了。如有疑問,可 :h pattern 或在 Un*x 系統中可 man 7 regex,甚至 man ed,man sed,man grep,man awk,man perlre 裡面也是會說些 regexp,但要注意和 vim 差異的地方!其中 perl 的 regexp 應該是最完整的了,如果您的系統沒有 perl 那應該是「稀有動物」了!:-) ㄟㄟㄟ!vim 只是一個編輯器,可不是獨立的程式語言!

基本的匹配

* 指前所綁住的字元或字元集合,出現 0 次或 0 次以上。
\+ 和 * 作用相同,但不包括出現 0 次。
\= 指前所綁住的字元恰好出現 0 或 1 次。
\| 這是多選,就是 or 的意思,被 \| 隔開的 pattern,任一個符
合的話就算符合。

\+, \=, \| 會加上一個 \,是因原字元在 vi(m) 就具有特殊意義,在一般的 regexp 中是 +,?,| 就可以了,只是提醒您一下,以免搞混了!
記住 * \+ 是不可數的!用辭不是是精確,只是幫助您記憶啦!
在 elvis 及 ed 中是使用 \? 來匹配出現 0 或 1 次,而不是 \=,這裡要非常小心!
[實例] dg*
指 * 前所綁住的字元 g 出現 0 次或 0 次以上。也就是說 d(出現 0 次),dg,dgggg,dgggggggg 都是符合這個 pattern。如果您下尋找指令 /dg*,那符合這個 pattern 的字串都會被找出來。如果用在代換就要非常小心了,像 extended 中的 d 也是會被置換掉的。例如您下 :%s/dg*/test/g 的話,那 extended 這個字會換成 extentestetest。

shell 中使用的通用字元為 pattern matching notation 和 regexp 不同的。dg* 在 shell 中是解為以 dg 開頭的任意字串,這就不包括 d 在內了,也就是說在 shell 中,* 是代表任一字元或字串。
[實例] dg\+
dg, dgg, dgggggg 皆符合,但 d 則不符合。如果是 dg\= 的話,就只有 d、dg 這兩個符合了。

[實例] :%s/The\|All/test/g
全文中只要是 The 或 All 都會被替換成 test。注意,如果文中有 There 也是會被替換成 testre!要如何避免這種情形呢?下面會另述及限定使用法。

[實例] /123-\=4567
這樣會找出,123-4567 及 1234567。當然 123-456789 也是會被找出來。

[...] 字元集合,表示中括號中所有字元中的其中一個。
[^..] 這是上述 [...] 的補集,表非中括號內字元的其中一個。
. 除換行字元外的任一單一字元。指本身,非指前所綁之字元。
就好像 shell 中的 ? 一樣。如果要指定真正的英文句點,要
用 \ 來 escape,就是說 \. 這時的 . 是代表真正句點,而不
是 regexp 中的特殊意義。其他如 \* 亦同。

[實例]

[Aa]
A 或 a 其中的一個。
[12345]
12345 其中的一個數目字。可用 [1-5] 來表示。連續性的數目字或字元可用 - 來隔開,寫出頭尾來代表就可以了。[0-9] 就表 0 到 9 的數目字,[a-d] 就代表 abcd 四個英文字母

[實例] W[0-9]*\.cc
這個例子是說以 W 開頭,後接 0-9 其中一個或多個數目字或不接什麼,然後是一個句點,最後是 cc。所以 W.cc,W1.cc,W2.cc,W345.cc,W8976543287.cc 皆符合。如果要表示 W 及 .cc 間夾一個以上的數目字,要寫成 W[0-9][0-9]*\.cc。

[實例] .*
這代表任意字元或字串,或什麼都沒有,腦筋急轉彎,對照前面的定義想一下。當然這是不包括換行字元的。

[實例]
[^M] 表除 M 以外的任意字元。
[^Tt] 表 T 及 t 以外的任意字元。
[^0-9] 表非數目字之字元。
[^a-zA-Z] 表非英文字母之字元。

注意,^ 要在中括號內,且在最開頭的地方,否則另有含意。
^ 匹配行首,指其後綁住的字串,出現在行首才符合。
$ 匹配行尾,指其前綁住的字串,出現在行尾才符合。含換行字元。

不是在行首的 ^ 指的是 ^ 這個字元。不是在行尾的 $ 是指 $ 本身這個字元。
[實例] /^What
這樣只有在行首的 What 才會被找出來。注意! Whatever, What's 也是會被找出來。如果是 /What$ 則是在行尾的 What 才會被找出來。

[實例] ^$
這是什麼東東?行首也是行尾的行。ㄚ,就是空白行嘛!當然也不能說這個行是沒有什麼東東啦!空白行至少也是會有個換行字元。在後面會詳述如何消除全文的空白行。

\(...\) 記憶 pattern,可由 \1, \2...\9 來叫出。

[實例] :%s/\([a-z]\)\1/test/g
這樣 aa, bb, cc, dd, ..., zz 都會被 test 替換掉。這和 :%s/[a-z][a-z]/test/g 是不一樣的意思,後者會把 aa, ab, ac... ba, bb, bc...zz 都換成 test。也就是說 \(...\) 由 \1 叫出時會有對稱性的配對出現。

[實例] :%s/\(.\)\(.\)r\2\1/test/g
會將中間為 r,前有二個任一字元,後有兩個具對稱性的字元所組成的字串替換成 test。\2 是呼叫第二組 \(.\),而 \1 是呼叫第一組 \(.\)。例如:12r21,cfrfc,7grg7 等都會被替換成 test。

\< 匹配字(word)首。所謂 word 包括文數字及底線。
\> 匹配字尾。這就是前所提及的限定用法,被 \<,或 \> 括住的
pattern 就會被限制住,使 regexp 不能再向右(左)擴充解釋。

ed 及 perl 中可以 \b 來表示這兩個符號,perl 中只支援 \b,ed 則 \b 及 \<, \>皆支援。但在 perl 可多加個 ? 來限制 regexp 的擴充解譯。
功能上而言,這是和 ^ $ 一樣的定位樣式(anchor pattern)指所綁住的字串必須是單字邊界(word boundary),前或後或前後除了空白字元及標點符號外不可再有其它字元。
在 vim 中 \b 是表示 即 backspace 鍵。
[實例] :%s/\/test/g
這樣只有 abbbc 才會被替換成 test。如果沒有這樣限定,:%s/abbbc/test/g,那 deabbbcly 中的 "abbbc" 亦會被替換成 test。所以前面 :%s/The\|All/test/g 可換成 :%s/\\|\/test/g 這樣一來,There 就不會被替換成 testre 了!

[實例] :%s/\《abbbc/test/g
這樣的話,只要是以 abbbc 為首的字(word),其中的 abbbc 的部份都會被 test 所替換。注意!是指字首,而不是指行首。所以 abbbc,abbbcerd,abbbckijuds 都符合。

\{n,m} 指前所綁住的字元或字元集合最少出現 n 次,最多出現 m 次。

這在一般的 regexp 表示成 \{n,m\}。vim 及 elvis 兩種表示法皆支援。perl 則直接使用 {}。以下會舉四種不同的例子,請大家發揮一下想像力。:-)
[實例] \{最小值,最大值}
如 [0-9]\{3,4} 匹配至少三位數,但不可多於四位數的數目字。如:
123
12
1
123456
1234567
12345678
1234
12345

如果下 :%s/[0-9]\{3,4}/test/g 的話,那 1,12 這兩組不會被替換,因為不滿 3 位數。而 12345,則會換成 test5。123456,則會換成 test56。12345678,則會換成 testtest。1234567 也是會換成 testtest。123,1234 這兩組則會被替換成 test。您可以親自操作一次就知道怎麼一回事了。操作時最後加 gc 來 confirm,這樣您會更瞭解實際替換的內容。ㄟ,別忘了 u 可以回複您的編輯動作。

[實例] \{數目字}
xy\{20} 表示 x 後接 20 個 y。
e[x-z]\{4} 表示 e 後接有四個字元,是 x,y,z 的其中一個的
  組合。如:exxxx, exyyz, ezzyz, exyzz 皆符合。

[實例] \{最小值,}
xy\{2,} 表 x 後接至少二個的 y。相當於 xyyy* 或 xyy\+ 。

[實例] \{,最大值}
xy\{,4} 表 x 後接至多四個或更少的 y (可能沒有)。
因此 x, xy, xyy ,xyyy, xyyyy 皆符合。

中介字元(metacharacter, or character classes)

主要是簡化 regexp 的書寫。

\s 表空白字元,即

不含換行字元,這是編輯器的特性使然。在 perl 的 \s 是包含換行字元的。而且 vim 及 elvis 皆不支援 \n 這種換行中介字元。
\S 表非空白字元。
\d 表數目字(digits),即 [0-9]。
\D 表非數目字,即 [^0-9]。
\w 表一般字元(word character),包括底線。即 [0-9a-zA-Z_]。
\W 表非一般字元,即 [^0-9a-zA-Z_]。
\a 表英文字母(alphabetic character),即 [a-zA-Z]。
\A 表非英文字母,即 [^a-zA-Z]。
\l 表小寫字母(lowercase character),即 [a-z]。
\L 表非小寫字母,即 [^a-z]。
\u 表大寫字母(uppercase),即 [A-Z]。
\U 表非大寫字母,即 [^A-Z]。

原始 vi 不支援此種中介字元。
使用中介字元的比對速度將會比使用字元集合 [] 的快。

全域性的指令

:[range]g/pattern/[cmd]

cmd 是 ed 可用的指令,預設是 p(print),您可查一下 man ed,就可以知道有什麼指令可用。這個小節裡主要是說明 d(delete) 的功能。因為是要說明如何消除空白行。需注意的是,d 是行刪除指令,凡含 pattern 的整行都會被刪掉,而且 range 不指定的話,預設是全篇文章,因為 g 就是代表 globe。
在 vim 的 help 檔裡說的是 ex 指令,但 ex 實際上是和 vim 連結的,因此這裡特別指出 ed。但 ed 的指令少數可能會和 vim 的 ex 不同,這是因為 ed 和 vim 並非同步在發展,作者也非同一人。
:g/^$/d

這樣就會刪除全文的空白行。前面已提過 ^$ 代表的是空白行。但這裡有個問題,如果空白行裡包含了其它空白字元(即 Space 或 Tab)的話。表面看起來是和一般空白行一模一樣,但卻暗藏玄機,用上面的方法就無法刪除這種空白行了!怎麼辦?來!看招!
:g/^[]*$/d

在 vim 或 elvis 裡您可以如此照打,也就是 代表空白字元, 代表按 Tab 鍵的結果。在原始 vi 則不行,得自行按出特殊字元出來,就是 Ctrl-v Space 及 Ctrl-v Tab。或採更簡單的打法:
:g/^\s*$/d

還記得中介中元嗎?好用吧!少打了不少字。:-) 意思就是刪除含 0 或 1 個以上空白字元的行。
有些書中寫成 :%s/^$//g 可以刪除空白行,這是錯誤的,因為 :s 這個指令只更動一行裡的內容物,但不會做刪除一行的動作。

& 替代變數

代表置換時合於 patern 的字元或字串。

[實例] :%s/\u\d\d\d\d\d\d\d\d\d\>/ID:&/g
這樣全文中的身份證字號前就會加上 ID: 字樣,也就是說 T123456789 會被換成 ID:T123456789。還記得嗎? \d 就是 [0-9],\u 代表大寫的英文字母。加個 \> 是防止 T12345678999 也被換掉。當然前面再加個 \< 更保險。ID: 字樣您用中文也行!
另一個好用的例子是電話號碼前加上 TeL:,就請您自行練習了!

[實例] 將檔案 3 至 7 行的資料向右移 2 個空白

:3,7s/.*/ &/

但這樣連空白行也是會插入空白字元,較高明的做法是:
:3,7s/.\+/ &/

這樣空白行就不會去動它了!想通了 .* 及 .\+ 的意思了嗎?往前翻一下 . * \+ 的定義。

[實例] 將檔案 3 至 7 行的資料向左移 2 個空白

:3,7s/^ //

就是刪去行首的二個空白啦!

[實例] 將全文的 Edward 這個單字,前後加上中括號

:%s/\/[&]/g

[實例] 將全文的 Edward 這個單字,改成大寫的。

:%s/\/\U&/g

ㄟ!\U 不是代表非大寫字母嗎?喔!您搞錯位置了。\U 在 pattern 的位置的時候是指非大寫字母的樣式,即 [^A-Z],但如果是在置換字串位置的時候是指將其後的字串通通改成大寫。與其相對的是 \L,會將其後的字串改為小寫。詳細請 :h sub-replace-special。
[實例] 將全文每行最後加上
這個 HTML tag。

:%s/.*/&
/g

怎麼樣,是否已感覺到 regexp 威力無窮了呢?還是您已經快睡著了呢?:-) 不過也請您想想,如果是在沒有 regexp 功能的編輯器裡,範例中的一些動作您會怎麼做呢?一個一個去改?

greedy 陷阱

regexp 會有貪心的傾向,什麼意思呢?就是說在同一行內,如果有多個符合 pattern 的情形,會找最長的那一個。

注意!greedy 的特性是針對會反覆比對的 regexp 而言,例如:*, \=, \+, \{} 等。前面所舉的 .* 的例子,由於 greedy 的關係,在整篇文章中做替換時,會被當成是每一行整行,因為 regexp 會去找每一行最長符合的那一個。
[實例] This is a test. Test for regexp.
如果您下 :%s/[Tt].*t/program/g 原意是想把所有的 Test 或 test 換成 program 的,結果由於 regexp 的貪心,整個 "This is a test. Test" 會換成 program。結果原文就變成了 program for regexp. 因此在全文替換時要非常小心,避免使用彈性太大的 regexp。像此例,只要下 :%s/\<[Tt]est\>/program/g 就可以了!

最後提醒您,這可不是 regexp 的全部,礙於篇幅及在下功力的問題,當然是沒辦法全面詳盡的向各位做介紹,在下只是將各位領進門,修行就得看各位了!如果還想更深入的研究 regexp,可參考: Mastering Regular Expressions(O'Reilly & Associates) 一書。

David Rayner (zzapper) 15 Years of Vi + 4 years of Vim and still learning 27Dec05 : Last Update
Tips Sign-Up __BEGIN__
------------------------------------------------------------------------------
" searching
/joe/e : cursor set to End of match
/joe/e+1 : cursor set to End of match plus 1
/joe/s-2 : cursor set to Start of match minus 2
/^joe.*fred.*bill/ : normal
/^[A-J]\+/ : search for lines beginning with one or more A-J
/begin\_.*end : search over possible multiple lines
/fred\_s*joe/i : any whitespace including newline
/fred\|joe : Search for FRED OR JOE
/.*fred\&.*joe : Search for FRED AND JOE in any ORDER!
/\/i : search for fred but not alfred or frederick
/\<\d\d\d\d\> : Search for exactly 4 digit numbers
/\D\d\d\d\d\D : Search for exactly 4 digit numbers
/\<\d\{4}\> : same thing
/\([^0-9]\|^\)%.*% : Search for absence of a digit or beginning of line
" finding empty lines
/^\n\{3} : find 3 empty lines
" using rexexp memory in a search
/\(fred\).*\(joe\).*\2.*\1
" Repeating the Regexp (rather than what the Regexp finds)
/^\([^,]*,\)\{8}
" visual searching
:vmap // y/" : search for visually highlighted text
:vmap // y/=escape(@", '\\/.*$^~[]') : with spec chars
" \zs and \ze regex delimiters :h /\zs
/<\zs[^>]*\ze> : search for tag contents, ignoring chevrons
" zero-width :h /\@=
/<\@<=[^>]*>\@= : search for tag contents, ignoring chevrons
/<\@<=\_[^>]*>\@= : search for tags across possible multiple lines
" searching over multiple lines \_ means including newline
/ : search for multiple line comments
/fred\_s*joe/i : any whitespace including newline
/bugs\(\_.\)*bunny : bugs followed by bunny anywhere in file
:h \_ : help
" search for declaration of subroutine/function under cursor
:nmap gx yiw/^\(sub\function\)\s\+"
" multiple file search
:bufdo /searchstr/ : use :rewind to recommence search
" multiple file search better but cheating
:bufdo %s/searchstr/&/gic : say n and then a to stop
" How to search for a URL without backslashing
?http://www.vim.org/ : search BACKWARDS!!! clever huh!
" Specify what you are NOT searching for (vowels)
/\c\v([^aeiou]&\a){4} : search for 4 consecutive consonants
----------------------------------------
#substitution
:%s/fred/joe/igc : general substitute command
:%s/\r//g : Delete DOS returns ^M
" Is your Text File jumbled onto one line? use following
:%s/\r/\r/g : Turn DOS returns ^M into real returns
:%s= *$== : delete end of line blanks
:%s= \+$== : Same thing
:%s#\s*\r\?$## : Clean both trailing spaces AND DOS returns
:%s#\s*\r*$## : same thing
" deleting empty lines
:%s/^\n\{3}// : delete blocks of 3 empty lines
:%s/^\n\+/\r/ : compressing empty lines
%s#<[^>]\+>##g : delete html tags, leave text
" IF YOU ONLY WANT TO KNOW ONE THING
:'a,'bg/fred/s/dick/joe/igc : VERY USEFUL
" duplicating columns
:%s= [^ ]\+$=&&= : duplicate end column
:%s= \f\+$=&&= : same thing
:%s= \S\+$=&& : usually the same
" memory
:s/\(.*\):\(.*\)/\2 : \1/ : reverse fields separated by :
:%s/^\(.*\)\n\1$/\1/ : delete duplicate lines
" non-greedy matching \{-}
:%s/^.\{-}pdf/new.pdf/ : delete to 1st pdf only
" use of optional atom \?
:%s#\<[zy]\?tbl_[a-z_]\+\>#\L&#gc : lowercase with optional leading characters
" over possibly many lines
:%s/// : delete possibly multi-line comments
:help /\{-} : help non-greedy
" substitute using a register
:s/fred/a/g : sub "fred" with contents of register "a"
:s/fred/\=@a/g : better alternative as register not displayed
" multiple commands on one line
:%s/\f\+\.gif\>/\r&\r/g | v/\.gif$/d | %s/gif/jpg/
:%s/a/but/gie|:update|:next : then use @: to repeat
" ORing
:%s/suck\|buck/loopy/gc : ORing (must break pipe)
" Calling a VIM function
:s/__date__/\=strftime("%c")/ : insert datestring
" Working with Columns sub any str1 in col3
:%s:\(\(\w\+\s\+\)\{2}\)str1:\1str2:
" Swapping first & last column (4 columns)
:%s:\(\w\+\)\(.*\s\+\)\(\w\+\)$:\3\2\1:
" filter all form elements into paste register
:redir @*|sil exec 'g#<\(input\|select\|textarea\|/\=form\)\>#p'|redir END
:nmap ,z :redir @*sil exec 'g@<\(input\select\textarea\/\=form\)\>@p'redir END
" decrement numbers by 3
:%s/\d\+/\=(submatch(0)-3)/
" increment numbers by 6 on certain lines only
:g/loc\|function/s/\d/\=submatch(0)+6/
" better
:%s#txtdev\zs\d#\=submatch(0)+1#g
:h /\zs
" increment only numbers gg\d\d by 6 (another way)
:%s/\(gg\)\@<=\d\+/\=submatch(0)+6/
:h zero-width
" rename a string with an incrementing number
:let i=10 | 'a,'bg/Abc/s/yy/\=i/ |let i=i+1 # convert yy to 10,11,12 etc
" as above but more precise
:let i=10 | 'a,'bg/Abc/s/xx\zsyy\ze/\=i/ |let i=i+1 # convert xxyy to xx11,xx12,xx13
" find replacement text, put in memory, then use \zs to simplify substitute
:%s/"\([^.]\+\).*\zsxx/\1/
" Pull word under cursor into LHS of a substitute
:nmap z :%s#\<=expand("")\>#
" Pull Visually Highlighted text into LHS of a substitute
:vmap z :%s/\<*\>/
----------------------------------------
" all following performing similar task, substitute within substitution
" Multiple single character substitution in a portion of line only
:%s,\(all/.*\)\@<=/,_,g : replace all / with _ AFTER "all/"
" Same thing
:s#all/\zs.*#\=substitute(submatch(0), '/', '_', 'g')#
" Substitute by splitting line, then re-joining
:s#all/#&^M#|s#/#_#g|-j!
" Substitute inside substitute
:%s/.*/\='cp '.submatch(0).' all/'.substitute(submatch(0),'/','_','g')/
----------------------------------------
" global command display
:g/gladiolli/# : display with line numbers (YOU WANT THIS!)
:g/fred.*joe.*dick/ : display all lines fred,joe & dick
:g/\/ : display all lines fred but not freddy
:g/^\s*$/d : delete all blank lines
:g!/^dd/d : delete lines not containing string
:v/^dd/d : delete lines not containing string
:g/fred/,/joe/d : not line based (very powerfull)
:g/-------/.-10,.d : Delete string & 10 previous lines
:g/{/ ,/}/- s/\n\+/\r/g : Delete empty lines but only between {...}
:v/\S/d : Delete empty lines (both types)
:v/./,/./-j : compress empty lines
:g/^$/,/./-j : compress empty lines
:g/《input\|《form/p : ORing
:g/^/put_ : double space file (pu = put)
:g/^/m0 : Reverse file (m = move)
:'a,'b/^/m'b : Reverse a section a to b
:g/^/t. : duplicate every line
:g/fred/t$ : copy lines matching fred to EOF
:g/stage/t'a : copy lines matching stage to marker a
:g/\(^I[^^I]*\)\{80}/d : delete all lines containing at least 80 tabs
" perform a substitute on every other line
:g/^/ if line('.')%2|s/^/zz /
" match all lines containing "somestr" between markers a & b
" copy after line containing "otherstr"
:'a,'bg/somestr/co/otherstr/ : co(py) or mo(ve)
" as above but also do a substitution
:'a,'bg/str1/s/str1/&&&/|mo/str2/
:%norm jdd : delete every other line
" incrementing numbers (type as 5 characters)
:.,$g/^\d/exe "norm! \": increment numbers
:'a,'bg/\d\+/norm! ^A : increment numbers
" storing glob results (note must use APPEND) you need to empty reg a first with qaq.
"save results to a register/paste buffer
:g/fred/y A : append all lines fred to register a
:g/fred/y A | :let @*=@a : put into paste buffer
:let @a=''|g/Barratt/y A |:let @*=@a
:'a,'b g/^Error/ . w >> errors.txt
" duplicate every line in a file wrap a print '' around each duplicate
:g/./yank|put|-1s/'/"/g|s/.*/Print '&'/
" replace string with contents of a file, -d deletes the "mark"
:g/^MARK$/r tmp.ex | -d
" display prettily
:g//z#.5 : display with context
:g//z#.5|echo "==========" : display beautifully
" Combining g// with normal mode commands
:g/|/norm 2f|r* : replace 2nd | with a star
"send output of previous global command to a new window
:nmap :redir @a:g//:redir END:new:put! a
----------------------------------------
" Global combined with substitute (power editing)
:'a,'bg/fred/s/joe/susan/gic : can use memory to extend matching
:g/fred/,/joe/s/fred/joe/gic : non-line based (ultra)
----------------------------------------
" Find fred before beginning search for joe
:/fred/;/joe/-2,/sid/+3s/sally/alley/gIC
----------------------------------------
" create a new file for each line of file eg 1.txt,2.txt,3,txt etc
:g/^/exe ".w ".line(".").".txt"
----------------------------------------
" Absolutely essential
----------------------------------------
* # g* g# : find word under cursor () (forwards/backwards)
% : match brackets {}[]()
. : repeat last modification
@: : repeat last : command (then @@)
matchit.vim : % now matches tags 《script> : word completion in insert mode
: Line complete SUPER USEFUL
/ : Pull onto search/command line
/ : Pull onto search/command line
:set ignorecase : you nearly always want this
:syntax on : colour syntax in Perl,HTML,PHP etc
:h regexp : type control-D and get a list all help topics containing
regexp (plus use TAB to Step thru list)
----------------------------------------
" MAKE IT EASY TO UPDATE/RELOAD _vimrc
:nmap ,s :source $VIM/_vimrc
:nmap ,v :e $VIM/_vimrc
----------------------------------------
#VISUAL MODE (easy to add other HTML Tags)
:vmap sb "zdiz : wrap around VISUALLY selected Text
:vmap st "zdiz ?> : wrap around VISUALLY selected Text
----------------------------------------
" Exploring
:Exp(lore) : file explorer note capital Ex
:Sex(plore) : file explorer in split window
:ls : list of buffers
:cd .. : move to parent directory
:args : list of files
:lcd %:p:h : change to directory of current file
:autocmd BufEnter * lcd %:p:h : change to directory of current file automatically (put in _vimrc)
----------------------------------------
" Buffer Explorer (Top Ten Vim Script)
" needs bufexplorer.vim http://www.vim.org/script.php?script_id=42
\be : buffer explorer list of buffers
\bs : buffer explorer (split window)
----------------------------------------
" Changing Case
guu : lowercase line
gUU : uppercase line
Vu : lowercase line
VU : uppercase line
g~~ : flip case line
vEU : Upper Case Word
vE~ : Flip Case Word
ggguG : lowercase entire file
" Titlise Visually Selected Text (map for .vimrc)
vmap ,c :s/\<\(.\)\(\k*\)\>/\u\1\L\2/g
" Uppercase first letter of sentences
:%s/[.!?]\_s\+\a/\U&\E/g
----------------------------------------
gf : open file name under cursor (SUPER)
:nnoremap gF :view : open file under cursor, create if necessary
ga : display hex,ascii value of char under cursor
ggVGg? : rot13 whole file
ggg?G : rot13 whole file (quicker for large file)
:8 | normal VGg? : rot13 from line 8
:normal 10GVGg? : rot13 from line 8
, : increment,decrement number under cursor
win32 users must remap CNTRL-A
=5*5 : insert 25 into text (mini-calculator)
----------------------------------------
" Makes all other tips superfluous
:h 42 : also http://www.google.com/search?q=42
:h holy-grail
:h!
----------------------------------------
" Markers & moving about
'. : jump to last modification line (SUPER)
`. : jump to exact spot in last modification line
g; : cycle thru recent changes (oldest first) (new in vim6.3)
g, : reverse direction (new in vim6.3)
:changes
:h changelist : help for above
: retrace your movements in file (starting from most recent)
: retrace your movements in file (reverse direction)
:ju(mps) : list of your movements
:help jump-motions
:history : list of all your commands
:his c : commandline history
:his s : search history
q/ : Search history Window (puts you in full edit mode)
q: : commandline history Window (puts you in full edit mode)
: : history Window
----------------------------------------
" Abbreviations & maps
:map :'a,'bw! c:/aaa/x
:map :r c:/aaa/x
:map :.w! c:/aaa/xr
:map :r c:/aaa/xr
:ab php : list of abbreviations beginning php
:map , : list of maps beginning ,
" allow use of F10 for mapping (win32)
set wak=no : :h winaltkeys
" For use in Maps
: carriage Return for maps
: Escape
: normally \
: | pipe
: backspace
: No hanging shell window
#display RGB colour under the cursor eg #445588
:nmap c :hi Normal guibg=#=expand("")
----------------------------------------
" Simple PHP debugging display all variables yanked into register a
iab phpdb exit("
Debug a ");
----------------------------------------
" Using a register as a map (preload registers in .vimrc)
:let @m=":'a,'bs/"
:let @s=":%!sort -u"
----------------------------------------
" List your Registers
:reg : display contents of all registers
:reg a : display content of individual registers