-1

I'm not sure if I'm asking this right (I think this is a buffer thing?) so here is the scenario (in gherkin) As a vim user, i would like to use the silver searcher to search for files which have keywords and be able to go back and forth between the files i have visted:

given user launched vi
and user searched for a term using silver searcher
and search results appeared in the quick help window
enter image description here and user selected one of the files
and that file loaded

question: how can the user go back to the original file that was already loaded before they jumped to the new file? and supposing they did go back, how can they go forward back to the file they just searched?

  • what's so bad about the question? – abbood Nov 16 '17 at 15:34
1

Jumplist

You can use <c-o> / <c-i> to travel back and forward through the jump list. You can see the jump list via :jumps. However this my not be the fastest way to get back as you may have jumped a few times.

Alternative file

You can use <c-6> / <c-^> / :b # to switch to the alternative buffer. This is typically the previous buffer. You can see which buffer is the alternate buffer by looking for the buffer marked with # in the buffer list via: :ls / :buffers

This only works if the previous buffer is the one you want to go to or if you know the buffer number you want to go to (e.g. 5<c-6>).

Use file marks

You can set an upper case mark (file marks) which will allow you to jump between files. Simply set it before you search mM, then after you search jump via 'M.

The downside with this is you must set the mark before you do your searching.

Splits

If you know you are going go back to your original buffer use a split to view your search result (maybe via <c-w><cr> in the quickfix window) and keep your original buffer open. That way all you need to do is close your search buffer (possibly the quickfix window as well, :cclose).

Once again this requires you to know in advance that you will be going back.

Advanced buffer navigation

You can use :b to directly jump to the buffer you desire. Use :b {partial-file-name} to jump back. You can also use tab completion (<c-d> to list completions) and globs (e.g. :b **/foo*.bar).

A common mapping for buffer navigation:

nnoremap <leader>b :ls<cr>:b<space>

For more help see

:h jump-motions
:h alternative-file
:h mark-motions
:h :b
:h :ls
:h CTRL-6
:h c_CTRL-D
:h cmdline-completion
1

You can use :bpre and :bnext to go to previous buffer and next one.

  • How can I view/list all the open buffers? – abbood Nov 15 '17 at 7:24
  • :buffers will help. :h buffers for more. – Tuyen Pham Nov 15 '17 at 7:25

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.