Take the 2-minute tour ×

For example, I have some code and I use "*" 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.

share|improve this question

4 Answers 4

up vote 1 down vote accepted

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
share|improve this answer
Awesome, this is just what I was looking for. I did modify your mapping a little bit to avoid having to hit the enter key twice: cmap <C-R>/ <C-R>=substitute(substitute(@/, '^\\<', '', ''), '\\>$', '', '') <CR> –  Paul Wicks Apr 29 '10 at 19:51
Another option is to use the g* search command rather than *. See my updated answer for details: stackoverflow.com/questions/2739135/… –  Tim Henigan Apr 29 '10 at 20:11
ctrl-r followed by / inserts the text from the search register, but if you were to do ctrl-r followed by any letter on the keyboard, it would insert the contents of the corresponding register. Besides the letters, there are other special registers, such as . (the last entered text), % (the current filename) etc. By remapping ctrl-r you lock away all of the potential useful shortcuts that can be constructed from these various registers. –  nelstrom Apr 29 '10 at 22:05
@nelstrom: I'm not remapping CTRL-R, I'm remapping CTRL-R /. All the other CTRL-R commands still work. –  rampion Apr 30 '10 at 14:55

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.

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.

share|improve this answer
This works, although when used in conjunction with * it also has the word boundaries. Is there anyway to get the text without them? –  Paul Wicks Apr 29 '10 at 17:11
The * search sets up the search pattern to include the word boundaries. So you can't use * to get the pattern without them. –  Tim Henigan Apr 29 '10 at 17:17
@Paul Wicks: See my update regarding the g* search command. –  Tim Henigan Apr 29 '10 at 20:10

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>


:!ack <cword>
share|improve this answer
This did not work for me using vim 7.2 on windows. –  Tim Henigan Apr 29 '10 at 16:58
yes, probably because ack and grep don't handle the \< and the \> the same.. –  John Weldon Apr 29 '10 at 17:00
you can do: :! echo @/ to see what you have to work with. –  John Weldon Apr 29 '10 at 17:01
I do exactly that (:! echo @/), but the output of echo is literally '@/'. –  Tim Henigan Apr 29 '10 at 17:08
Anyway to get the text, without the word boundaries? –  Paul Wicks Apr 29 '10 at 17:11

You can yank the word into a register and use the @regnum in the command mode line

"1yw :!grep @1

share|improve this answer
The last search string is already in the / register –  Dave Kirby Apr 29 '10 at 20:21

Your Answer


