tags:

views:

89

answers:

4

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.

A: 

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

"1yw :!grep @1

ThePosey
The last search string is already in the / register
Dave Kirby
A: 

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>
John Weldon
This did not work for me using vim 7.2 on windows.
Tim Henigan
yes, probably because ack and grep don't handle the `\<` and the `\>` the same..
John Weldon
you can do: `:! echo @/` to see what you have to work with.
John Weldon
I do exactly that (`:! echo @/`), but the output of echo is literally '@/'.
Tim Henigan
Anyway to get the text, without the word boundaries?
Paul Wicks
@Tim Henigan, my mistake, `:echo @/` will show you the contents of the `/` register...
John Weldon
+3  A: 

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.

Tim Henigan
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
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
@Paul Wicks: See my update regarding the `g*` search command.
Tim Henigan
+1  A: 
rampion
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
Another option is to use the `g*` search command rather than `*`. See my updated answer for details: http://stackoverflow.com/questions/2739135/how-to-retrieve-the-currently-searched-for-text-in-vim/2739223#2739223
Tim Henigan
`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
@nelstrom: I'm not remapping `CTRL-R`, I'm remapping `CTRL-R /`. All the other `CTRL-R` commands still work.
rampion