98

I've tried MiniBufExplorer, but I usually end up with several windows showing it or close it altogether. What I'd like is something like LustyJuggler with incremental search, the way I switch between buffers in Emacs. Surely there is a script like this?

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487

16 Answers16

79

I used to use a combination of tabs and multiple gvim instances, keeping groups of related files as tabs in each instance. So long as I didn't end up with too many tabs in one instance, the tab bar shows you the name of each file you're editing at a glance.

Then I read a post by Jamis Buck on how he switched from TextMate back to vim, and learned some great tricks:

  • Ctrl+w s and Ctrl+w v to split the current window
  • Ctrl+6 to switch back and forth between two buffers in the same window.
  • the awesome fuzzyfinder.vim which gives you auto-completing search of files in your current directory or of buffers you currently have open
  • Jamis' own fuzzy_file_finder and fuzzyfinder_textmate, which slightly modify how fuzzyfinder works to behave more like a similar feature in TextMate (as far as I can tell, the difference is that it matches anywhere in the filename instead of only from the start). Watch this video to see it in action.

Now I just have one gvim instance, maximised, and split it into multiple windows so I can see several files at once. I bound Ctrl+F to fuzzyfinder\_textmate, so now if I type (say) Ctrl+F mod/usob it opens up app/models/user\_observer.rb. I almost never bother with tabs any more.

Update 2010/08/07

While fuzzyfinder\_textmate remains awesome, as Casey points out in the comments, it's no longer maintained. Also, it (and/or fuzzyfinder.vim) gets a bit slow and unstable when working with large projects (lots of directories or files), so I've been looking for an alternative.

Fortunately, there seems to be a very nice alternative in the form of Wincent Colaiuta's Command-T plugin. This has very similar (if not slightly better) behaviour to fuzzyfinder\_textmate, but is noticeably faster; it also has nice features like being able to open the found file in a split or vertical split. Thanks (and upvotes!) to David Rivers for pointing to it.

Kevin Bowen
  • 1,625
  • 2
  • 23
  • 27
Sam Stokes
  • 14,617
  • 7
  • 36
  • 33
33

I use the basics - ':ls' + ':bn'/':bp' + ':b <part-of-name>'

orip
  • 73,323
  • 21
  • 116
  • 148
  • 8
    :-) I never thought I'd see "simple" and "':ls' + ':bn'/':bp' + ':b '" in the same sentence. – paxdiablo Nov 29 '08 at 12:22
  • 16
    Pax, I still don't see it. :-) – csexton Nov 29 '08 at 13:54
  • This, plus `:map :bn` and `:map :bp`, since `C-n` and `C-p` are fast to type and allow cycling between buffers if you press them repeatedly. (I never used them for their original purpose.) It's like cycling between browser tabs with `C-PgUp` and `C-PgDn` or whatever shortcut your browser has. – Tobia Sep 30 '16 at 15:28
  • 1
    Ctrl+N is the standard for 'New File' in the majority of other editors and Ctrl+P is the paste command, so for most people these will be really confusing. Tim Pope's [vim-unimpaired](https://github.com/tpope/vim-unimpaired) gives you `[b` for the previous buffer and `]b` for the next buffer. Plus loads of other goodies that use the concept of `]` for next and `[` for previous. – icc97 Aug 19 '17 at 09:25
33

I like "ctrl-w s" and "ctlr-w v" to split the window. Then I map the movement keys (h, j, k, l) with ctrl held down to move between the split windows:

" Map ctrl-movement keys to window switching
map <C-k> <C-w><Up>
map <C-j> <C-w><Down>
map <C-l> <C-w><Right>
map <C-h> <C-w><Left>

Having to move my hand over to the arrow keys is annoying.

Next, I set up ctlr-tab to switch between buffers in the current window (like a lot of other environments):

" Switch to alternate file
map <C-Tab> :bnext<cr>
map <C-S-Tab> :bprevious<cr>

These have worked pretty well for me over the last several years although vim always has more secrets than you can know.

