2008-11-01 74 views
229

我是VIM的新手。我使用:e:w命令來編輯和寫入非常方便的文件。我不確定是否有「close」命令關閉當前文件而不離開VIM?關閉文件而不退出VIM應用程序?

我知道:q命令可以用來關閉文件,但如果它是最後一個文件,VIM也會關閉;實際上在Mac OS上,MacVIM確實退出了。只有VIM窗口關閉,我可以使用Control-N再次打開空白VIM。我希望VIM保持空白屏幕打開狀態。

回答

306

這將刪除緩存(這相當於關閉文件)

:bd 
+5

對於OP的要求,你比我的要好,但我傾向於選擇:enew,因爲我喜歡在緩衝區列表中有緩衝區。 :) – Rytmis 2008-11-01 22:39:38

+2

當我這樣做,vim顯示第一個緩衝區,但我仍然可以訪問緩衝區 – 2009-12-02 17:02:53

+12

@Martin,完全刪除緩衝區,使用**:bw ** – sebnow 2010-12-02 11:14:03

8

如果你已經保存了最後一個文件,那麼:enew是你的朋友(:如果你不想保存最後一個文件,那麼是enew!)。請注意,原始文件仍將位於緩衝區列表中(可通過ls訪問)。

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.
20

如果你在你的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

:bd可以被映射。我將它映射到F4,如果因爲我不再需要的某些更改而需要強制關閉,則將shift-F4映射到F4。

37

前面已經提到的,你要找: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竊聽我了好一陣子。

5

如果修改文件,要關閉不退出VIM和不保存,應鍵入:bd!

0

我已經將bd映射到F4與 在您的.vimrc文件中插入以下內容並按F4鍵,它將關閉當前文件。

:map <F4> :bd<CR>