20

When I edit multiple files I oftentimes want to go back and forth between the last edited files. I know about :bn(ext) and :bp(revious) to switch between buffers but they don't keep the history of the last used files. Instead they use the order in which the files were opened. E.g., if I opened the files in this order

A, B, C

an my navigation history would be

A, B, C, B

then :bprevious in the last buffer (B) would send me to A. Is there a command or plugin that would send me to C? I know of Ctrl-6, but this only switches between the last two buffers. I would like to go back and forth multiple buffers.

Ton van den Heuvel
  • 10,157
  • 6
  • 43
  • 82
lassej
  • 6,256
  • 6
  • 26
  • 34

2 Answers2

20

I have been wanting the same functionality for quite some time now as well. Your question inspired me to finally do something about it. Have a look at bufsurf, a small plugin that provides the required functionality. It provides the user with two commands:

:BufSurfBack 

to navigate backwards in history, and similarly:

:BufSurfForward

to navigate forwards in history. For each window or tab a separate navigation history is kept in memory. Please let me know if you experience any problems, I did not have the chance to test this extensively yet.

Ton van den Heuvel
  • 10,157
  • 6
  • 43
  • 82
  • @Ton van den Heuvel awesome!! Can you somehow reset the history? After a while, I've build up a large history of files and I want to start working on something else. Can I clear the history or close the window in some smart way? – Cotten Apr 09 '14 at 08:54
  • @Cotten, thanks! Resetting is not possible as far as I know. It is not terribly difficult to implement though. You can file a ticket or better even submit a pull request :) https://github.com/ton/vim-bufsurf – Ton van den Heuvel Apr 09 '14 at 11:00
5

Check out the lustyjuggler buffer plugin. It's awesome for this. It keeps the buffers in last used order and maps them to the home row keys so you can very quickly go back/forth between them.

http://www.vim.org/scripts/script.php?script_id=2050

You can see it demoed toward the end of my most recent vim screencast were I go over buffers: http://lococast.net/archives/185

Rick
  • 15,484
  • 5
  • 25
  • 29
  • Hi Rick, thanks for the answer, but I am looking for a solution where I don't have to switch to a different mode to move between recent buffers. Ideally I would be able to map Shift-H and Shift-L to the previous and next actions. – lassej Sep 14 '10 at 13:22
  • 1
    @lassej: Just find the commands from the plugin which do what you want, and map them to the keys you want them mapped to. – Cascabel Sep 14 '10 at 20:34