Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickly switching buffers in Vim normal mode

Tags:

vim

Recently I found out that I'm "using tabs incorrectly" in Vim. I've been trying to just use buffers in Vim since, assisted through MiniBufExplorer, but I find it painful because of how many keystrokes it takes to change buffers from normal mode. With tabs, I can just do gt or gT to hop back and forth between tabs in normal mode, and I can also do NUMBERgt to go to a specific tab.

With buffers, I either have to enter command mode with :bn, :bp, or with MiniBufExplorer, use Ctrl + k or Ctrl + Up to hop up to the buffer window, scroll left or right with h and l and then hit Enter to select the buffer I want. Or I can do something involving a leader sequence, but it always requires removing multiple fingers away from home row. That's a real pain.

How can I get something equivalent switching tabs in normal mode to switch buffers in normal mode, so I can do something like gn/gp for :bn/:bp and NUMBERgn for :buf NUMBER?

like image 258
gotgenes Avatar asked Apr 05 '11 21:04

gotgenes


People also ask

How do I toggle buffers in Vim?

To toggle between the current and the last buffer use the Ctrl-^ (normal mode) command (on most keyboards, hold down Ctrl and press the 6 key on the main keyboard).

How do I close all buffers in Vim?

Just put it to your . vim/plugin directory and then use :BufOnly command to close all buffers but the active one.

How do I switch between tabs in Vim?

To switch to the next tab, use :tabn, and to switch to the previous tab, use :tabp (short for tabnext and tabprevious respectively). You can also jump over tabs by using :tabn 2, which will move to the second next tab. To jump to the first tab, use :tabr (tabrewind) and to jump to the last tab use :tabl (tablast).

How does vi editor use buffers?

Whenever you delete something from a file, vi keeps a copy in a temporary file called the general buffer. You can also delete or copy lines into temporary files called named buffers that will let you reuse those lines during your current vi work session.


1 Answers

Add this to your .vimrc

map gn :bn<cr> map gp :bp<cr> map gd :bd<cr>   

Note that you are remapping gp and gd, but maybe you don't care about that (:help gp, :help gd).

For more information on how to map key strokes see :help map-overview and :help map.txt.

Btw, I personally use <leader> instead of g in the mapping. My <leader> is set to ;. This puts my <leader> key on the home row which makes me willing to map all kinds of stuff using <leader>. :help mapleader if you want to change your <leader> key.

like image 107
Eric Johnson Avatar answered Oct 13 '22 04:10

Eric Johnson