Tabs, Windows, and Buffers in Vim

Posted on Nov 6, 2024

I move back and forth between neovim and vscode frequently, depending on the day of the week, which way the wind is blowing, and a litany of other things including how I feel like my system is behaving, even if that is something that is completely subjective. I found myself struggling to really grasp the keystrokes required to manage my split panes, so, this post is 50% education for you, and 50% a reminder for me.

Tabs in vscode are like tabs in your web browser, execpt for files – when that tab is open, the file is open, and when you close that tab, the file goes away. There’s a bit of a one-to-one relationship that is fairly easy to grok. Vim’s relationship with tabs is a bit different and can be really frustrating at times because it behaves so differently than tabs anywhere else.

Vim has three layers of view abstractions: tabs, windows, and buffers.

Tabs

A tab in vim is a collection of one or more windows, allowing you to group windows usefully. If you were working on two components of the same application – like a client program and a server program – you can have the files for the client program open in the first tab, and files for the server program open in another. You can switch back and forth bwetween those tabs using :tabn and :tabp.

Windows

A window in vim is a view onto a single buffer. Opening a new window with :split or :vsplit allows you to include a filename in the call, opening up a file into a new buffer, and then opening up that buffer as a view. A buffer can be viewed in multiple windows within a single vim session – I like to use this when doing peer reviews, being able to specifically call out sections of code in specific buffers that I have questions about or want more clarification on.

Control+W gives you the “windows command mode”, allowing the following modifiers:

  • Control+W + R - To rotate windows up/left.
  • Control+W + r - To rotate windows down/right.
  • Control+W + L - Move the current window to the “far right.”
  • Control+W + H - Move the current window to the “far left.”
  • Control+W + J - Move the current window to the “very bottom.”
  • Control+W + K - Move the current window to the “very top.”
  • Control+W + W - Toggle between open windows and.

Buffers

You can think of a buffer as an open file, that may or may not be visibile on the current screen. When you open a file in vim, that file gets placed into a buffer, and it’s the only buffer there on startup. If this buffer is unmodified, and you open another file with the :edit command, that buffer goes into the background and you start working with the new file. You can get a list of all open buffers open with the :ls command, and then switch to that buffer by using :b<number>. The previous buffer only goes away when you delete it by either :quitting out or by using the :bdelete command, keeping in mind that it’s recoverable with the :ls! and :b<number> commands until you use the :bwipe command.

You can get a quick list of the buffers open in a Vim session with :ls. This all means that when most people think of tabs in more familiar text editors, the idea of a buffer in Vim is actually closest to what they’re thinking.