Dave Ray
  • 39,616
  • 7
  • 83
  • 82
  • 3
    Your comment "switch to alternate file" is misleading because Vim has an alternate file already, it's the previous file you were on, and you can switch the current (%) and alternate (#) files easily using ctrl-6. – graywh Apr 21 '09 at 17:45
  • 10
    Also, with hjkl will switch windows--arrow keys not required. – graywh Apr 21 '09 at 17:46
  • 4
    is asking for RSI. I have mapped to `:bp` and to `:bn` – puk Jan 09 '12 at 12:23
  • 4
    Remapping seems like a poor choice in normal mode, as that is commonly used (for concatenating lines). – Michael Percy Oct 12 '13 at 01:24
24

I have been using Wincent Colaiuta's Command-T vim plugin for a couple months now. Wincent wrote the parts of it that need to be fast in C, and I must say that it is! And, I think its file pattern matching logic is even better than Textmate's Command-T. Check out the screencast.

The Command-T plug-in for VIM provides an extremely fast, intuitive mechanism for opening files with a minimal number of keystrokes. It's named "Command-T" because it is inspired by the "Go to File" window bound to Command-T in TextMate.

Files are selected by typing characters that appear in their paths, and are ordered by an algorithm which knows that characters that appear in certain locations (for example, immediately after a path separator) should be given more weight.

Easier buffer switching contains many useful tips. I have adapted the following to my .vimrc, which does buffer-name auto-completion, maps the most useful buffer-switching commands to my <Leader> and left-side home row keys, and shows the current buffer number in the status line:

"" Tab triggers buffer-name auto-completion
set wildchar=<Tab> wildmenu wildmode=full

let mapleader = ","

map <Leader>t :CommandT<Return>
map <Leader>a :bprev<Return>
map <Leader>s :bnext<Return>
map <Leader>d :bd<Return>
map <Leader>f :b 

"" Show the buffer number in the status line.
set laststatus=2 statusline=%02n:%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P

I also use MiniBufExplorer, which provides a compact listing of each listed buffer in its own horizontal split up top.

David Rivers
  • 2,896
  • 1
  • 31
  • 39
18
nmap <Leader>bb :ls<CR>:buffer<Space>

and nobody mentioned nice plugin ctrlp.vim. Using this plugin you can search buffer by name.

KOlegA
  • 638
  • 6
  • 8
15

I use

CTRL-J for next buffer

CTRL-K for previous buffer

CTRL-L for next tab

CTRL-H for previous tab

Here is the .vimrc configuration:

map <C-J> :bnext<CR>
map <C-K> :bprev<CR>
map <C-L> :tabn<CR>
map <C-H> :tabp<CR>

See http://syskall.com/my-biggest-vim-productivity-boost/

Olivier Lalonde
  • 19,423
  • 28
  • 76
  • 91
6

I've recently gone more minimalistic.

