Chinaunix首页 | 论坛 | 博客
  • 博客访问: 939043
  • 博文数量: 173
  • 博客积分: 3436
  • 博客等级: 中校
  • 技术积分: 1886
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-07 09:29
文章分类

全部博文(173)

文章存档

2016年(6)

2015年(10)

2014年(14)

2013年(8)

2012年(36)

2011年(63)

2010年(19)

2009年(17)

分类: C/C++

2014-05-09 13:04:19

寄存器操纵:
  "?nyy: 将当前行及其下n行的内容存储到寄存器?中,此中?为一个字母,n为一个数字 

  "?nyw: 将当前行及其下n个字存储到寄存器?中,此中?为一个字母,n为一个数字 

  "?nyl: 将当前行及其下n个字符存储到寄存器?中,此中?为一个字母,n为一个数字 

  "?p: 取出寄存器?中的内容并将其放到光标位置处。这里?可以是一个字母 ,也可以是一个数字 

  ndd: 将当前行及其下共n行文本删除,并将所删内容放到1号删除寄存器中 vi 中配置tab为4和自动转换成空格


一般在vim中我们使用这样的方法去替换:%s/dest/new/g ,其中dest是查找目标,new是替换的新内容,g表示全文替换,一般的vim配置为默认全文替换,所以g也可以不要。

下文总结了常用的一些替换模式,以供参考学习;内容转自网络,原文没有出处信息!

VIM中常用的替换模式总结。

0,:g/null/d

找到null的行并且删掉

1,简单替换表达式

替换命令可以在全文中用一个单词替换另一个单词:

:%s/four/4/g

“%” 范围前缀表示在所有行中执行替换。最后的 “g” 标记表示替换行中的所有匹配点。如果仅仅对当前行进行操作,那么只要去掉%即可

    如果你有一个象 “thirtyfour” 这样的单词,上面的命令会出错。这种情况下,这个单词会被替换成”thirty4″。要解决这个问题,用 “\<” 来指定匹配单词开头:

         :%s/\

显然,这样在处理 “fourty” 的时候还是会出错。用 “\>” 来解决这个问题:

         :%s/\/4/g

如果你在编码,你可能只想替换注释中的 “four”,而保留代码中的。由于这很难指定,可以在替换命令中加一个 “c” 标记,这样,Vim 会在每次替换前提示你:

         :%s/\/4/gc

2,删除多余的空格

要删除这些每行后面多余的空格,可以执行如下命令:

         :%s/\s\+$//

命令前面指明范围是 “%”,所以这会作用于整个文件。”substitute” 命令的匹配模式是

“\s\+$”。这表示行末($)前的一个或者多个(\+)空格(\s)。替换命令的 “to” 部分是空的:”//”。这样就会删除那些匹配的空白字符。

3,匹配重复性模式

星号项 “*” 规定在它前面的项可以重复任意次。因此:

         /a*

匹配 “a”,”aa”,”aaa”,等等。但也匹配 “” (空字串),因为零次也包含在内。星号 “*” 仅仅应用于那个紧邻在它前面的项。因此 “ab*” 匹配 “a”,”ab”,”abb”,”abbb”,等等。如要多次重复整个字符串,那么该字符串必须被组成一个项。组成一项的方法就是在它前面加 “\(”,后面加 “\)”。因此这个命令:

         /\(ab\)*

匹配: “ab”,”abab”,”ababab”,等等。而且也匹配 “”。

要避免匹配空字串,使用 “\+”。这表示前面一项可以被匹配一次或多次。

         /ab\+

匹配 “ab”,”abb”,”abbb”,等等。它不匹配 后面没有跟随 “b” 的 “a”。

要匹配一个可选项,用 “\=”。 例如:

         /folders\=

匹配 “folder” 和 “folders”。

4,指定重复次数

要匹配某一项的特定次数重复,使用 “\{n,m}” 这样的形式。其中 “n” 和 “m” 都是数字。在它前面的那个项将被重复 “n” 到 “m” 次 (|inclusive| 包含 “n” 和 “m”)。例如:

         /ab\{3,5}

匹配 “abbb”,”abbbb” 以及 “abbbbb”。

    当 “n” 省略时,被默认为零。当 “m” 省略时,被默认为无限大。当 “,m” 省略时,就表示重复正好 “n” 次。例如:

         模式            匹配次数

         \{,4}             0,1,2,3 或 4

         \{3,}             3,4,5,等等

         \{0,1}            0 或 1,同 \=

         \{0,}             0 或 更多,同 *

         \{1,}             1 或 更多,同 \+

         \{3}              3

 

