Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting Buffers in Vim

Is it possible to reset the alternate buffer in a vim session to what it was previously?

By alternate buffer, I mean the one that is referred to by #, i.e. the one that is displayed when you enter cntl-^.

Say I've got two files open main.c and other.c and :ls gives me:

  1 %a   "main.c"              lines 27
  2 #    "other.c"             lines 56

Say I open another file, e.g. refer.c, :ls will now give me:

  1 %a   "main.c"              lines 27
  2      "other.c"             lines 56
  3 #    "refer.c"             lines 125

If I delete the buffer containing refer.c, :ls now shows:

  1 %a   "main.c"              lines 27
  2      "other.c"             lines 56

But if I do a cntl-^, refer.c will be displayed again!

Is there some way to get vim to reset the alternate buffer back to what it last was automatically? A "history" of alternate buffers?

Or am I stuck with doing a :2 b to reload other.c into the alternate buffer?

Or maybe there is a good reason for this behaviour?

like image 629
Rob Wells Avatar asked Jan 24 '23 02:01

Rob Wells


1 Answers

In this case, "alternate" just means "previous". So, yes, :b2 (or 2 ctrl-6) is probably the easiest way to change which two buffers will be toggled by ctrl-6.

Also, take a look at the :keepalt command.

like image 166
graywh Avatar answered Jan 28 '23 05:01

graywh