Sunday, October 18, 2020

Buffers, Windows and tabs in vim/neovim

There are three concepts when it comes to viewing/editing files in vim.

Buffer:

When you open a file in vim the contents are loaded to RAM. This portion of the ram which has the contents of the file is the buffer. Now, this loading happens behind the scenes. It is no good if the file contents are in the buffer. We have to view it. That is what visual editors like vim are for. There are ways and means of editing files without having the contents of the file displayed on the screen. For that you can combine the power of ed and then the later front ends like grep and sed. So, now we are still at a stage where vim has loaded the file into memory and named that block of memory with a name taken from the file name itself. This buffer name can be used in moving around buffers when we have multiple buffers.

When you create an empty buffer, vim allocates a certain amount of the RAM for the buffer. If you write this content to a file then the contents are saved. If you delete the buffer without saving it then all your content created in this buffer is out in the ether.

For playing with buffers vim provides commands starting with :buffer. It follows the object, action on the object philosophy of the other vim commands.

When you shift from one buffer to another using the buffer next command or similar, vim prompts you to save the present buffer to file. This can be overridden with settings in the vim config file.


Window:

This is the area where the contents of the buffer is displayed to the user. After loading the file into the buffer, vim then reads the contents of the buffer and displays it on the screen for the user's viewing pleasure. A full screen of content, which was a big deal in the good old days. To display the file vim creates a window. The file contents are displayed within this window. Okay, you have been editing a single file for many days. Now, you want to refer another file. So you now go ahead and open it up with :e file2

You are in for a shocker, the screen is now showing only the contents of file2. As we have already clarified, a window is the place where vim displays the contents of the file. In the present case we have only one window. So, when you requested vim to open a second file, vim agreed and showed the contents on the solitary windows. To show that vim had to clear the screen and load up the buffer which was the contents read in from file2.

The situation here is we are having one window and two buffers, as such vim can display only one buffer while the other is still loaded and available with vim read to be displayed when asked for. You can ask to bring the buffer contents of file1 to the fore by using appropriate :buffer command. But, instead a more elegant way of viewing both files side by side would be to create another window. This will reduce the screen estate available for each windows. if you open two windows then both share 50% of the screen and so forth.

Ctrl-W v    opens a new window and splits the screen into half vertically. Now you can open file1 in the left window and file2 in the right window. Now there are two buffers and two windows displaying the respective buffer. What if I open the same file in both the windows, well all your editing activities in one window will reflect in the second window also. try it. Because you are editing the same buffer.

To end the discussion about windows, when you close a window, the buffer is not offloaded from memory. It is still there within vim. You have to delete the buffer using the respective command to instruct vim to offload the buffer from memory.

Tab:

Tabs are the masters of vim windows. Every tab has access to the full screen. Withing tabs you create windows as explained above. In the above example there was only one tab which is started by default. Every tab can have multiple windows. When you close a tab you close all the windows in that tab. tabs are good when you want all your files to use the complete screen of your monitor. Whenever a new tab is created it is started with exactly one window. If you want to view file1 and file2 using the full screen estate of your monitor and move between them then, the :tab command is the way to go. The disadvantage is that you cant see the file side by side.

When you close a tab, all the windows within that tab are closed. But all the buffers you were viewing in that tab within their respective windows are still in memory. You have to delete the buffers to remove them from memory.


 Of course, when you quit vim, all the buffers are closed and any changes which you have done to the buffers, will be queried for saving.

In summary, we see that there is no one single strategy which works for and individual. And for every individual, the strategy depends on the task/s at hand. So, dont be very harsh on your preferences. You will be losing out on the most important aspect of vim, ie, "situational editing"

No comments:

Post a Comment

Nobody can deter me away from "free as in freedom" concept seeded by Sri RMS. See to it that u dont make fun of my belief. If u think otherwise, no need to comment.