这是篇每个 Vim 用户都应该阅读的文章,原文在这里
http://www.rayninfo.co.uk/vimtips.html 这篇文章很早被翻译过,好像是一个清华的哥们翻译的。我再原来的基础上又整理
和新增了些内容--这是两年前做的,后来没有再更新。有兴趣的人可以继续更新。
__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
查找以一个或多个 A-J 中的字母开头的行
/begin\_.*end : search over possible multiple lines
查找在 begin 和 end 两个单词之间尽可能多的行
/fred\_s*joe/i : any whitespace including newline
查找在 fred 和 joe 两个单词之间任意多的空格,包括新行
/fred\|joe : Search for FRED OR JOE
查找 fred 或 joe
/\([^0-9]\|^\)%.*% : Search for absence of a digit or beginning of line
查找
/.*fred\&.*joe : Search for FRED AND JOE in any ORDER!
查找同时包含 FRED 和 JOE 的行,不分前后顺序
/\<fred\>/i : search for fred but not alfred or frederick
查找 fred, 而不是 alfred 或者 frederick,也就是全字匹配
/\<\d\d\d\d\> : Search for exactly 4 digit numbers
查找4个数字的全字匹配
/\D\d\d\d\d\D : Search for exactly 4 digit numbers
查找4个数字的全字匹配
/\<\d\{4}\> : same thing
同上
# finding empty lines 查找空行
/^\n\{3} : find 3 empty lines
查找 3 行空行
# Specify what you are NOT searching for (vowels)
# 指定不要查找什么
/\c\v([^aeiou]&\a){4} : search for 4 consecutive consanants
# using rexexp memory in a search
# 在查找中使用正则表达式存储
/\(fred\).*\(joe\).*\2.*\1
# Repeating the Regexp (rather than what the Regexp finds)
# 重复正则表达式
/^\([^,]*,\)\{8}
# visual searching
# 可视模式下的查找
:vmap // y/<C-R>"<CR> : search for visually highlighted text
查找被高亮显示的文本
:vmap <silent> // y/<C-R>=escape(@", '\\/.*$^~[]')<CR><CR> : with spec chars
# searching over multiple lines \_ means including newline
# 查找多行。\_ 表示包括新行
/<!--\_p\{-}--> : search for multiple line comments
查找多行注释
/fred\_s*joe/i : any whitespace including newline
查找在 fred 和 joe 两个单词之间任意多的空
格,包括新行
/bugs\(\_.\)*bunny : bugs followed by bunny anywhere in file
bugs 后任意位置含有 bunny 单词的多个行
:h \_ : help
帮助
# search for declaration of subroutine/function under cursor
# 查找光标下子程序/函数的声明
:nmap gx yiw/^\(sub\<bar>function\)\s\+<C-R>"<CR>
# multiple file search
# 在多个文件中查找
:bufdo /searchstr
:argdo /searchstr
# How to search for a URL without backslashing
# 如何不使用反斜线查找 URL
?http://www.vim.org/ : search BACKWARDS!!! clever huh!
----------------------------------------
# substitution
# 替换
:%s/fred/joe/igc : general substitute command
普通替换命令
:%s/\r//g : Delete DOS returns ^M
删除 DOS 回车符 ^M
# Is your Text File jumbled onto one line? use following
# 你的文本文件是否乱七八糟的排成一行?使用如下命令
:%s/\r/\r/g : Turn DOS returns ^M into real returns
转换 DOS 回车符 ^M 为真正的回车符
:%s= *$== : delete end of line blanks
删除行尾空格
:%s= \+$== : Same thing
同上
:%s#\s*\r\?$## : Clean both trailing spaces AND DOS returns
删除行尾空格和 DOS 回车符
:%s#\s*\r*$## : same thing
删除行尾空格和 DOS 回车符
# deleting empty lines
# 删除空行
:%s/^\n\{3}// : delete blocks of 3 empty lines
删除三行空行
:%s/^\n\+/\r/ : compressing empty lines
压缩多行空行为一行
# 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
只删除到第一个 pdf
# 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/<c-r>a/g : sub "fred" with contents of register "a"
用"a"寄存器里的内容替换"fred"
: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/
# ORing
:%s/suck\|buck/loopy/gc : ORing (must break pipe)
不懂
# Calling a VIM function
# 调用 Vim 函数
:s/__date__/\=strftime("%c")/ : insert datestring
插入日期
# Working with Columns sub any str1 in col3
# 处理列,替换所有在第三列中的 str1
:%s:\(\(\w\+\s\+\)\{2}\)str1:\1str2:
# Swapping first & last column (4 columns)
# 交换第一列和最后一列 (共4列)
:%s:\(\w\+\)\(.*\s\+\)\(\w\+\)$:\3\2\1:
# filter all form elements into paste register
# 把所有的form元素(就是html里面的form啦)放到register里
:redir @*|sil exec 'g#<\(input\|select\|textarea\|/\=form\)\>#p'|redir END
:nmap ,z :redir @*<Bar>sil exec
'g@<\(input\<Bar>select\<Bar>textarea\<Bar>/\=form\)\>@p'<Bar>redir END<CR>
# 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
# find replacement text, put in memory, then use \zs to simplify substitute
# 查找需替换的文本,保存,然后使用 \zs 命令简单替换
:%s/"\([^.]\+\).*\zsxx/\1/
# Pull word under cursor into LHS of a substitute
# 不懂
:nmap <leader>z :%s#\<<c-r>=expand("<cword>")<cr>\>#
# Pull Visually Highlighted text into LHS of a substitute
# 不懂
:vmap <leader>z :<C-U>%s/\<<c-r>*\>/
----------------------------------------
# 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 (see tip 227)
# 全局命令显示(参见 tip 227)
:g/fred.*joe.*dick/ : display all lines fred,joe & dick
显示所有包含fred,joe 和 dick 的行
:g/\<fred\>/ : display all lines fred but not freddy
显示所有全字匹配 fred 的行
:g/<pattern>/z#.5 : display with context
显示上下文
:g/<pattern>/z#.5|echo "==========" : display beautifully
显示得很漂亮
:g/^\s*$/d : delete all blank lines
删除所有的空行
:g!/^dd/d : delete lines not containing string
删除所有行首不是 dd 的行
:v/^dd/d : delete lines not containing string
同上
:g/fred/,/joe/d : not line based (very powerfull)
并不基于行(非常强大)
:g/{/ ,/}/- s/\n\+/\r/g : Delete empty lines but only between {...}
删除在 {...} 只见的空行
:v/./.,/./-1join : compress empty
...
read more »