VIM直接查询选中文本

来源:互联网 发布:五年级简便算法公式 编辑:程序博客网 时间:2024/05/22 02:07

在使用Vim的时候会经常用到查询功能,Vim默认的查询往往需要你输入要查询的字符串,要查询的字符串明明已经在文件里面了还要在输入一遍 真是麻烦啊。我就想Vim能不能像某些文本编辑器一样,选中一些文本,查询的时候自动把文本拷贝到查询框。Goolge了一下,果真发现有一个这样的vim tip:

 

http://vim.wikia.com/wiki/VimTip171

 

他的主要功能有:

 

  • Press * to search forwards for selected text, or # to search backwards. - 用*向前查询选中词,用#向后查询
  • As normal, press n for next search, or N for previous. As normal, press n for next search, or N for previous. - n/N照常
  • Handles multiline selection and search. Handles multiline selection and search. (下面的不太用到,就不翻译了:))
  • Whitespace in the selection matches any whitespace when searching (searching for "hello world" will also find "hello" at the end of a line, with "world" at the start of the next line). Whitespace in the selection matches any whitespace when searching (searching for "hello world" will also find "hello" at the end of a line, with "world" at the start of the next line).
  • Each search is placed in the search history allowing you to easily repeat previous searches. Each search is placed in the search history allowing you to easily repeat previous searches.
  • No registers are changed. No registers are changed.
  • A global variable (g: VeryLiteral) controls whether selected whitespace matches any whitespace (by default, VeryLiteral is off, so any whitespace is found).
  • Type /vl to toggle VeryLiteral to turn whitespace matching off/on (assuming the default backslash leader key). Type / vl to toggle VeryLiteral to turn whitespace matching off / on (assuming the default backslash leader key).
  • When VeryLiteral is off, any selected leading or trailing whitespace will not match newlines, which is more convenient, and avoids false search hits. When VeryLiteral is off, any selected leading or trailing whitespace will not match newlines, which is more convenient, and avoids false search hits.

 

把下面的代码保存为vsearch.vim 放到~/.vim/plugins目录下,就可以使用了