What is the difference between BufRead and BufEnter?

你说的曾经没有我的故事 提交于 2020-01-13 08:40:34

问题


I am confused between the two events BufRead and BufEnter. What is the difference between them?

Some example scenarios where I would choose one over the other would be greatly helpful.


回答1:


A buffer is the in-memory representation of a file; to edit one, it has to be displayed in a window.

The BufRead event is triggered after Vim has read the file into its memory. Unless you reload via :edit!, this only happens once.

The BufEnter is fired whenever you activate a certain buffer, i.e. when it becomes the buffer the cursor is in. For example when you have split windows that show different buffers, and you move between then. Also in a single window, when you use a command like <C-^>, :bnext to change which buffer is currently edited.




回答2:


BufRead is triggered after the buffer has been populated with the content of a file.

BufEnter is triggered after you enter a buffer for editing.

You may use BufNew, BufRead or BufAdd if you want to do something upon the creation of the buffer: setting some global variable, populating a buffer list of your own for use in a script.

You may use BufEnter when you want to set specific options depending on the filetype or whatever.



来源:https://stackoverflow.com/questions/15129517/what-is-the-difference-between-bufread-and-bufenter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!