Sublime Forum

What is a buffer?

#1

I’ve been working with views & regions for a while ( example here ), and I’ve seen several references to buffers in the API reference. However, there isn’t much of an explanation of what they are & what they’re used for. ( I think they are somewhat related to views? )

Please answer the following to clarify:
1. What is a buffer?
2. What are ( common uses + benefits ) of buffers?
3. In what cases would a buffer be preferable to a view & vice versa?
4. Links with more info or examples! :grin:

0 Likes

#2

The buffer is the data of a opened file or yet to create file. Usually for each view there is just one buffer. when two views shares the same buffer, that is a “cloned view” (any changes you do in one view will be shown in the other view, regardless if the file was saved or not). When you open a file twice (without cloning) you have two buffers, if you change these two views independently, you risk to overwrite the changes made into the other view.

An easy way to see a buffer in action.

  1. Mainmenubar - view – layout - columns 2
  2. CTRL+SHIFT+P, then search for “new view into file” (this is a poor named command that should be called “clone view”)
  3. Now drag the tab to the second column
  4. type anything in any of the two tabs.
  5. Anything you type should be shown in both views because these share the same buffer.

Some applications for this feature, if you have a really big screen, you may want to show more lines of the same file in columns syncing the scrolling, anything you write in any of the three columns will appear in other columns because these share the buffer.

You almost never want two buffers for a same file, because that leads to data lost when saving. Thats why I created https://github.com/titoBouzout/PreventFakeClones which is trying to detect when you open the same file in two instances and then closes any duplicated, and clones the first view getting the same buffer for all the views involved.

BUG 1: Theres a bug in ST with the use of clones. When you run a command or use on_activated(on_something), you always get as “view” parameter the first view, even if the view was the third view of this cloned file. For this reason I almost always use sublime.active_window().active_view() instead of self.view.

BUG 2: There two views will share the settings object.

BUG 3: You cannot move a cloned view to other windows.

BUG N: I cannot remember

Or something in these lines

3 Likes

#3

@tito

Great answer, thanks!

Funny that you responded to this, I actually just recommended your plugin while trying to rack up some rep @ StackOverflow :joy:

I’ve kinda worked with multiple views on a buffer then ( just cloning programatically ). I ran into BUG 1 that you mentioned & came to the same solution.


So I get that all views will have a buffer, and cloned views will share a buffer.

You also mentioned:

“The buffer is the data of a opened file or yet to create file.”

Does that mean you can have a buffer without a view? If so, does this offer any performance advantage over working with a view?

I ask because I’m working on a plugin that does a ridiculous amount of string manipulation, and its performance slows down if working with a file over 10k lines or so.

0 Likes

#4

I mean you can clone a view of a non-saved file

[quote=“fico, post:3, topic:18353”]
I ask because I’m working on a plugin that does a ridiculous amount of string manipulation, and its performance slows down if working with a file over 10k lines or so.
[/quote] the best thing you can do if you dont really need to use the ST API is to just manipulate the text, and replace everything. Because when doing lots of operation sublime text is eventually slow.

1 Like

#5

Btw, bug 2 can be “fixed” by restarting ST. I made a bug report on the github tracker but am on mobile currently and cba to dig it up.

1 Like

#6
2 Likes