Paritosh Piplewar
Paritosh Piplewar

Reputation: 8132

How to create search shortcut in vim

Most of the time I do some regular search like anything that starting with def,it should take my cursor to that place, so /def <search character> . I am thinking to create some shortcut.

I tried doing this

command # /def

with a hope that if I do ESC# it will type /def<space>, but it doesn't.

It is throwing error

E182: Invalid command name

How can i do that ?

Upvotes: 0

Views: 509

Answers (2)

Peter Rincker
Peter Rincker

Reputation: 45157

An alternative to using / to search is to use ctags. Using tags lets you do the following:

  • jump to definition under cursor via <c-]>
  • <c-w><c-]> is the same as <c-]>, but open definition in new split window
  • :tag {tag_name} will jump to the definition of {tag_name}
  • :tag uses completion so <tab> and <c-d> can help you type less
  • :tag can use a regex to match part of a tag. e.g. :tag /foo (use completion with this also)
  • Use <c-t> to jump back after you visit a definition (It pops the tag stack)
  • Look into Tim Pope's Effortless Ctags with Git article for easy tag file generation

For a quick mapping use the following:

nnoremap <leader>t :ta<space>

More alternatives:

  • Look into cscope for more defintion querying fun. See :h cscope
  • jedi-vim is a popular python completion plugin. It has go to definition support as well
  • Use :vimgrep/:grep/ack/ag/git-grep to search across several files when tags are not available
  • Use gd (go to definition) for simple cases. See :h gd
  • Look into alternative tag generators like GNU Global or language specific tag generators. e.g jstags

For more help please see the following:

:h tags
:h ctrl-]
:h ctrl-w_ctrl-]
:h :tag
:h ctrl-t
:h :vimg
:h :grep
:h gd
:h cscope

Upvotes: 1

Kent
Kent

Reputation: 195229

you are looking for map not command. try this:

nnoremap <F3> /def<space>

I didn't map #, instead I used <F3>, since # is very useful in normal mode. you can use # if you like.

Upvotes: 2

Related Questions