Skip to main content
Share Your Experience: Take the 2024 Developer Survey
71 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
43 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,790
37 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
  • 50.1k
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,780
22 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
  • 17.4k
20 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
  • 335
19 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,711
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
  • 17.4k
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

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
  • 311
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
  • 41.1k
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,780
14 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
  • 41.1k
14 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
  • 26.4k
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.9k
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,790
12 votes

Reopen the most recently closed buffer

You can try CTRL-O, this will go to last cursor posision in jump list, even for closed buffers. This will even work from fresh vim instance, so you can open last file with that.
grodzik's user avatar
  • 4,638
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,582
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

What is a scratch window?

A 'scratch' buffer is just an informal term for a place to type arbitrary temporary content. Following the accepted answer and another question, I created the following function in my vimrc. The ...
NeilG's user avatar
  • 201
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
  • 26.4k
9 votes
Accepted

Closing all buffers that represent .js files?

Your question is answered on a duplicate from one on StackOverflow: Use <C-A> to expand the *.js on the command line. This way :bd *.js will become :bd file1.js file2.js file42.js. My loop-free ...
Luc Hermitte's user avatar
  • 17.4k
9 votes
Accepted

Change buffer focus on enter?

You can change your autocommand to execute <c-w>w after executing NERDTree. To do so you can use wincmd which is the equivalent of pressing <C-w> but with a command (see :h :wincmd) ...
statox's user avatar
  • 50.1k
9 votes
Accepted

Switch to viewport by name?

You probably want to set the switchbuf option to "useopen" and then use :sbuffer NAME instead of :buffer to move your cursor to a window that has this buffer loaded. See :help switchbuf for more ...
GAgbo's user avatar
  • 106
9 votes

Is it possible to simultaneously edit one file in two views?

I think you need to read :h windows-intro which explains the concepts of buffer, window and tab, summarized like this: Summary: A buffer is the in-memory text of a file. A window is a viewport ...
statox's user avatar
  • 50.1k
9 votes

How can I ensure Vim doesn't quit entirely when the last window is closed, unless it's empty?

This is something I wanted when I first started using Vim, but I no longer do. From looking at your account, I can see you've been using Vim for several years, but for the benefit of other readers who ...
Rich's user avatar
  • 32.1k
9 votes
Accepted

How can I view the name of the swapfile being used for the current buffer?

:swapname This will show the filename of the swapfile for the current buffer. (For example: /c/path/.swu) I found this (after a few tries) by typeing :swTAB. I also found this question: VIM swap-...
Thomas F's user avatar
  • 256

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