jpmelos
jpmelos

Reputation: 3562

How can I map a search in Vim?

I'm a Python programmer, and, by convention, methods starting with a _ are private, and therefore should not be called outside of the class it belongs to.

I want a faster way for finding classes and public methods in Python (because I often have to dive into other people codes to investigate APIs), so I wrote a search, which is

/^    def [^_]\|^class 

This works great when typed directly into Vim, but of course that takes too long to type. I'd like to map it to a key (say, t, since I don't use it at all in Vim as it is now). So, I put this in my ~/.vimrc:

nmap t /^    def [^_]\|^class 

But it's not working: when I hit the t, Vim searches for the string as it is, instead of parsing the special characters (which are, in this case, ^, [^_] and \|).

Anyone knows how to write this mapping? I tried my luck in Vim Wikia, Google and all sorts of sources I could think of, no luck so far.

Thank you!

Upvotes: 1

Views: 251

Answers (1)

Anton Kovalenko
Anton Kovalenko

Reputation: 21517

You have a problem with \|: one layer of escaping is removed when :nmap itself is read, so you need another layer: use \\ to say \.

nmap t /^    def [^_]\\|^class
# or maybe add a final <CR> key
nmap t /^    def [^_]\\|^class<CR>

Upvotes: 3

Related Questions