To cycle buffers I use ]b and [b from unimpaired: https://github.com/tpope/vim-unimpaired

To jump straight to an open buffer just use Vim's tab completion with :b. A few letters is enough to get to any open buffer with a tab or two.

Similarly to open buffers I use :e with relative paths and tab complete.

I also use :ls occasionally to see what buffers I have open (and to check their modified status).

To get rid of a buffer I use :bw to wipe the buffer. I usually make a temporary split and change buffers to preserve my layout though since :bw also closes the active window.

All the minibuf things I tried just ended up annoying me, and I don't want some smart-matching thing opening random files for me. If I really need to browse for something I use NERDtree (:e .).

IDK, Lately I also dropped Yankring (because it screws up xp) and started using registers, and I recently decided the f/t movements are the greatest thing ever...

kitsu.eb
  • 2,996
  • 1
  • 26
  • 28
  • This is all great advice. I mapped `ll` to `:ls`. Also it's really useful to make sure that you have `set wildmenu` in your `.vimrc` as then you get a small menu that comes up with all the alternatives for `:b` – icc97 Aug 19 '17 at 09:31
6
imap <A-1> <Esc>:tabn 1<CR>i
imap <A-2> <Esc>:tabn 2<CR>i
imap <A-3> <Esc>:tabn 3<CR>i
imap <A-4> <Esc>:tabn 4<CR>i
imap <A-5> <Esc>:tabn 5<CR>i
imap <A-6> <Esc>:tabn 6<CR>i
imap <A-7> <Esc>:tabn 7<CR>i
imap <A-8> <Esc>:tabn 8<CR>i
imap <A-9> <Esc>:tabn 9<CR>i

map <A-1> :tabn 1<CR>
map <A-2> :tabn 2<CR>
map <A-3> :tabn 3<CR>
map <A-4> :tabn 4<CR>
map <A-5> :tabn 5<CR>
map <A-6> :tabn 6<CR>
map <A-7> :tabn 7<CR>
map <A-8> :tabn 8<CR>
map <A-9> :tabn 9<CR>
Terminus
  • 902
  • 2
  • 10
  • 21
  • 1
    The most interesting thing with Vim is that you may learn something every day ! – Luc M Jul 07 '09 at 14:43
  • The question is about *buffers*, not *tabs*... but you can `:tab sball` first, or instead of using `:tabn` you should use `:buffer` – pera Oct 19 '13 at 21:07
  • Yes! This was what I was looking for, whether anyone was using the Alt- mappings or if those had hidden traps (like the Ctrl- ones do). I'm planning to also make use of the second code sample from https://vim.fandom.com/wiki/Easier_buffer_switching#Mappings_for_buffer_number (basically, use a `while` loop and `execute`) to make this more concise and easier to work with. – Sundar R Mar 15 '21 at 18:27
4

To list and switch between buffers I use:

nnoremap <Leader>l :ls<CR>:b<space>

To switch between buffers:

map <Leader>n :bn<CR>
map <Leader>p :bp<CR>
diegoviola
  • 61
  • 1
2

The excellent Buffer Explorer, the be has gotten to be such strong muscle memory that I find myself wishing I could use it in other applications. I find it to be extremely fast when actively editing more than two files.

csexton
  • 24,061
  • 15
  • 54
  • 57
2

I use tselectbuffer. It's really fast and unlike bufexplorer doesn't take space in your window. It also has a incremental search.I tried minibufexplorer and I found the navigation in the buffer a bit difficult.

anita
  • 21
  • 1
2

I've spent quite a while building my .vimrc to work with this HTML::Mason project I've been on for four years, so I have an odd mix of tabs and split windows. For your viewing enjoyment:

map ;o :Sex <CR>
map <C-J> <C-W>j
map <C-K> <C-W>k
map <C-l> <C-W>l
map <C-h> <C-W>h
map ;] :tabnext<CR>
map ;[ :tabprev<CR>
map <C-t> :tabe +"browse ."<CR>
map <C-O> :NERDTreeToggle ~/curr/trunk/<CR>
Jack M.
  • 30,350
  • 7
  • 55
  • 67
2

I use tselectbuffer. It's really fast and unlike bufexplorer doesn't take space in your window. It also has a incremental search.I tried minibufexplorer and I found the navigation in the buffer a bit difficult.

Taurus Olson
  • 3,153
  • 2
  • 26
  • 21
  • Thanks for the pointer, now I use both. The defaults for BufExplorer are `,be`, `,bs`, `,bv`, so I mapped TSelectBuffer to `,bb`. – Brady Trainor Jul 22 '14 at 03:21
2

I have mapped <S-J> and <S-K> to :bp and :bn, although I admit I don't use it as the number of files is greater than 10. I have then mapped <C-J> and <C-K> to Gnome Terminal's previous and next tabs, and I usually run 4 instances of vim for each of the 4 different projects I work on. I still really wish next and previous buffer would go to the history of buffers I have been working on, and not the order int he buffer list.

puk
  • 16,318
  • 29
  • 119
  • 199
0

When there are several buffers open in a Vim session, it can become difficult to keep track of the buffers and their respective buffer numbers. If this is the case, switching to a different file can be made easier using a simple map:

:nnoremap (F5) :buffers(CR):buffer(Space)

vim.wikia.com

castor
  • 1
  • 1
0

i use simple :vsplit with ^W+w/^W+r and :tabnew with Ctrl+Alt+PgUp/PgDown key combinations.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
  • 1
    I have to agree with what [Zathrus](http://stackoverflow.com/users/16220/zathrus) said [here](http://stackoverflow.com/questions/102384/using-vims-tabs-like-buffers#103590). Tabs in Vim (or Emacs with TabBar for that matter) simply do not work like in the usual tabbed interfaces. – Alexey Romanov Nov 29 '08 at 10:59