Vim创建一个空白缓冲区而不是退出

时间:2016-12-29 22:13:30

标签: vim macvim nerdtree

我想开始使用Vim作为我的主编辑,因为我听说它真的很棒,很有成效。所以我使用Pathogen安装了一堆插件,并创建了一个很好的长.vimrc文件。 我使用的插件之一(并且非常喜欢)是NERDTree,因此我将以下行复制到NERDTree git存储库中的.vimrc文件中:

autocmd StdinReadPre * let s:std_in = 1
autocmd VimEnter *
      \ NERDTree |
      \ wincmd p
autocmd VimEnter *
      \ if argc() == 0 && !exists("s:std_in") |
      \ NERDTree |
      \ wincmd p |
      \ endif
autocmd VimEnter *
      \ if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") |
      \ exe 'NERDTree' argv()[0] |
      \ wincmd p |
      \ ene |
      \ endif
autocmd BufEnter *
      \ if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) |
      \ q |
      \ endif

现在我的问题是:无论如何我都想这样做,除非我明确地告诉它(使用像:Quit这样的特殊命令或使用<,否则vim不会退出在MacVim上kbd>⌘ q 。所以基本上,如果我只有一个缓冲区,包含一个打开的文件和一个NERDTree缓冲区(或者即使我只打开一个缓冲区,也没有NERDTree),当我:q:close时那个缓冲区,我想让它给我一个空白缓冲区,就像你打开vim一样。但是我不知道如何编写脚本,所以我希望能帮助我添加到.vimrc

TL; DR:如何使Vim仅使用显式命令退出,而不是在退出最后一个缓冲区时退出?

(编辑:请阅读评论@ romainl的答案,以便更清楚地了解我正在寻找什么。

P.S。这是我的完整.vimrc文件,以防万一:

" => Pathogen and FTPlugins Setup
execute  pathogen#infect()
filetype plugin indent on

let mapleader = "\ "

" => Color Theme Options
colorscheme monokai

" => Custom Commands
""""command W  w
""""command Q  q
""""command Wq wq
""""command WQ wq
""""command B  b
command Tabs   set listchars=tab:\|\ ,trail:~,extends:>,precedes:<
command NoTabs set listchars=tab:\ \ ,trail:~,extends:>,precedes:<
" =*=> Plugin-Related
" =*=> Theme-Related
command Light
      \ set background=light |
      \ colorscheme solarized
command Dark
      \ set background=dark |
      \ colorscheme solarized

" => Custom Mappings
" =*=> Basic Editor Commands
nnoremap '                  :%s/
                                                        " Find and replace
nnoremap !                  :!
                                                        " Run shell command
nnoremap <C-o>    :bprevious<CR>
nnoremap <C-p>    :bnext<CR>
nnoremap <leader> <C-w>
" =*=> Workdir Manipulation
" nnoremap <C-j>    :lcd %:p:h<CR>
" =*=> Plugin-Related
" =*=*=> NERDTree
noremap  <C-f>    :NERDTreeToggle<CR>
noremap  <C-d>    :NERDTreeFind<CR>
" =*=*=> BufExplorer
nnoremap <Tab>    :BufExplorerHorizontalSplit<CR>

" => Settings
syntax on                                               " Enable syntax highlighting
set number                                              " Show line numbers
set ruler                                               " Show row & column number
set splitright                                          " Vertically split to the right by default
set splitbelow                                          " Horizontally split below by default
set ignorecase                                          " Case-insesitive tab completion
set hidden                                              " Allow buffer switching with unwritten changes
set laststatus=2                                        " Always show status bar (even when there's only one window)
set wildchar=<Tab> wildmenu wildmode=full               " Enhanced tab-completion
set listchars=tab:\|\ ,trail:~,extends:>,precedes:<     " Set symbols for hidden characters
set list                                                " Show hidden characters
set autochdir                                           " When opening a file, automatically set workingdir (of the current buffer) to the directory containing that file
set nowrap                                              " Disable line wrapping
" =*=> Tab Settings (commonly overwritten in ftplugins)
set tabstop=4
set shiftwidth=4
set shiftround
set autoindent
set smartindent
set expandtab                                           " Output spaces when pressing <Tab>, not actual tab characters ('\t')
" =*=> Split vertically by default

" =*=> GVim Settings
set guioptions-=r                                       " Remove right-hand scroll bar
set guioptions-=L                                       " Remove left-hand scroll bar
" =*=> Plugin-Related
" =*=*=> NERDTree
let NERDTreeShowHidden = 1                              " Always show hidden files
let NERDTreeShowBookmarks = 1
" =*=*=> NERDCommenter
let g:NERDSpaceDelims = 1                               " Add spaces after comment delimiters by default
let g:NERDCommentEmptyLines = 1                         " Allow commenting and inverting empty lines (useful when commenting a region)
let g:NERDTrimTrailingWhitespace = 1                    " Enable trimming of trailing whitespace when uncommenting

" => AutoCommands
autocmd BufEnter * lcd %:p:h                   " When opening a file, automatically set workingdir (of the current buffer) to the directory containing that file
" =*=>Plugin-Related
" =*=*=> NERDTree
autocmd StdinReadPre * let s:std_in = 1
autocmd VimEnter *
      \ NERDTree |
      \ wincmd p
autocmd VimEnter *
      \ if argc() == 0 && !exists("s:std_in") |
      \ NERDTree |
      \ wincmd p |
      \ endif
autocmd VimEnter *
      \ if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") |
      \ exe 'NERDTree' argv()[0] |
      \ wincmd p |
      \ ene |
      \ endif
autocmd BufEnter *
      \ if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) |
      \ q |
      \ endif
                " Up to here, copied from repository's README.md

" => Miscellanea
highlight NonText    guifg=DimGray ctermbg=NONE guibg=NONE
highlight SpecialKey guifg=DimGray ctermbg=NONE guibg=NONE " Set Hidden Characters Color

2 个答案:

答案 0 :(得分:1)

:quit:close都适用于Windows。

如果确实想要摆脱缓冲区,请使用:bdelete:bunload

此命令用新的空缓冲区(:enew)替换当前缓冲区并删除前一个缓冲区(:bdelete#):

:enew|bd#

您可以根据需要进行映射:

nnoremap <key> :enew\|bdelete#<CR>

答案 1 :(得分:0)

这将删除所有缓冲区并留空。

:%bd

或作为映射:

nnoremap <key> :%bd<CR>

这也会巧合地关闭所有窗口和标签,因为如果所有缓冲区都关闭,它们会自动关闭。如果你有任何未保存的缓冲区,它将无法工作,这可能是你想要的。如果您确定要关闭而不保存,请添加!

我通常只想关闭当前缓冲区,但仍然打开其余缓冲区。所以我用这个。如果只剩下一个缓冲区,则不会关闭任何内容。

:bp|bd#

或作为映射:

nnoremap <key> :bp\|bd#<CR>

修改 回复当只剩下一个缓冲区时使用enew的评论:

function! BufferDelete()
    if len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) > 1
        execute "bp|bd#"
    else
        execute "enew|bd#"
    endif
endfunction
nnoremap <key> :call BufferDelete()<CR>