2

I have a small question: let's suppose I'm at the beginning of line 10 and I search a term with the command /term_to_search and then I press <Enter> to start navigating search results with n or N. Then I want to go back to the location I were before starting the search (the beginning of line 10). How can I do? I know that the commands CTRL+O and CTRL+I jump back and forth the list of jumps, but how can I do it in just one keypress? Do I need to define a new special mark to do that (if so, how)?

Thanks in advance.

4 Answers 4

6

I do this by first creating a mark - for example, create a mark named "a" with ma in command mode – then search or move around the file however you want. Then go back to your mark with 'a.

3

Two options I can think of off the top of my head:

  1. :[v]split, then search in only one window
  2. :nnoremap / mz/; then `z should take you back. Only works for one search at a time (i.e. new searches overwrite the mark)
2

let's suppose I'm at the beginning of line 10

Then the easiest way back is 10G or :10. It's not that useless if you have line numbers on and your memory is not bad either.

Also, sometimes the command g; (go back one position in the change list) proves to be be useful.

0

If, contrary to my initial thinking, OP just wants a way to do what they describe manually (and the voting so far suggests that's most people are reading it that way) then this is overkill. But if one wants a transparent, automated method that allows return to search start any time w/o having to think about it ahead of time....this should help...

If we want to get a little fancier (though still down and dirty) we can create an autocommand and a mapping like so...

augroup searchorigin
  autocmd!
  au CmdLineLeave * let b:cmdtype = expand('<afile>') | if (b:cmdtype == '/' || b:cmdtype == '?') | let b:searchorigin = getpos(".") | endif
augroup END

nnoremap <leader>/ :exec 'call setpos(".", b:searchorigin)'<CR>

The autocommand will record the starting point of your current search in a buffer-local variable, allowing this to work simultaneously on different searches occurring in different buffers. The mapping will move the cursor back to that point using that variable.

So just start a search as you always do and then hit \/ and you'll be back to the start point. (Replace <leader>/ in the mapping with whatever free key(s) you desire.)

4
  • Registers != marks, and marks are buffer local for the lowercase.
    – D. Ben Knoble
    Commented Jul 18, 2019 at 12:39
  • @D.BenKnoble Umm. Okay. Who said otherwise?
    – B Layer
    Commented Jul 19, 2019 at 11:39
  • read your last points as counterarguments to the mark answer. I guess you know what they say when you assume 😅
    – D. Ben Knoble
    Commented Jul 19, 2019 at 12:41
  • My first (ill advised) inclination was to store getpos() result in a register...that's the origin of that bullet point (which is kind of superfluous in retrospect).
    – B Layer
    Commented Jul 19, 2019 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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