Silly VIM question

Status
Not open for further replies.

snotnose

Ars Praefectus
3,454
Subscriptor
I've got a list of stuff. Fairly often I want to search for things like ^Wfoo, ^Wbar, ^Wblatz. In other words, they all start with ^W.

Is there a way in VIM that I can put it into a mode where when I use / to search, it will prepend ^W in front of my query? It needs to be a switchable mode, I do this kind of search in batches, then I want search to work as normal.

Yeah, I could do it in a macro. But then I have to find an unused key and remember how to make a macro.

Lazy minds want to know.
 

snotnose

Ars Praefectus
3,454
Subscriptor
I'm not talking about control-W. The lines I'm interested in start with W. /^W finds the next line starting with W. I'm lazy and getting tired of typing ^Wxyz, where x, y, and z are [0-9].

That is, to find W382 I want to type /382, not /^W382. I care about the line starting with W because for every line starting with Wxyz I care about, there are 10 lines with Wxyz in them I don't care about. FYI, lines starting with Wxyz define xyz, those containing Wxyz use xyz.

Stupid, I know. I should dig into macros, they'll probably do it.
 
Code:
:nmap / /^W
Then / followed by xyz. If you want it to also work from insert mode, add this mapping:
Code:
:imap / <ESC>/

Although in that case I would suggest not using '/' :) A function key (eg. <F3>) will work nearly the same except that you'll need to put the ^W in the imap as well. You're getting it from the nmap if you use / there. So, to sum it up, either:
Code:
:nmap / /^W
:imap / <ESC>/
or
Code:
:nmap <F3> /^W
:imap <F3> <ESC>/^W

You remove the mapping with :unmap and :iunmap.
 
Status
Not open for further replies.