My experience switching to buffers
About a year ago I stumbled upon an article explaining the difference between Vim tabs and buffers. The author emphasized that tabs are merely window layouts, and therefore one-file-per-tab idea I was used to at the moment just wasn’t proper. Instead, author suggested the use of buffer commands to switch between multiple files. I decided to give it a shot, and here are some ideas I would like to share after switching to the use of buffers.
Buffers are open files (they also may not be associated with any files), but they’re not necessarily visible at any given moment. In Vim, windows are not linked to any particular buffer, so you can easily cycle through buffers from within any window.
First and foremost, you probably want to add set hidden
to your .vimrc
.
This option lets you switch between buffers without having to save files.
At it’s basics, you only need few commands for operating buffers:
- Use
:ls
to list all buffers for this session. - To move between next and previous buffers use
:bn
and:bp
respectively. - Use
:b partial_buffer_name
for navigating to the buffer of your choice. Buffer name auto-complete is supported. - You can also use
:bN
, whereN
is a buffer number to jump to a specific buffer. - Get in a habit of closing buffers you will not use with
:bd
.
The hardest thing about stopping to use tabs and switching to buffers instead
is not having the visual aid: I was used to having a list of files always
available at a glance, at the top of my screen. To check what files I’m working
on now - I have to hit four keys: :ls
(fourth being “Enter”).
Not surprisingly, this taught me to be more mindful about my editing experience. I usually have a clear structure of the files I’m working on in my head. And if the list is getting to long to easily remember, then I’m probably doing something wrong: time to take a break and reset my Vim session.
The experience overall reminded me of my recent switch to blank keyboard key caps: with no inscriptions to aid you, I first felt a bit at loss, especially with they keys I couldn’t find without looking: like function keys or special symbols. But after some time with the blank key caps, I improved my typing skill, and know where even the most obscure characters hide. The switch helped me to improve my typing experience overall.
With Vim, it’s a similar story. After getting past the initial confusion, I achieved high level of awareness about my editing sessions. It didn’t make the editing process any faster, but instead much more satisfying.
But you already new that, since Vim isn’t really about speed.
UPDATE: Made a few corrections and added a :b partial_buffer_name
command
thanks to /u/___violet___’s Reddit comment.
10 read-only comments
These are the read-only comments I've exported from Disqus (which I no longer use). If you'd like to share your thoughts about this article, you can ✍️ Reply by email.
re. using :ls and :b in a comfortable, easy, fast way to switch buffers, I have this keymap in my Vim config: https://github.com/dubgeise...
True. Also, after submitting this post I found that vim-unimpaired supports ]b and [b for switching between buffers. Leader key does indeed feel quite natural for this though.
I also use CTRL-6 to switch between the active buffer and recent buffer.
My favorite way of interacting with vim buffers is via the CtrlP plugin http://kien.github.io/ctrlp... It makes life easier.
I like to use CtrlP as well, especially if I have an unmanageable number of buffers open. I do try to keep the number of open buffers down, reducing the clutter both in the list of buffers and in my head.
Didn't know about <c-6> at the time of writing. I should probably add it. It's quite an awkward combination, don't you think?
It is awkward, but you get used pretty quickly; it is a very handy command. I have remapped the command to something easier in the past, but lately I've being forcing myself to use vanilla VIM so I don't have to customize it too much on foreign computers ;-)
I'm still trying to convert myself to navigating *more* with just buffers and only using windows/tabs for organizing different persistent projects and viewing segments of files/similar files side by side. It's slow going, especially when I need to multitask among two or more projects at once. I'm hopeful though!
Similarly, working more with registers (especially for working with recorded macros!) and q/ and q: has had an amazing impact on my vim productivity.
Agreed. It does sound pretty hard - working with multiple projects in multiple tabs (since your buffers are mixed up). What I usually do - I work on separate projects in separate tmux windows, so that there's no bleed-through in my Vim sessions. This makes working with buffers so much more manageable.
I use CtrlP at this point too. I originally switched to it out of frustration getting CmdT to work easily and repeatably on Windows, plus this does away with the Ruby dependency (I think that's what it used )
CtrlP is great for navigating an entire project of files (like CmdT and the TextMate that inspired it) but also works with MRU files in addition to functionality you covered.
I only wish it was slightly smarter, in that sometimes it will find no close match in one mode and switch to some bizarre non-match before I have to manually change modes (check :h CtrlP for info on this )
One other thing I'm glad it respects is globbing/path expansion, using built in vim specifiers and I think a more detailed custom format as well. You can also explicitly ignore certain path expansions, such as avoiding searching through arbitrarily deeply-nested dependency packages/modules/gems/eggs/whatever.
Hasn't been mentioned here I don't think, but in addition to toggling via <c-6> you can navigate back and forth through buffer history for a window via <c-i> and <c-o>
Also, definitely check out the combo of session.vim and vim's built-in history file functionality, which if configurable. It's so awesome to be able to reopen a file days or seems later and be able to undo dozens of edits as needed. Vim will act like you never :q'd in the first place!