Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Hot answers tagged

105 votes
Accepted

Close and unload a buffer without exiting Vim

There is command to do exactly that: :bdelete or just :bd. By default it will unload current buffer. To unload other buffer, first get the list of all buffers with :buffers command, and after that you ...
user avatar
  • 1,881
87 votes
Accepted

Is there an easy way to close a scratch buffer/preview window?

The top window is called the preview window. So any of <c-w>z, <c-w><c-z> or :pc[lose][!] should work. The below is the help for :help :pclose CTRL-W z CTRL-W_z CTRL-W CTRL-...
user avatar
  • 1,846
74 votes

Fastest way to switch to a buffer in vim?

The :b command can also take a substring of the name of the file of the buffer which you want to travel to, which is very convenient. For example, if you have three buffers foo, bar, and baz, then :...
user avatar
  • 13.6k
68 votes
Accepted

How to save window, split, and buffer layout?

You can use vim's :mksession and write each "workspace" to a different file, then reopen a session using vim -S session_file, however, if you're open to using a plugin then I find the vim-startify ...
user avatar
  • 2,201
57 votes
Accepted

Dump the output of internal vim command into buffer

You can use :redir to redirect the output to a variable, register, or file. Example of redirecting to the unnamed register: :redir @">|silent scriptnames|redir END|enew|put Alternatively Tim Pope'...
user avatar
56 votes
Accepted

How can I reload all buffers at once?

See :help bufdo for what you want to do. It will execute a command in each buffer in the buffer list. For example: :bufdo e You may also want to look at :help noconfirm to disable the confirmation ...
user avatar
  • 731
42 votes

Close and unload a buffer without exiting Vim

You can completely wipe out a buffer using the :bwipeout (or :bw) command. This completely removes the buffer from memory, including any marks, option settings, etc. that you have added to it. ...
user avatar
  • 521
38 votes
Accepted

How to go to previous open buffer from Vim command line?

The jumplist is not the best way to do buffer navigation. Vim has a great number of buffer navigation tools which are probably better used for this task. <c-^>/<c-6> - jump to the ...
user avatar
34 votes

Is there an easy way to close a scratch buffer/preview window?

For my specific case, the preview window was opened by plugins. So, I used plugin configuration to automatically close these windows. Supertab As suggested by Alex Kroll: let g:...
user avatar
  • 23.7k
33 votes

How do you open a new buffer in the current window?

The answer is to use :ene[w] Hah I should have looked at the help before! My first instinct was to try :e without any arguments. I looked at the help for this and a bit further down is :ene[w] ...
user avatar
  • 1,121
32 votes
Accepted

How do I open a new buffer without opening it in a split?

You can use :enew. See :h :enew: Edit a new, unnamed buffer. This fails when changes have been made to the current buffer, unless 'hidden' is set or 'autowriteall' is ...
user avatar
  • 45.9k
31 votes

How to save window, split, and buffer layout?

You can use tpope's vim-obsession plugin to easily manage sessions. It is like a wrapper to Vim's in-built mksession, but provides a set of other niceties as well. You can save the current session (...
user avatar
  • 15.3k
30 votes

Fastest way to switch to a buffer in vim?

This is what I use: nnoremap <Leader>b :ls<CR>:b<Space> Now pressing \b will list the available buffers and prepare :b for you. Then you can just type the buffer number, and hit ...
user avatar
  • 3,392
28 votes
Accepted

A built-in way to make Vim open a new buffer with file?

I think you might be misunderstanding what you're seeing happen. :edit does not close your current buffer. It just replaces it in the current window. If you type :buffers your previous buffer ...
user avatar
  • 6,320
23 votes
Accepted

BufWinLeave * mkview with unnamed file: Error 32

Switching to ?* as the regex will no longer match empty file names. I actually recommend you switch to the following, which is a corresponding block of .vimrc that has been tweaked to suppress many of ...
user avatar
22 votes

Fastest way to switch to a buffer in vim?

:CtrlPBuffer from plugin CtrlP (5k ★) Main feature of this plugin is to provide a fuzzy matching to conveniently open a different buffer with visual feedback. Open the fuzzy buffer listing with :...
user avatar
  • 3,960
21 votes

How do I have buffers listed in a quickfix window in Vim?

You can populate the quickfix list with each buffer like this: :call setqflist(map(filter(range(1, bufnr('$')), 'buflisted(v:val)'), '{"bufnr": v:val}')) setqflist() takes a List of Dictionary items ...
user avatar
  • 7,140
20 votes

How can I reload all buffers at once?

You can do this with the :checktime command. From the docs: :checkt[ime] Check if any buffers were changed outside of Vim. This checks and warns you if you would end up ...
user avatar
  • 875
20 votes
Accepted

How to iterate over buffers in Vimscript

You can iterate over buffers if you use :bnext and all. I highly advice against this method. It'll trigger autocommands, and you'll have to remember where you were. In other words, it can be damn slow,...
user avatar
18 votes

Renumbering buffer list

"the buffer numbers get crazy" Tell me about it! By the end of the day I'm easy over 100 buffers. But luckily, as you can see in this animation, you have tab completion for buffer names. So, even ...
user avatar
17 votes

What is the difference between a tab, viewport, window, split and buffer?

A buffer is the in-memory text of a file. It may differ from the saved version of the file. A window is a view of a buffer. You can have two (or more) windows editing different parts of the same ...
user avatar
  • 2,221
17 votes
Accepted

How to open same file in multiple buffers

I'm not sure about what you mean by buffer. In Vim jargon, a buffer maps a file, or a file that could be saved later. If you really want independent buffers initialized with a same file, you'll need ...
user avatar
16 votes

How to save window, split, and buffer layout?

As a side note, I'd like to point out that I built yet another plugin dhruvasagar/vim-prosession as an extension to tpope/vim-obsession that enhances it even further to create & manage vim ...
user avatar
  • 5,145
16 votes

What is the order of WinEnter, BufEnter, BufRead, Syntax, FileType events?

To complete the @sundar answer: You can log the order of the events simply with a logging function: augroup EventLoggin autocmd! autocmd BufNewFile * call s:Log('BufNewFile') autocmd BufReadPre *...
user avatar
  • 15k
16 votes

Using :Explore without closing current buffer

In :help netrw-explore they mention several other commands to explore your files. Among them are :Texplore which opens the file explorer window in a new tab instead of using your current window. ...
user avatar
  • 6,416
16 votes
Accepted

What is a scratch window?

It is probably a scratch buffer, which can be named. From the help: scratch Contains text that can be discarded at any time. It is kept when closing the window, it must be deleted explicitly. ...
user avatar
  • 298
15 votes
Accepted

Execute content of buffer as vimscript

Yes, you can yank the whole buffer with ggyG or :%y and execute it with :@". In short: :%y|@"<CR> Used in a mapping: nnoremap <key> :%y|@"<CR> Used in a flexible command that ...
user avatar
  • 32.6k

Only top scored, non community-wiki answers of a minimum length are eligible