How to retrieve the currently searched for text in vim?
For example, I have some code and I use "*开发者_JAVA技巧" to search for something within that code. Not finding what I want in that file, I'd like to use something like ack
or grep
to search for it within the local directory. I know I can do :! ack whatever
to do the search from within vim, but what I'd like to know is is there a way to replace whatever
with the currently searched for word within vim.
You can use Ctrl-r followed by / to insert the last-search register.
:!grep <Ctrl-r> / file_list
See this Vim Tips Wiki entry for more info.
Update:
The *
search command command always includes word boundaries.
However, the g*
search command behaves the same as *
, but without word boundaries.
This could be used to work around your issue rather than using the custom macro in rampion's answer.
Riffing off of Tim Henigan's answer, put this in your .vimrc
cmap <C-R>/ <C-R>=substitute(substitute(@/, '^\\<', '', ''), '\\>$', '', '')
Now when you hit CTRL-R /
in command line mode, it will drop the word boundry markers, so you should be able to use
:!grep CTRL-R / file-list
I haven't found a way to (easily) pass the contents of a search register to an external program, without resorting to key mappings and eval statmements.
However, you can use <cword>
to pass the word currently under the cursor to an external program:
:!echo <cword>
or
:!ack <cword>
You can yank the word into a register and use the @regnum in the command mode line
"1yw :!grep @1
精彩评论