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

Dav*_*.ca 253 vim

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

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

Vin*_*vic 333

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

:bd 
Run Code Online (Sandbox Code Playgroud)

  • @Martin,要完全删除缓冲区,请使用**:bw** (18认同)
  • 虽然我倾向于选择你的优先选择,但是你的优于我的优先选择:enew因为我喜欢缓冲区列表中的缓冲区.:) (6认同)
  • 当我这样做时,vim显示第一个缓冲区,但我仍然可以访问缓冲区 (2认同)

seb*_*now 41

如前所述,您正在寻找: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
Run Code Online (Sandbox Code Playgroud)

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

:bw 2
:b 2 
E86: Buffer 2 does not exist
Run Code Online (Sandbox Code Playgroud)

不知道:bw给我打了很长时间.

  • 只要记住,VIM的帮助中会讲到`:bw`这样的内容-“除非您知道自己在做什么,否则请不要使用它。” (3认同)

Gow*_*wri 22

如果你的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
Run Code Online (Sandbox Code Playgroud)

  • 现在这个片段做了什么? (11认同)
  • @dolzenko它在正常模式下映射'fc'和'fq'以选择性地保存当前缓冲区,然后在删除原始缓冲区之前切换到新缓冲区.这样可以保留您设置的任何拆分. (2认同)
  • 不要重新映射"f"..."f"实际上非常有用.通过此映射,您永远无法"找到"前进的"c"或"q". (2认同)

eph*_*ent 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.


Ryt*_*mis 9

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


pab*_*ara 6

如果修改文件并希望在不退出VIM且未保存的情况下关闭它,则应键入:bd!