5,多选一匹配

在一个查找模式中,”或” 运算符是 “\|”。例如:

         /foo\|bar

这个命令匹配了 “foo” 或 “bar”。更多的抉择可以连在后面:

         /one\|two\|three

匹配 “one”,”two” 或 “three”。

    如要匹配其多次重复,那么整个抉择结构须置于 “\(” 和 “\)” 之间:

         /\(foo\|bar\)\+

这个命令匹配 “foo”,”foobar”,”foofoo”,”barfoobar”,等等。

    再举个例子:

         /end\(if\|while\|for\)

这个命令匹配 “endif”,”endwhile” 和 “endfor”。

匹配一个单词

替换命令可以在全文中用一个单词替换另一个单词:

:%s/four/4/g

“%” 范围前缀表示在所有行中执行替换。最后的 “g” 标记表示替换行中的所有匹配点。如果仅仅对当前行进行操作,那么只要去掉%即可

  如果你有一个象 “thirtyfour” 这样的单词,上面的命令会出错。这种情况下,这个单词会被替换成”thirty4″。要解决这个问题,用 “\<” 来指定匹配单词开头:

       :%s/\

显然,这样在处理 “fourty” 的时候还是会出错。用 “\>” 来解决这个问题:

       :%s/\/4/g

如果你在编码,你可能只想替换注释中的 “four”,而保留代码中的。由于这很难指定,可以在替换命令中加一个 “c” 标记,这样, 会在每次替换前提示你:

       :%s/\/4/gc


上下文相关替换


除了直接使用一个单词(或短语)替换另一个,还有稍微复杂的全局替换语法。这些语法可以对一个模式进行搜索,一旦找到含有模式的行,就可以使用不同与模式的串进行替换,我们把这种替换叫做上下文相关替换

语法格式如下:
:g /pattern/s/old/new/g    将会把包含pattern的行中,把所有old替换为new
第一个g表示是在文件的所有行上执行的命令,模式pattern识别要发生替换的行。在那些包含模式pattern的行上,ex将把old替换(s)为new。最后的g表示在该行上进行全部替换。

如果用来进行搜索的模式与想要修改的模式相同,那么就不必重复它:
:g/string/s//new/g  等价于  :g/string/s/string/new/g
还要注意:
:g/string/s//new/g  也等价于 :% s/string/new/g
可以用第二种方式少输一些字符



