Hot answers tagged

63

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 can specify the number after :bd to remove it. Also :bd + space + tab allows completion using buffer name.


63

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-Z CTRL-W_CTRL-Z :pc :pclose :pc[lose][!] Close any "Preview" window currently open. When the 'hidden' option is set, or when the buffer ...


46

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 :b bar will switch to the bar file. :b o will switch to the foo file. :b a will give you an error because it could mean either bar or baz, but you can fix the ...


46

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 Startify plugin is exactly what I need for this sort of scenario: https://github.com/mhinz/vim-startify It does a great job of managing sessions, in vim and mccvim, and it ...


28

The sbuffer command will let you create a split with an existing buffer name or number. :sb# will open a split with buffer number # (as displayed in the :buffers list). :sb foo will open a split with the buffer named foo. Tab-complete will cycle the available buffer names. You can use :vertical sb... if you want a vertical split instead of the default, ...


28

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:SuperTabClosePreviewOnPopupClose = 1 YouCompleteMe let g:ycm_autoclose_preview_window_after_insertion = 1 let g:ycm_autoclose_preview_window_after_completion = 1 If the second ...


26

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. Similarly, :bdelete (or :bd) removes the buffer, but leaves it in memory and keeps marks and option settings. As per the comment by Tom Hale, the Vim documentation ...


25

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 dialog before issueing the bufdo command :set noconfirm and reenabling it after the bufdo command. :set confirm


23

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 (or buffer layout) by giving the command :Obsess. If you don't supply an argument, it writes a session file called Session.vim by default. To reload a session, ...


23

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] Edit a new, unnamed buffer. This fails when changes have been made to the current buffer, unless 'hidden' is set or 'autowriteall' is set and the file can be ...


21

No (not without deleting buffers). Vim does not support manually buffer number assignment or re-ordering of buffers once you open them. It's philosophy is that each buffer gets an identifier that is fixed for the lifetime of that buffer (in the help for :ls, it notes that "each buffer has a unique number. That number will not change..."). You could use ...


19

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's scriptease.vim provides the :Scriptnames command which will load :scriptnames into the quickfix list and :copen. If you find yourself redirecting many ...


18

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 describing each error (filename, line number, position, etc.). In this case we're specifying a minimal set of information: the buffer number map() takes a List ...


14

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 Enter. Or you can type part of the filename, and hit Enter. (However I usually hit Tab before hitting Enter, to check I got the right buffer. If not, I ...


14

"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 though you can't renumber the buffers, you can still jump around easily. I don't know if it's clear from the animation but, the "tab completion" is unlike command ...


14

"Bad" is a bit relative (your hard drive won't be erased or anything). bdelete makes the buffer unlisted, but doesn't purge marks, options, the buffer name, the buffer number, and so on. If you reload the buffer that information is retained (less what may be modified by autocommands), which may be useful because it allows you to keep using any marks, or ...


14

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 should still be listed. It is still open and in Vim's memory. The only way to make them go away is to run :bd or :bw (or :q of course). So, to answer your ...


13

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 works on the whole buffer by default or an optional range: command! -bar -range=% Foo execute <line1> . ',' . <line2> . 'y|@"'


13

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 buffer. A viewport is synonymous with a window. A tab page contains one or more windows. You can see what windows are in which tab with :tabs. A split is where ...


13

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 to fill each buffer with :read path/to/filename. Then, you'll have completely independent buffers. Saving them won't even update the initial file. In Vim, we ...


12

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 sessions by default in a centralised repository as per configuration on a per directory basis and loads them automatically when you launch vim without any arguments on ...


12

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 set and the file can be written. And you can use :enew! to discard unsaved changes in the current buffer.


12

By default, Vim will refuse to replace the current buffer with another one if it contains unsaved changes. You'll need to add the option below to your vimrc if you don't want that behavior (and you don't want that): set hidden Buffers are Vim's equivalent of documents in other programs. When you switch to another document, you switch to another buffer. ...


12

I've been noticing a flaw in my workflow when I'm editing a lot of buffers: I am on a buffer and I want to switch to another one of which I don't remember the name and which isn't the alternative buffer. So using :b# is not possible and using the completion with the command :b isn't convenient neither. On the contrary, I find tab-completion to be extremely ...


12

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, and with plenty side effects. Stay away if you can. Until now my preferred approach was to use filter() on range(1, bufnr('$')) to keep the buffers I'm ...


11

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 * call s:Log('BufReadPre') ... autocmd User * call s:Log('User') augroup END function! s:Log(eventName) abort silent execute '!echo '.a:eventName.' &...


11

:bd will do that. From the documentation: :[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 ...


10

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 with two versions of a file. The command will ask you what to do for each buffer the file of which has a changed timestamp. To disable this ...


10

I tried googling for this with different sets of keywords, and struck gold on one such attempt with this result: http://www.ibm.com/developerworks/library/l-vim-script-5/ Specifically, this part is relevant to the current question: For example, if you start Vim, edit a file named demo.txt, swap into Insert mode, type in some text, save the file, and ...


10

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. There are other variants you could try, like :Sexplore (horizontal split) or :Vexplore (vertical split). When your cursor is on a file, you can also hit o, v, ...


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