[vim]Best of Vim Tips
最编程
2024-02-29 12:22:36
...
zzapper's Tips Page
------------------------------------------------------------------------------
__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
/[^0-9]\|^[^0-9]\|^%.*% : Search for absence of a digit or beginning of line
/.*fred\&.*joe : Search for FRED AND JOE in any ORDER!
/\<fred\>/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
# finding empty lines
/^\n\{3} : find 3 empty lines
# 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
/bugs_.*bunny : bugs followed by bunny anywhere in file
: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
?http://www.vim.org/ : search BACKWARDS!!! clever huh!
----------------------------------------
#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/<c-r>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/
# 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 @*<Bar>sil exec 'g@<input\<Bar>select\<Bar>textarea\<Bar>/\=form\>@p'<Bar>redir END<CR>
# decrement numbers by 3
:%s/\d\+/\=(3-submatch(0))/
# 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
:%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)
:g/fred.*joe.*dick/ : display all lines fred,joe & dick
:g/\<fred\>/ : display all lines fred but not freddy
: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
: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/\S/d : Delete empty lines (both types)
:v/./.,/./-1join : 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)
: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
# 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 <c-a> as 5 characters)
:.,$g/^\d/exe "norm! \<c-a>": increment numbers
:'a,'bg/\d\+/norm! ^A : increment numbers
# storing glob results (note must use APPEND)
:g/fred/y A : append all lines fred to register 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
----------------------------------------
# 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
----------------------------------------
# Absolutely essential
----------------------------------------
* # g* g# : find word under cursor (<cword>) (forwards/backwards)
% : match brackets {}[]()
. : repeat last modification
matchit.vim : % now matches tags <tr><td><script> <?php etc
<C-N><C-P> : word completion in insert mode
<C-X><C-L> : Line complete SUPER USEFUL
/<C-R><C-W> : Pull <cword> onto search/command line
/<C-R><C-A> : Pull <CWORD> onto search/command line
:set ignorecase : you nearly always want this
------------------------------------------------------------------------------
__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
/[^0-9]\|^[^0-9]\|^%.*% : Search for absence of a digit or beginning of line
/.*fred\&.*joe : Search for FRED AND JOE in any ORDER!
/\<fred\>/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
# finding empty lines
/^\n\{3} : find 3 empty lines
# 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
/bugs_.*bunny : bugs followed by bunny anywhere in file
: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
?http://www.vim.org/ : search BACKWARDS!!! clever huh!
----------------------------------------
#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/<c-r>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/
# 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 @*<Bar>sil exec 'g@<input\<Bar>select\<Bar>textarea\<Bar>/\=form\>@p'<Bar>redir END<CR>
# decrement numbers by 3
:%s/\d\+/\=(3-submatch(0))/
# 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
:%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)
:g/fred.*joe.*dick/ : display all lines fred,joe & dick
:g/\<fred\>/ : display all lines fred but not freddy
: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
: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/\S/d : Delete empty lines (both types)
:v/./.,/./-1join : 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)
: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
# 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 <c-a> as 5 characters)
:.,$g/^\d/exe "norm! \<c-a>": increment numbers
:'a,'bg/\d\+/norm! ^A : increment numbers
# storing glob results (note must use APPEND)
:g/fred/y A : append all lines fred to register 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
----------------------------------------
# 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
----------------------------------------
# Absolutely essential
----------------------------------------
* # g* g# : find word under cursor (<cword>) (forwards/backwards)
% : match brackets {}[]()
. : repeat last modification
matchit.vim : % now matches tags <tr><td><script> <?php etc
<C-N><C-P> : word completion in insert mode
<C-X><C-L> : Line complete SUPER USEFUL
/<C-R><C-W> : Pull <cword> onto search/command line
/<C-R><C-A> : Pull <CWORD> onto search/command line
:set ignorecase : you nearly always want this
推荐阅读
-
Linux 工具 vim 概念、操作和基本命令说明
-
使用 Linux 操作系统的基本开发工具 - vim、gcc/g++、MakeFile、gdb、yum
-
Linux 学习 sudo 和 vim 配置
-
使用 apt-get 安装 telnet、curl、ifconfig、vim、ping 和其他工具的 Linux 环境 [翻译
-
离线安装vim
-
Linux]Linux 环境基本工具(yum、vim)的基本使用和配置(继续)
-
Linux yum 和 vim 的使用 - I. yum 的使用
-
用 vim 解压各种格式文件
-
Linux] centos 7 vim 默认一个制表符键相当于 8 个空格 -> 改为 4 个空格 - 四种解决方案
-
Linux 编辑器 -vim 的使用