关闭文件而不退出VIM应用程序?

时间:2008-11-01 22:35:04

标签: vim

我是VIM的新手。我使用:e:w命令编辑和编写非常方便的文件。我不确定是否有“关闭”命令关闭当前文件而不离开VIM?

我知道:q命令可用于关闭文件,但如果它是最后一个文件,VIM也会关闭;实际上在Mac OS上,MacVIM确实退出了。只关闭了VIM窗口,我可以使用Control-N再次打开一个空白的VIM。我希望VIM保持空白屏幕。

11 个答案:

答案 0 :(得分:326)

这会删除缓冲区(转换为关闭文件)

:bd 

答案 1 :(得分:40)

正如已经提到的,你正在寻找:bd ,但是这并没有完全删除缓冲区,它仍然可以访问:

:e foo
:e bar
:buffers
  1 #h   "foo"                          line 1
  2 %a   "bar"                          line 1
Press ENTER or type command to continue
:bd 2
:buffers
  1 %a   "foo"                          line 1
Press ENTER or type command to continue
:b 2
2   bar

您可能希望:bw 完全删除它。

:bw 2
:b 2 
E86: Buffer 2 does not exist

不知道:bw 在一段时间内困扰我。

答案 2 :(得分:21)

如果你的vim窗口中有多个拆分窗口,那么:bd会关闭当前文件的拆分窗口...所以我喜欢使用更高级的东西:

map fc <Esc>:call CleanClose(1)

map fq <Esc>:call CleanClose(0)


function! CleanClose(tosave)
if (a:tosave == 1)
    w!
endif
let todelbufNr = bufnr("%")
let newbufNr = bufnr("#")
if ((newbufNr != -1) && (newbufNr != todelbufNr) && buflisted(newbufNr))
    exe "b".newbufNr
else
    bnext
endif

if (bufnr("%") == todelbufNr)
    new
endif
exe "bd".todelbufNr
endfunction

答案 3 :(得分:17)

:[N]bd[elete][!]                        *:bd* *:bdel* *:bdelete* *E516*
:bd[elete][!] [N]
                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.  Any windows for this buffer are
                closed.  If buffer [N] is the current buffer, another buffer
                will be displayed instead.  This is the most recent entry in
                the jump list that points into a loaded buffer.
                Actually, the buffer isn't completely deleted, it is removed
                from the buffer list |unlisted-buffer| and option values,
                variables and mappings/abbreviations for the buffer are
                cleared.

答案 4 :(得分:9)

如果您已经保存了最后一个文件,则:enew是您的朋友(:enew!如果您不想保存最后一个文件)。请注意,原始文件仍将位于缓冲区列表中(可通过以下方式访问:ls)。

答案 5 :(得分:6)

如果您修改某个文件并希望关闭它而不退出VIM且未保存,则应键入:bd!

答案 6 :(得分:3)

:可以映射bd。我将它映射到F4,如果因为我不再需要的某些改变而需要强制关闭,则将它转换为F4。

答案 7 :(得分:2)

有一个插件:bufkill(见http://www.vim.org/scripts/script.php?script_id=1147

这会添加BD等删除缓冲区,而不会关闭任何拆分(如bd)。

答案 8 :(得分:1)

我已将bd映射到F4 在.vimrc文件中插入以下内容,按F4键,它将关闭当前文件。

:map <F4> :bd<CR>

答案 9 :(得分:0)

查看Butane插件,以便在关闭缓冲区时保持窗口布局。

https://github.com/Soares/butane.vim

答案 10 :(得分:-1)

我有同样的问题,所以我制作了插件。 此插件替换:q和其他命令,然后阻止窗口关闭。

如果您还有问题,请尝试使用以下插件。 https://github.com/taka-vagyok/prevent-win-closed.vim

相关问题