vimtricks.wiki Concise Vim tricks, one at a time.

How do I close a buffer without closing the window?

Answer

:bp | bd #

Explanation

The :bp | bd # command switches to the previous buffer and then deletes the alternate buffer. This effectively closes a buffer while keeping the window open.

How it works

  • :bp switches to the previous buffer
  • | chains commands
  • :bd # deletes the buffer you just left
  • The window remains open showing the previous buffer

Example

You are editing test.go in a split and want to close it without closing the split:

:bp | bd #

The split now shows the previous buffer instead.

Tips

  • :bd alone closes the buffer and the window if it is the only one
  • :bw wipes the buffer completely (removes from buffer list)
  • :set hidden allows switching buffers without saving
  • Consider using :enew to create a new empty buffer in the window

Next

How do you yank a single word into a named register?