点击(此处)折叠或打开

  1. " When started as "evim", evim.vim will already have done these settings.
  2. if v:progname =~? "evim"
  3. finish
  4. endif

  5. " This must be first, because it changes other options as a side effect.
  6. set nocompatible

  7. " allow backspacing over everything in insert mode
  8. set backspace=indent,eol,start

  9. syntax on
  10. set nobackup " do not keep a backup file, use versions instead
  11. set nowritebackup " don't keep backup before overwriting
  12. set hidden " can hide buffer

  13. set cindent " always set autoindenting on
  14. set cinoptions=:0,t0 " Don't indent CASE and Return Type

  15. set incsearch " do incremental searching
  16. set hlsearch " highlith match text
  17. set ignorecase smartcase " case sensitive with capital

  18. set history=50
  19. set sm " match ()
  20. set list
  21. set listchars=tab:\.\ ,trail:\ " display as '. '
  22. set ruler " show cursor position
  23. set showcmd " show command in ruler
  24. " set nowrap " no wrap newline

  25. " Jump to last known curson position.
  26. autocmd BufReadPost *
  27. \ if line("'\"") > 0 && line("'\"") <= line("$") |
  28. \ exe "normal! g`\"" |
  29. \ endif

  30. " Convenient command to see the difference between the current buffer and the
  31. " file it was loaded from, thus the changes you made.
  32. command Di vert new | set bt=nofile | r # | 0d_ | diffthis
  33. \ | wincmd p | diffthis

  34. " Command to reload the buffer
  35. command Re let position = getpos(".") | %d | r | 1d | call setpos(".", position)

  36. " vim -b : edit binary using xxd-format!
  37. augroup Binary
  38. au!
  39. au BufReadPre *.bin let &bin=1
  40. au BufReadPost *.bin if &bin | %!xxd
  41. au BufReadPost *.bin set ft=xxd | endif
  42. au BufWritePre *.bin if &bin | %!xxd -r
  43. au BufWritePre *.bin endif
  44. au BufWritePost *.bin if &bin | %!xxd
  45. au BufWritePost *.bin set nomod | endif
  46. augroup END

  47. " Start key map
  48. nmap gr :grep `find . -name '*.[chsS]'`
  49. " nmap ,e :e =expand("%:p:h") . "/"
  50. nmap ,e :e %
  51. " nmap :call setreg('+', @*)
  52. nmap :let @+=@*
  53. vmap "+y
  54. imap
  55. imap

  56. " map :source $VIMRUNTIME/folder.vim
  57. map @@
  58. map :!ctags -R .&
  59. map ^xx
  60. map ^i//
  61. map ^i## /* for ASM */
  62. map ^i%% /* for TeX */
  63. " map :cs reset
  64. " map :w:!if [ -f [Mm]akefile ]; then make xen >& /dev/null; fi
  65. " map :w:!if [ -f [Mm]akefile ]; then make >& /dev/null; fi
  66. " map :cc
  67. " map :cn
  68. map :w:make
  69. " map :if (&hlsearch == 1) set nohlsearch else set hlsearch endif

  70. set pastetoggle=
  71. imap
  72. imap
  73. imap
  74. imap
  75. imap
  76. imap
  77. " imap

  78. imap ``` printf("");

  79. set nocsverb " suppress error
  80. set cscopequickfix=s-,c-,d-,i-,t-,e-,g-
  81. " set cscopequickfix=s-,t-
  82. " cs add ~/tmp/cscope.out ~/tmp/src
  83. " cs add ~/tmp/cscope.out
  84. "cs add ~/wtos2/T10_check/WTOS_7.1.1_T10/src/cscope.out
  85. " cs add ~/wtos2/WTOS_7.1.1_T10/src/io/xusb/cscope.out
  86. cs add ~/wtos2/WTOS-main-check/WTOS-main/src/cscope.out

  87. " map :cs find g =expand("")
  88. " imap :cs find g =expand("")
  89. nmap s :cs find s =expand("")
  90. nmap g :cs find g =expand("")
  91. nmap c :cs find c =expand("")
  92. nmap t :cs find t =expand("")
  93. nmap e :cs find e =expand("")
  94. nmap f :cs find f =expand("")
  95. nmap i :cs find i =expand("")
  96. nmap d :cs find d =expand("")

  97. " Using 'ESC' then a search type makes the vim window
  98. " split horizontally, with search result displayed in
  99. " the new window.

  100. " map :scs find g =expand("")
  101. " imap :scs find g =expand("")
  102. nmap s :scs find s =expand("")
  103. nmap g :scs find g =expand("")
  104. nmap c :scs find c =expand("")
  105. nmap t :scs find t =expand("")
  106. nmap e :scs find e =expand("")
  107. nmap f :scs find f =expand("")
  108. nmap i :scs find i ^=expand("")$
  109. nmap d :scs find d =expand("")

  110. " Hitting 'ESC' *twice* before the search type does the serach
  111. " with quickfix list
  112. "

  113. nmap s :set cscopequickfix+=s-:cs find s =expand(""):set cscopequickfix-=s-
  114. nmap g :set cscopequickfix+=g-:cs find g =expand(""):set cscopequickfix-=g-
  115. nmap c :set cscopequickfix+=c-:cs find c =expand(""):set cscopequickfix-=c-
  116. nmap t :set cscopequickfix+=t-:cs find t =expand(""):set cscopequickfix-=t-
  117. nmap e :set cscopequickfix+=e-:cs find e =expand(""):set cscopequickfix-=e-
  118. nmap i :set cscopequickfix+=i-:cs find i ^=expand("")$:set cscopequickfixi=g-
  119. nmap d :set cscopequickfix+=d-:cs find d =expand(""):set cscopequickfix-=d-

  120. set csverb
  121. set cst "search in both tags and cscope.out
  122. set cspc=4 "display last 3 lines

  123. " Don't use Ex mode
  124. map Q

  125. " This is an alternative that also works in block mode, but the deleted
  126. " text is lost and it only works for putting the current register.
  127. vnoremap P "_dP

  128. " Switch syntax highlighting on, when the terminal has colors
  129. " Also switch on highlighting the last used search pattern.
  130. if &t_Co == 1
  131. syntax off
  132. set nohlsearch
  133. endif

  134. " Load vimgdb mapping
  135. set splitright
  136. set splitbelow
  137. run macros/gdb_mappings.vim

阅读(662) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~