如何在Vim中关闭一个缓冲区(从多个缓冲区中删除)?

时间:2022-02-24 14:42:15

I open several files in Vim by, for example, running

我在Vim中打开几个文件,例如,运行。

vim a/*.php

which opens 23 files.

23日开幕的文件。

I then make my edit and run the following twice

然后我编辑并运行以下两次。

:q

which closes all my buffers.

它关闭了所有的缓冲区。

How can you close only one buffer in Vim?

如何在Vim中仅关闭一个缓冲区?

9 个解决方案

#1


447  

A word of caution: "w does not stand for write but for wipeout!"

提醒一句:“w不代表写作,而代表着失败!”

More from manuals:

更多的从手册:

:bd

:双相障碍

Unload buffer [N] (default: current buffer) and delete it from the buffer list. If the buffer was changed, this fails, unless when [!] is specified, in which case changes are lost. The file remains unaffected.

卸载缓冲区[N](默认:当前缓冲区)并从缓冲区列表中删除它。如果缓冲区被更改,则此操作失败,除非(!]指定,在这种情况下,更改将丢失。文件仍然不受影响。

If you know what you're doing, you can also use :bw

如果你知道自己在做什么,你也可以用:bw。

:bw

:bw

Like |:bdelete|, but really delete the buffer.

像|:bdelete|,但真正删除缓冲区。

#2


331  

If this isn't made obvious by the the previous answers:

如果之前的答案没有明显的说明:

:bd will close the current buffer. If you don't want to grab the buffer list.

:bd将关闭当前缓冲区。如果您不想获取缓冲区列表。

#3


89  

Check your buffer id using :buffers

检查您的缓冲区id使用:缓冲区。

you will see list of buffers there like

您将看到类似的缓冲区列表。

1  a.php
2  b.php
3  c.php

if you want to remove b.php from buffer

如果你想移除b。php从缓冲区

:2bw

if you want to remove/close all from buffers

如果您想要删除/关闭所有的缓冲区。

:1,3bw

#4


35  

Rather than browse the ouput of the :ls command and delete (unload, wipe..) a buffer by specifying its number, I find that using file names is often more effective.

通过指定它的编号,我发现使用文件名通常更有效,而不是浏览:ls命令和删除(卸载,删除..)缓冲区的输出。

For instance, after I opened a couple of .txt file to refresh my memories of some fine point.. copy and paste a few lines of text to use as a template of sorts.. etc. I would type the following:

例如,在我打开了几个.txt文件以刷新我对一些细节的记忆之后。复制并粘贴几行文本用作排序的模板。我将键入以下内容:

:bd txt <Tab>

Note that the matching string does not have to be at the start of the file name.

注意,匹配的字符串并不一定要在文件名的开头。

The above displays the list of file names that match 'txt' at the bottom of the screen and keeps the :bd command I initially typed untouched, ready to be completed.

上面显示了在屏幕底部匹配“txt”的文件名列表,并保持:我最初输入的bd命令未被修改,准备完成。

Here's an example:

这里有一个例子:

doc1.txt doc2.txt
:bd txt 

I could backspace over the 'txt' bit and type in the file name I wish to delete, but where this becomes really convenient is that I don't have to: if I hit the Tab key a second time, Vim automatically completes my command with the first match:

我可以在“txt”位上返回,并输入我想要删除的文件名,但这变得非常方便,我不必:如果我第二次按Tab键,Vim会自动完成我的第一个匹配命令:

:bd doc1.txt

If I want to get rid of this particular buffer I just need to hit Enter.

如果我想要清除这个特定的缓冲区,我只需要点击回车。

And if the buffer I want to delete happens to be the second (third.. etc.) match, I only need to keep hitting the Tab key to make my :bd command cycle through the list of matches.

如果我想要删除的缓冲区恰好是第二个。等)匹配,我只需要继续按Tab键,使我的:bd命令循环通过匹配列表。

Naturally, this method can also be used to switch to a given buffer via such commands as :b.. :sb.. etc.

当然,这种方法也可以通过以下命令切换到给定的缓冲区:b..:某人. .等。

This approach is particularly useful when the 'hidden' Vim option is set, because the buffer list can quickly become quite large, covering several screens, and making it difficult to spot the particular buffer I am looking for.

当设置“隐藏”Vim选项时,这种方法特别有用,因为缓冲区列表可以很快变得相当大,覆盖多个屏幕,并且很难找到我要查找的特定缓冲区。

To make the most of this feature, it's probably best to read the following Vim help file and tweak the behavior of Tab command-line completion accordingly so that it best suits your workflow:

为了充分利用这个特性,最好阅读下面的Vim帮助文件,并相应地调整选项卡命令行完成的行为,以使它最适合您的工作流程:

:help wildmode

The behavior I described above results from the following setting, which I chose for consistency's sake in order to emulate bash completion:

我在上面描述的行为来自以下设置,我选择一致性的原因是为了模仿bash完成:

:set wildmode=list:longest,full

As opposed to using buffer numbers, the merit of this approach is that I usually remember at least part of a given file name letting me target the buffer directly rather than having to first look up its number via the :ls command.

与使用缓冲区号不同,这种方法的优点是,我通常至少记住一个给定文件名的一部分,让我直接针对缓冲区,而不必首先通过ls命令查找它的编号。

#5


30  

Use:

使用:

  • :ls - to list buffers
  • :ls -列出缓冲区。
  • :bd#n - to close buffer where #n is the buffer number (use ls to get it)
  • :bd#n -关闭缓冲区,其中#n是缓冲区号(使用ls来获取它)

Examples:

例子:

  • to delete buffer 2:

    删除缓冲2:

    :bd2
    

#6


13  

You can map next and previous to function keys too, making cycling through buffers a breeze

你也可以映射下一个和之前的功能键,使循环通过缓冲区的微风。

map <F2> :bprevious<CR>
map <F3> :bnext<CR>

from my vimrc

从我vimrc

#7


1  

How about

如何

vim -O a a

That way you can edit a single file on your left and navigate the whole dir on your right... Just a thought, not the solution...

这样你就可以在你的左边编辑一个文件,并在你的右边导航整个目录。只是一个想法,而不是解决方案……

#8


1  

[EDIT: this was a stupid suggestion from a time I did not know Vim well enough. Please don't use tabs instead of buffers; tabs are Vim's "window layouts"]

[编辑:这是一个愚蠢的建议,从一段时间我还不太了解Vim。请不要使用制表符代替缓冲区;标签是Vim的“窗口布局”

Maybe switch to using tabs?

可能切换到使用选项卡?

vim -p a/*.php opens the same files in tabs

vim - p / *。php在制表符中打开相同的文件。

gt and gT switch tabs back and forth

gt和gt交换标签。

:q closes only the current tab

:q只关闭当前选项卡。

:qa closes everything and exits

:qa关闭一切并退出。

:tabo closes everything but the current tab

:tabo关闭所有东西,除了当前标签。

#9


1  

Those using a buffer or tree navigation plugin, like Buffergator or NERDTree, will need to toggle these splits before destroying the current buffer - else you'll send your splits into wonkyville

那些使用缓冲或树导航插件的人,比如Buffergator或NERDTree,在破坏当前缓冲区之前需要切换这些分割,否则你将把你的分裂发送到wonkyville。

I use:

我使用:

"" Buffer Navigation                                                                                                                                                                                        
" Toggle left sidebar: NERDTree and BufferGator                                                                                                                                                             
fu! UiToggle()                                                                                                                                                                                              
  let b = bufnr("%")                                                                                                                                                                                        
  execute "NERDTreeToggle | BuffergatorToggle"                                                                                                                                                              
  execute ( bufwinnr(b) . "wincmd w" )                                                                                                                                                                      
  execute ":set number!"                                                                                                                                                                                    
endf                                                                                                                                                                                                        
map  <silent> <Leader>w  <esc>:call UiToggle()<cr>   

Where "NERDTreeToggle" in that list is the same as typing :NERDTreeToggle. You can modify this function to integrate with your own configuration.

在该列表中“NERDTreeToggle”与键入:NERDTreeToggle相同。您可以修改这个函数来集成您自己的配置。

#1


447  

A word of caution: "w does not stand for write but for wipeout!"

提醒一句:“w不代表写作,而代表着失败!”

More from manuals:

更多的从手册:

:bd

:双相障碍

Unload buffer [N] (default: current buffer) and delete it from the buffer list. If the buffer was changed, this fails, unless when [!] is specified, in which case changes are lost. The file remains unaffected.

卸载缓冲区[N](默认:当前缓冲区)并从缓冲区列表中删除它。如果缓冲区被更改,则此操作失败,除非(!]指定,在这种情况下,更改将丢失。文件仍然不受影响。

If you know what you're doing, you can also use :bw

如果你知道自己在做什么,你也可以用:bw。

:bw

:bw

Like |:bdelete|, but really delete the buffer.

像|:bdelete|,但真正删除缓冲区。

#2


331  

If this isn't made obvious by the the previous answers:

如果之前的答案没有明显的说明:

:bd will close the current buffer. If you don't want to grab the buffer list.

:bd将关闭当前缓冲区。如果您不想获取缓冲区列表。

#3


89  

Check your buffer id using :buffers

检查您的缓冲区id使用:缓冲区。

you will see list of buffers there like

您将看到类似的缓冲区列表。

1  a.php
2  b.php
3  c.php

if you want to remove b.php from buffer

如果你想移除b。php从缓冲区

:2bw

if you want to remove/close all from buffers

如果您想要删除/关闭所有的缓冲区。

:1,3bw

#4


35  

Rather than browse the ouput of the :ls command and delete (unload, wipe..) a buffer by specifying its number, I find that using file names is often more effective.

通过指定它的编号,我发现使用文件名通常更有效,而不是浏览:ls命令和删除(卸载,删除..)缓冲区的输出。

For instance, after I opened a couple of .txt file to refresh my memories of some fine point.. copy and paste a few lines of text to use as a template of sorts.. etc. I would type the following:

例如,在我打开了几个.txt文件以刷新我对一些细节的记忆之后。复制并粘贴几行文本用作排序的模板。我将键入以下内容:

:bd txt <Tab>

Note that the matching string does not have to be at the start of the file name.

注意,匹配的字符串并不一定要在文件名的开头。

The above displays the list of file names that match 'txt' at the bottom of the screen and keeps the :bd command I initially typed untouched, ready to be completed.

上面显示了在屏幕底部匹配“txt”的文件名列表,并保持:我最初输入的bd命令未被修改,准备完成。

Here's an example:

这里有一个例子:

doc1.txt doc2.txt
:bd txt 

I could backspace over the 'txt' bit and type in the file name I wish to delete, but where this becomes really convenient is that I don't have to: if I hit the Tab key a second time, Vim automatically completes my command with the first match:

我可以在“txt”位上返回,并输入我想要删除的文件名,但这变得非常方便,我不必:如果我第二次按Tab键,Vim会自动完成我的第一个匹配命令:

:bd doc1.txt

If I want to get rid of this particular buffer I just need to hit Enter.

如果我想要清除这个特定的缓冲区,我只需要点击回车。

And if the buffer I want to delete happens to be the second (third.. etc.) match, I only need to keep hitting the Tab key to make my :bd command cycle through the list of matches.

如果我想要删除的缓冲区恰好是第二个。等)匹配,我只需要继续按Tab键,使我的:bd命令循环通过匹配列表。

Naturally, this method can also be used to switch to a given buffer via such commands as :b.. :sb.. etc.

当然,这种方法也可以通过以下命令切换到给定的缓冲区:b..:某人. .等。

This approach is particularly useful when the 'hidden' Vim option is set, because the buffer list can quickly become quite large, covering several screens, and making it difficult to spot the particular buffer I am looking for.

当设置“隐藏”Vim选项时,这种方法特别有用,因为缓冲区列表可以很快变得相当大,覆盖多个屏幕,并且很难找到我要查找的特定缓冲区。

To make the most of this feature, it's probably best to read the following Vim help file and tweak the behavior of Tab command-line completion accordingly so that it best suits your workflow:

为了充分利用这个特性,最好阅读下面的Vim帮助文件,并相应地调整选项卡命令行完成的行为,以使它最适合您的工作流程:

:help wildmode

The behavior I described above results from the following setting, which I chose for consistency's sake in order to emulate bash completion:

我在上面描述的行为来自以下设置,我选择一致性的原因是为了模仿bash完成:

:set wildmode=list:longest,full

As opposed to using buffer numbers, the merit of this approach is that I usually remember at least part of a given file name letting me target the buffer directly rather than having to first look up its number via the :ls command.

与使用缓冲区号不同,这种方法的优点是,我通常至少记住一个给定文件名的一部分,让我直接针对缓冲区,而不必首先通过ls命令查找它的编号。

#5


30  

Use:

使用:

  • :ls - to list buffers
  • :ls -列出缓冲区。
  • :bd#n - to close buffer where #n is the buffer number (use ls to get it)
  • :bd#n -关闭缓冲区,其中#n是缓冲区号(使用ls来获取它)

Examples:

例子:

  • to delete buffer 2:

    删除缓冲2:

    :bd2
    

#6


13  

You can map next and previous to function keys too, making cycling through buffers a breeze

你也可以映射下一个和之前的功能键,使循环通过缓冲区的微风。

map <F2> :bprevious<CR>
map <F3> :bnext<CR>

from my vimrc

从我vimrc

#7


1  

How about

如何

vim -O a a

That way you can edit a single file on your left and navigate the whole dir on your right... Just a thought, not the solution...

这样你就可以在你的左边编辑一个文件,并在你的右边导航整个目录。只是一个想法,而不是解决方案……

#8


1  

[EDIT: this was a stupid suggestion from a time I did not know Vim well enough. Please don't use tabs instead of buffers; tabs are Vim's "window layouts"]

[编辑:这是一个愚蠢的建议,从一段时间我还不太了解Vim。请不要使用制表符代替缓冲区;标签是Vim的“窗口布局”

Maybe switch to using tabs?

可能切换到使用选项卡?

vim -p a/*.php opens the same files in tabs

vim - p / *。php在制表符中打开相同的文件。

gt and gT switch tabs back and forth

gt和gt交换标签。

:q closes only the current tab

:q只关闭当前选项卡。

:qa closes everything and exits

:qa关闭一切并退出。

:tabo closes everything but the current tab

:tabo关闭所有东西,除了当前标签。

#9


1  

Those using a buffer or tree navigation plugin, like Buffergator or NERDTree, will need to toggle these splits before destroying the current buffer - else you'll send your splits into wonkyville

那些使用缓冲或树导航插件的人,比如Buffergator或NERDTree,在破坏当前缓冲区之前需要切换这些分割,否则你将把你的分裂发送到wonkyville。

I use:

我使用:

"" Buffer Navigation                                                                                                                                                                                        
" Toggle left sidebar: NERDTree and BufferGator                                                                                                                                                             
fu! UiToggle()                                                                                                                                                                                              
  let b = bufnr("%")                                                                                                                                                                                        
  execute "NERDTreeToggle | BuffergatorToggle"                                                                                                                                                              
  execute ( bufwinnr(b) . "wincmd w" )                                                                                                                                                                      
  execute ":set number!"                                                                                                                                                                                    
endf                                                                                                                                                                                                        
map  <silent> <Leader>w  <esc>:call UiToggle()<cr>   

Where "NERDTreeToggle" in that list is the same as typing :NERDTreeToggle. You can modify this function to integrate with your own configuration.

在该列表中“NERDTreeToggle”与键入:NERDTreeToggle相同。您可以修改这个函数来集成您自己的配置。