1

I opened 3 files by:

vim compile.f rtl.f tb.f -O

enter image description here

I navigate to a window of interest, which is rtl.f, press gf to open file below cursor and bf to go back rtl.f window.

enter image description here

After pressing gf, the window returned is not rtl.f, instead it's compile.f.

enter image description here

Why does it works this way? If I want to return to exactly where I left, what command should be used in Vim?

1
  • Are you asking about :bfirst? I’m not aware of a normal mode bf command.
    – D. Ben Knoble
    Commented May 23 at 16:25

2 Answers 2

0

The bf action load the first buffer which is probably not (unless you are lucky) the one that you just left.

The Ctrl-o solution

I would do: Ctrl o.

But if you did several motions in the new buffer you need to repeat the Ctrl o. If you would like to address that problem you could be interested to the following answer

The :Bd solution

With the vim-bbye plugin you can also do :Bd that deletes the current buffer but keep the split layout. You will then be back to your original buffer.

4
  • hello, I tried <Ctrl o> and it works as I expected. Thank you for your support. Commented May 23 at 5:53
  • Thanks for your feedback :-) Commented May 23 at 6:02
  • 1
    Probably not? (Small typo?)
    – D. Ben Knoble
    Commented May 23 at 16:26
  • Thanks for the review. I'm impressed how you manage to detect this small words errors :-) Commented May 23 at 17:08
1

When you do:

$ vim compile.f rtl.f tb.f -O

Vim creates one buffer for each file: compile.f, rtl.f, and tb.f and adds them to a global buffer list, generally in the order you gave at the command-line, which makes compile.f the first buffer in the list.

The command you use to go back to the previous buffer, :help :bfirst, actually—and unsurprisingly—goes to the first buffer in the buffer list, which is compile.f. So, basically, everything is working as it should and Vim is doing exactly what you tell it to do.

If you want to go back to the previous buffer after a gf, you can:

  • Use :help ctrl-o, as suggested in the other answer.

    Note that, technically, this goes to the location before the latest :help jump. If you didn't "jump" within the buffer opened via gf, then this should instantly lead you back to rtl.f. If you did, then you might have to press <C-o> a few times to finally go back to the desired buffer.

    I don't recommend <C-o> for this use case because "go back to previous buffer" is not its primary mission, which makes it non-deterministic.

  • Use :help ctrl-^ to go to "the alternate file".

    As explained in the doc, "the alternate file" is usually also "the previous buffer", so in the general case, pressing <C-^> (or <C-6> on some keyboards) after a gf should bring you back to where you were before the gf.

    I recommend <C-^> because it does exactly what you want.

    Note that this works a bit like a toggle. Once you have pressed <C-^> to come back after gf, you can press <C-^> again and again to alternate between the two buffers, which is pretty neat.

2
  • Hello @romainl, I solved my problem with ctrl+o. But your info about Ctrl+^ is really informative. Thank you. Commented May 23 at 6:11
  • <C-o> is not really a solution but oh well…
    – romainl
    Commented May 24 at 7:18

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.