While editing huge files, I need to move around based on pattern search (despite me using ctags, cscope, Lid) but I don't want to disturb the current highlight search while doing so.

The reason being the pattern I am going to use is for extremely short period of time and I would have to manually restore the last pattern back again.

Let me explain by example. If I have a file like following:

  1 #include <stdio.h>
  3 int main(int argc, char **argc)
  4 {
  5         printf("Hello World")
  6         foo();
  7         return 0;
  8 }
 10 void foo(void)
 11 {
 12         printf("Just another function with no declaration yet\n");
 13 }

lets say cursor is at line 5. printf("Hello World") and my current search pattern is printf.

Now I want to do add the prototype for the function foo(). I would have to do following:

  1. Search upwards for #include to get the last instance of #include in the file
  2. Add the prototype for foo in the next line
  3. Restore the last search pattern i.e. printf

What I want is when I had moved to line 1 by searching for #include, it should just move to the occurrence without changing the current pattern.

Is it possible to acheive this in vim easily?

TLDR I want to move/jump within a file, based on pattern without changing the current pattern so that the next time I do n or N the old pattern still works without having to restore it by doing / then uparrow


You can use these custom mappings to search upward and downward without "polluting" the search history (used by nN) or removing the highlight from the preceding "normal" search:

nnoremap ,/ :<C-u>call search(input("/"))<CR>
nnoremap ,? :<C-u>call search(input("?"),"b")<CR>

Overriding the normal / and ? doesn't sound to me as a viable idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.