Vim Tips Wiki
Register
(Comments on John Beckett's proposal for putting the pattern in the search history)
(→‎Comments: reply + my new attempt)
Line 102: Line 102:
   
 
Any thoughts, John?
 
Any thoughts, John?
  +
  +
----
  +
Thanks for pointing out the problem. Sorry, but somehow I missed seeing your edit, so my reply is delayed.
  +
  +
When I tried your new solution, I find that selecting 'something' and pressing * puts two items in the search history: 'something' (or '\Vsomething') and '//'. I don't like the resulting confusion, for example, when using /<Up> to locate the item-before-last that I searched for.
  +
  +
The char2nr code is sensational, but as you say, the search history is too unreadable for my preference.
  +
  +
There are some quirks I wouldn't worry about, for example, Vim itself has a quirk:
  +
<pre>
  +
/a?b<CR> (search forwards for 'a?b' -- good)
  +
?<Up><CR> (search backwards for 'a\?b' -- good)
  +
/<Up><CR> (search forwards for 'a\?b' -- bad, finds 'b' or 'ab')
  +
</pre>
  +
  +
So, here is what I an currently trying:
  +
<pre>
  +
function! s:VSetSearch(cmd)
  +
let temp = @@
  +
normal! gvy
  +
if @@ =~# '^[0-9A-Za-z ,_]*$'
  +
let @/ = @@ " keep simple cases simple in search history
  +
else
  +
" Escape both cmd and backslash because we're using @/ as a
  +
" temp variable that will be inserted into a '/' or '?' command.
  +
let @/ = '\V' . substitute(escape(@@, a:cmd.'\'), '\n', '\\n', 'g')
  +
endif
  +
let @@ = temp
  +
endfunction
  +
vnoremap * :<C-u>call <SID>VSetSearch('/')<CR>/<C-r>/<CR>
  +
vnoremap # :<C-u>call <SID>VSetSearch('?')<CR>?<C-r>/<CR>
  +
vmap <kMultiply> *
  +
</pre>
  +
  +
Please tell me what you think. --[[User:JohnBeckett|JohnBeckett]] 09:11, 16 August 2008 (UTC)
  +
  +
----

Revision as of 09:11, 16 August 2008

Tip 171 Printable Monobook Previous Next

created December 2, 2001 · complexity basic · author Raymond Li · version 5.7


With the following, you can use * (or #) to search forwards (or backwards) for the current visual selection from either characterwise visual mode or linewise visual mode (but not from blockwise visual mode). These visual searches behave like any other searches; the 'n' and 'N' commands work as they should, and the search history correctly records each search. This solution works for all characters, and even for searches that span multiple lines (that is, if you select "a" at the end of one line and "b" at the beginning of the next, we'll only find other lines that end in "a" and have "b" as the first character on the next line).

" vsearch.vim
" Visual mode search
function! s:VSetSearch()
  let temp = @@
  norm! gvy
  let @/ = '\V' . substitute(escape(@@, '\'), '\n', '\\n', 'g')
  let @@ = temp
endfunction

vnoremap * :<C-u>call <SID>VSetSearch()<CR>//<CR>
vnoremap # :<C-u>call <SID>VSetSearch()<CR>??<CR>

This code first defines a function, only accessible inside the script it's defined in (:help s:), which first backs up the unnamed register (:help expr-register) so it we can restore it, then yanks the visual selection into the unnamed register (:help :norm :help gv :help y). Then, it takes that string and escapes all \ to \\, and replaces every newline with a pattern that matches newlines (:help escape() :help substitute()), and finally stores the resulting string to the search pattern register (:help registers), prepended with \V to turn off the special meanings of all characters but \ (which we already escaped all instance of) (:help /\V). Finally, the function restores the unnamed register to the value it had when we started. The visual map for * presses : in visual mode, then <C-u> in command line mode to remove the '<,'> that : inserts in visual mode (:help c_CTRL-u). Then, it calls the function to set up the search pattern register (:help :call :help <SID>), and then searches forwards for the next instance of the pattern (:help /<CR>). The visual map for # does the same, but searches backwards instead (:help ?<CR>).

You can either put this code into a file in your plugins directory (make sure the filename ends in '.vim') or include it directly in your vimrc. To make the * key on the numeric keypad also trigger this mapping, you can add this line:

vmap <kMultiply> *

Comments

 TO DO 
Tips related to visual searching (need to merge):


One thing I don't like about the tip is that using it to search for something fails to put that something in the search history. Therefore, I can't press / followed by some up arrows to find what I searched for a few minutes ago.

Some approaches do not use \V but prefer to escape all the magic characters (I think '\.*$^~[]'). I like the \V (seems more robust), but I wanted to avoid having the '\V' in search history for simple searches.

I think the following fixes these points. I'm going to try this for a while and will, if ok, use this to replace the tip. Please add any comments below.

function! s:VSetSearch()
  let temp = @@
  normal! gvy
  if @@ =~# '^[0-9A-Za-z ,_]*$'
    let @/ = @@  " keep simple cases simple in search history
  else
    " Escape both slash and backslash because we're using @/ as a
    " temp variable that will be inserted into a '/' command.
    let @/ = '\V' . substitute(escape(@@, '/\'), '\n', '\\n', 'g')
  endif
  let @@ = temp
endfunction
vnoremap * :<C-u>call <SID>VSetSearch()<CR>/<C-r>/<CR>
vnoremap # :<C-u>call <SID>VSetSearch()<CR>?<C-r>/<CR>
vmap <kMultiply> *

Definitely not ok. That breaks pretty easily; try doing # after selecting "a?b" and you'll see that only "a" was searched for.

This works a little better:

function! s:VSetSearch()
  let temp = @@
  norm! gvy
  if @@ =~# '^[0-9A-Za-z ,_]*$'
    let @/ = @@
  else
    let @/ = '\V' . substitute(escape(@@, '\'), '\n', '\\n', 'g')
  endif
  let @@ = temp
  call histadd('/', @/)
endfunction

vnoremap * :<C-u>call <SID>VSetSearch()<CR>//<CR>
vnoremap # :<C-u>call <SID>VSetSearch()<CR>??<CR>

but it still has some quirks with handling / and ?... Consider, for example, selecting "a?b", pressing * (searches for "a?b"), pressing ?<Up><CR> (uses search "\Va\?b"; so matches "a?b"), then pressing /<Up><CR> (searches for "\Va\?b"; and \? behaves differently in a / search than a ? search - it now matches either "ab" or "b").

Changing the histadd() call to the following seems to work, but it yields an uglier pattern...

call histadd('/', substitute(@/, '[?/]', '\="\\%d".char2nr(submatch(0))', 'g'))

With that, we when searching on "a?b" or "a/b" we insert into the search history /\Va\%d63b/ and /\Va\%d47b/, respectively, which matches correctly but isn't terribly readable.

Any thoughts, John?


Thanks for pointing out the problem. Sorry, but somehow I missed seeing your edit, so my reply is delayed.

When I tried your new solution, I find that selecting 'something' and pressing * puts two items in the search history: 'something' (or '\Vsomething') and '//'. I don't like the resulting confusion, for example, when using /<Up> to locate the item-before-last that I searched for.

The char2nr code is sensational, but as you say, the search history is too unreadable for my preference.

There are some quirks I wouldn't worry about, for example, Vim itself has a quirk:

/a?b<CR>    (search forwards for 'a?b' -- good)
?<Up><CR>   (search backwards for 'a\?b' -- good)
/<Up><CR>   (search forwards for 'a\?b' -- bad, finds 'b' or 'ab')

So, here is what I an currently trying:

function! s:VSetSearch(cmd)
  let temp = @@
  normal! gvy
  if @@ =~# '^[0-9A-Za-z ,_]*$'
    let @/ = @@  " keep simple cases simple in search history
  else
    " Escape both cmd and backslash because we're using @/ as a
    " temp variable that will be inserted into a '/' or '?' command.
    let @/ = '\V' . substitute(escape(@@, a:cmd.'\'), '\n', '\\n', 'g')
  endif
  let @@ = temp
endfunction
vnoremap * :<C-u>call <SID>VSetSearch('/')<CR>/<C-r>/<CR>
vnoremap # :<C-u>call <SID>VSetSearch('?')<CR>?<C-r>/<CR>
vmap <kMultiply> *

Please tell me what you think. --JohnBeckett 09:11, 16 August 2008 (UTC)