Vim create a blank buffer instead of exiting

1.4k views Asked by At

I want to start using Vim as my main editor, as I heard it is really awesome and productive. So I installed a bunch of plugins using Pathogen, and created a nice, long .vimrc file. One of the plugins I use (and really like) is NERDTree, so I copied the following lines into my .vimrc file from the NERDTree git repository:

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

Now to my question: I want to make it so no matter what, vim won't quit unless I explicitly tell it to (using a special command like :Quit or using q on MacVim). So basically, if I only have, say, a buffer containing a file open along with a NERDTree buffer (or even if I just have a single buffer open, no NERDTree), when I :q or :close that buffer, I want it to present me with a blank buffer, like when you just open vim. But I am not sure how to script that, so I would love some help on what to add to my .vimrc

TL;DR: How do you make Vim only exit using an explicit command, and not when you quit the last buffer?

(EDIT: Read the comments to @romainl's answer for more clarification about what exactly I'm looking for.

P.S. Here's my full .vimrc file, just in case:

" => 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

There are 2 answers

1
Alejandro On BEST ANSWER

This will delete all buffers and leave you in a blank one.

:%bd

Or as a mapping:

nnoremap <key> :%bd<CR>

This will also close all windows and tabs coincidentally because those close automatically if all the buffers are closed. It won't work if you have any unsaved buffers, which is probably what you want. Add a ! if you're sure you want to close without saving.

I usually want to close just the current buffer, but still have the rest open. So I use this. This won't close anything if there is only one buffer left.

:bp|bd#

Or as a mapping:

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

EDIT: In response to comment about using enew when there is only one buffer left:

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>
4
romainl On

Both :quit and :close are for windows.

If you really want to get rid of a buffer, use :bdelete or :bunload.

This command replaces the current buffer with a new empty buffer (:enew) and deletes the previous buffer (:bdelete#):

:enew|bd#

You can map it if you want:

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