Best workflow using Vim buffers

418 views Asked by At

In the last 2-3 years that I started using Vim as my main editor, I've learned to use windows (splits) when working with multiple files (Because in every task, I need lots of files to work with)

But a few days ago, I ran into this question and it blow my mind (and my workflow :))

So I tried to use buffers and no windows and it's really hard. Imagine having multiple blocks (folders) and each one of them has a model.php and a controller.php in them. So at the start of the task, I don't know which block I need, so after a few minutes, I'll open multiple model.phps and controller.phps.

Now, if I don't have every file that I need in my buffer, I must search buffer first and when I see that I didn't load it, now I have to use explorer and load the file into buffer. So it's something like this:

:ls<CR>
{If the file that I need is here then}
:b num<CR>
{else}
:FZF {and finding that file}

So it's a lot harder than just working with windows (Where I can see what files are loaded in front of me)

(And of course the overhead of finding buffers and searching them by name/number is like opening the file every time that you needed)

But as said in this question and lots of other places, buffers should make your workflow easier than windows and windows should be used only for diffs and etc.

So is there any better ways to use buffers or am I doing something wrong?

(BTW, I'm currently using :Buffers from fzf.vim)

2

There are 2 answers

1
romainl On BEST ANSWER

So I tried to use buffers and no windows and it's really hard.

This means that you misunderstood both the spirit and the letter of the linked answer.

To recap, Vim's exact equivalent of "documents" in regular document-based applications is buffers. Vim also gives you a first layer of abstraction on top of buffers: windows, and another one on top of windows: tab pages in order to give you more flexibility with building your workflow.

Forcing oneself to use buffers instead of windows or instead of tab pages or whatever makes no sense as there is value in all three and such an attitude would only decrease the overall value of your editor. Use the interaction model that best suits your needs, not the interaction model that you convinced yourself is the purest.

As for the confusion between files and buffers, how about the confusion between files and tab pages or between buffers and windows? When you are dealing with abstractions built on top of other abstractions you have to have commands specific to one layer or another and learning how that layered cake works gives you the necessary intuition for deciding what command to use and when.

Basically, you have 3 cases:

Case # Is a buffer Is a file
1 Y Y
2 Y N
3 N Y

In case #1, the buffer is associated with a file so you can use both file-centric and buffer-centric commands to reach your target.

In case #2, the buffer is not associated with a file so you can only use buffer-centric commands to reach your target.

In case #3, there is no buffer so yo can only use file-related commands to reach your target.

Another way to think about it is to ask the question "Have I already been there?". If the answer is "no", then use file-centric commands, if the answer is "yes", use buffer-centric commands. If you have no idea or if you don't want to think about any of this, just use file-centric commands as a fallback.

Note that the context of that answer was "buffers vs windows vs tab pages". Abstracting yourself away from the notions of files or documents is the real deal.

1
Matt On
  1. When speaking of "best workflow" we inevitably speak of our personal habits and tastes. So just remember that it's your editor, your workflow and your "best practices". Not someone else's.

  2. Windows/tabs and buffers do not prohibit using each other. It is not a problem to open buffer in current window/tab even if it was already opened in another one (or even in dozen ones).

  3. If you feel uncomfortable searching through the buffers list then try doing it with some alternative tools. For example, you like clicking with mouse then run GVim and browse through "Buffers" menu; or you are good at memorizing numbers then make all buffers numbers to show in status line and switch buffers by typing NN<Ctrl-^> directly; or you like reading file contents then find some plugin that shows also "a buffer preview" etc.etc.