Hot answers tagged

67 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'...
Peter Rincker's user avatar
48 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 ...
Peter Rincker's user avatar
47 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. ...
Soylent17's user avatar
  • 571
42 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 ...
Tommy A's user avatar
  • 6,710
36 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 ...
statox's user avatar
  • 49.4k
35 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:...
muru's user avatar
  • 24.7k
25 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 ...
kevinlawler's user avatar
24 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 :...
Hotschke's user avatar
  • 4,640
21 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,...
Luc Hermitte's user avatar
19 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 ...
Luc Hermitte's 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 ...
Bruno Bronosky's user avatar
18 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 ...
Tom Hale's user avatar
  • 2,601
18 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. ...
wbogacz's user avatar
  • 315
17 votes

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

You can also use ctrl + o to go to the previous jump list, which might be buffer, going backwards. You can keep pressing the same command to go to the previous, previous buffer/jump list. You can also ...
Kaka Ruto's user avatar
  • 301
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. ...
saginaw's user avatar
  • 6,706
16 votes
Accepted

Vim hide buffer like in emacs?

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 ...
romainl's user avatar
  • 39.2k
15 votes
Accepted

How should the option 'switchbuf' work?

'switchbuf' is only honored by quickfix commands (like :cc) and buffer-splitting commands (like :sb). It doesn't affect :buffer and other "regular" buffer-related commands.
romainl's user avatar
  • 39.2k
14 votes

Fastest way to switch to a buffer in Vim?

[b and ]b from plugin vim-unimpaired (> 3k ★) If you happen to have installed vim-unimpaired, you can use [b (:bp[revious]) and ]b (:bn[ext]) for cycling through open buffers. No need to define ...
Hotschke's user avatar
  • 4,640
13 votes
Accepted

What is a more efficient way to use buffers?

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 ...
romainl's user avatar
  • 39.2k
13 votes
Accepted

different search patterns per window / buffer

You can use :autocmds to switch the current search pattern (register /) as you move along windows and buffers, but Vim will never highlight different patterns at the same time, as this still is a ...
Ingo Karkat's user avatar
  • 17.7k
13 votes
Accepted

Can I use syntax highlighting on an unwritten buffer?

In addition to setting the syntax, setting the filetype brings indent options (if you have those on), mappings, and other filetype specific vimscript to bear on your code. So I recommend :set ...
D. Ben Knoble's user avatar
  • 25.3k
12 votes
Accepted

how to close tab and remove this file from buffers

: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 ...
fruglemonkey's user avatar
  • 1,530
12 votes
Accepted

Is it possible to obtain the displayable area, width and height of current buffer in Vim?

These are the different ways (that I know of) to get dimensional information from Vim. Vim &columns and &lines will give you the total width and height of the screen Vim is displayed in. ...
Tommy A's user avatar
  • 6,710
12 votes
Accepted

Reopen the most recently closed buffer

I played around with this a bit and the following seems to work for the cases I tested, including closing a window split and a tab with one window using :q. augroup bufclosetrack au! autocmd ...
John O'M.'s user avatar
  • 8,462
11 votes
Accepted

Changing between the active buffer and the alternate buffer

CTRL-6 can be used to edit the alternate file - tested on OS X by changing the input source from U.S. to Spanish ISO. If you find yourself wanting to edit the alternate file often, you could consider ...
djjcast's user avatar
  • 911
10 votes

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

This is the first result show up on google search about 'Vim event order'. So, i think it's worth providing the actual answer. Using the command from @nobe4 answer, this is the result: Opening a new ...
C. Martins's user avatar
10 votes
Accepted

What does the '+' symbol mean when opening a buffer?

Should have looked a bit harder before asking, I found my answer: :help +cmd *+cmd* *[+cmd]* The [+cmd] argument can be used to position the cursor in the newly opened ...
Brett Y's user avatar
  • 538
10 votes
Accepted

Using :Explore without closing current buffer

I usually use a following flow when I want to open the file (while keeping the current file): Make a new vertical / horizontal split Open netrw from in the new split Select another file As mentioned ...
Borys Serebrov's user avatar
10 votes

How to clear all contents of a file

Fastest is :%delete Which can be abbreviated to :%d The % is a range standing for “whole file.” If you want to use a particular register (e.g., black hole): :%d_
D. Ben Knoble's user avatar
  • 25.3k

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