Hacker News new | ask | show | jobs
by dmix 4660 days ago
I have a scenario that I need help with in VIM: switching between 2-3 files while working on them simultaneously.

For ex: working on an HMTL+CSS+JS documents all at once. Assuming smaller screen where I can't have a vsplit screen.

How to I jump between them quickly? Buffers/tabs?

I love VIM but I haven't found a good tab/file switching solution. In Textmate tabbing was straight-forward. VIM doesn't seem well suited for tabs (especially with things like NERDTree and control-p).

7 comments

I don't have any plugins for this stuff. I just do:

    nnoremap <Leader>b :buffers<CR>:buffer<Space>
    nnoremap <Leader>n :bn<CR>
    nnoremap <Leader>p :bp<CR>
My <Leader> is space. So "space+b" opens up my buffers, I type a few letters that match a file and Tab to autocomplete it, then press enter to open it. I also use ctrl+shift+6 to quickly switch between the 2 most recent buffers.

So I'd give that a shot. If that doesn't work for you, try just ":tabe <file>" for your three files, then ctrl+pageup, ctrl+pagedown (or gt, gT) to quickly switch between them.

Thanks this is working for me.

I just switched b/p (since I use p w/ ctrl-p as a fuzzy finder to list all files).

For opening each file the first time:

https://github.com/kien/ctrlp.vim

For switching quickly between the 10 most recently used buffers:

https://github.com/vim-scripts/LustyJuggler

I suggest adding this to your .vimrc (the default mapping is <Leader>lj; my leader key is the spacebar, and I find space space to be much better):

let g:LustyJugglerDefaultMappings = 0

nnoremap <silent> <Leader><Leader> :LustyJuggler<CR>

For very quickly jumping between the current buffer, and the last buffer I have a mapping:

nnoremap <leader>dd <C-^>

because <C-^> is a bit awkward; <leader>dd could be anything that's very easy to type.

So most of the time I'm alternating between two files using the <C-^> mapping. When I need to jump to a less recently used buffer I double tap <space>, opening LustyJuggler, then double tap the appropriate home row key to jump to the buffer I want. If the buffer I want isn't in the ten most recently used, I open Ctrl-P, type enough characters from the file path of the file I want to make it the first choice, then hit enter.

Vim's tabs are not the same as TextMate tabs: they are workspaces, not 1-to-1 proxies for files. Don't try to use them like in other editors, it's an exercice in futility.

You can make use of command-line completion:

    :b <Tab>
which does partial words too:

    :b js<Tab> completes with teh name of your JS file
You can make command-line completion even faster:

    set wildcharm=<C-z>
    nnoremap <leader>b :b <C-z>
You can try quick, generic, mappings:

    nnoremap <PageUp> :bnext<CR>
    nnoremap <PageDown> :bprevious<CR>
You can try more precise ones:

    nnoremap <leader>1 :b1<CR>
    nnoremap <leader>2 :b2<CR>
    nnoremap <leader>3 :b3<CR>
    ...
to mimic TextMate.

You can try listing and choosing in one move:

    nnoremap gb :buffers<CR>:b<Space>
You can try the builtin Ctrl+^:

    3<C-^> is equivalent to :b3<CR>
You can remap it if you want:

    nnoremap & <C-^>
and do:

    1&
I swear by tabbar[1] as well as a few bindings to make switching between buffers faster. Specifically:

map <C-n> :bn<Cr>

map <C-p> :bp<Cr>

It probably helps that I've mapped caps-lock to ctrl so using ctrl is very convenient for me.

[1] http://www.vim.org/scripts/script.php?script_id=1338

Tabs in vim are different to tabs in other programs. You should be using buffers. This[1] StackOverflow answer is excellent. I use a plugin[2] to view my buffers in a bar.

1: http://stackoverflow.com/a/103590/1212338

2: https://github.com/bling/vim-bufferline

To be honest, I usually just have several terminals open and I alt-` (ubuntu) between them.

I have a friend who has a sweet setup with gvim that has a lot of tab and file management tools, so you might look into gvim, but I don't use it.

If you use tabs, you can use gt and gT in normal mode to switch to the next and previous tabs, respectively.