
Vim Splits – Move Faster and More Naturally - adarshpandit
http://robots.thoughtbot.com/post/48275867281/vim-splits-move-faster-and-more-naturally
======
btipling
I used to use splits, then I used tabs + splits, now I just use buffers. The
only time splits come into it are for quickfix or diffs. Just get used to
buffers. Lots of vim commands will work with buffers but not with splits or
tabs.

People new to vim will immediately jump on splits or tabs because of how their
previous editor work flow worked, but you aren't going to unlock the full
power of vim until you get used to buffers.

:b# last buffer used

:b num Switch to various buffers by number

:buffers See a list of buffers

:bd delete buffer

^^ map those to keys

cntrlp for easy buffer switching and opening:
<https://github.com/kien/ctrlp.vim>

~~~
burntsushi
I agree with regard to tabs, but I always felt like buffers were orthogonal to
splits. I use both all of the time. Buffers to manage a working set of files
and splits to manage the files that I want to viewable. I frequently find
myself using splits to write new code while referencing other parts of the
code base (or even the same file).

~~~
terhechte
Same here, I can quickly have all the functions, data, and docs visible, that
I need for the particular code that I'm currently writing. Having to switch
back and forth would only confuse me. Usually, the biggest problem I have with
other IDEs is that they don't allow me to split as freely as vim / emacs do.
XCode is shockingly stupid about it, for example.

------
phillmv
I am SO SATISFIED with my splits key remapping I have to share it:

    
    
        map <M-right> <C-w>l
        map <M-left> <C-w>h
    
        map <M-down> <C-w>j
        map <M-up> <C-w>k
    
        map <M-,> :split<CR><C-w>j " Horizontal split
        map <M-.> :vsplit<CR><C-w>l " Vertical split
        map <M-/> :close<CR>
    
        map <M-<> <C-w>K " Convert vertical to horizontal split
        map <M->> <C-w>L " Convert horizontal to vertical split

~~~
aerique
I moved over to evil-mode (Vim emulation) in Emacs a couple of months ago and
do most things the Vim way but some things are more efficient to do in a non-
Vim manner and this is one of them.

Although I use M-- and M-| for vertical and horizontal splits.

~~~
gnosis
As someone who's been using vim for about 20 years, here are the problems I
have with any vi/vim emulation modes in emacs:

* Configuring emacs still requires learning a lot about emacs (which takes a lot of time).

* These vi/vim emulation modes doen't touch SLIME's keybindings, afaik. So, to make them less finger-twisting will require manual rebinding (which takes yet more time).

* Most emacs tutorials assume you're using the standard key bindings. So you still have to learn them.

All of the above make the various vi/vim emulation modes fine for people who
already know emacs, or who are willing to sink a ton of time in learning it
and reconfiguring it. But they are far less attractive to vim users who
already have a nice environment set up in vim and feel comfortable in it.

I guess they're still better than nothing. If I had to use emacs, I'd
certainly use something like evil mode. But evil mode alone is not enough to
make a vim veteran feel comfortable in emacs.

~~~
aerique
You are right on all points, I come however from 10 years of Vim and 15 years
of Emacs so it is a natural fit.

Vim emulation modes are mostly for people who prefer a certain environment,
like f.e. Emacs but want more efficient keybindings.

------
IgorPartola
I have mapped the Tab key to move between split windows. I also mapped Ctrl+T
to open a new tab, and CTRL-H and CTRL-L to move to the left/right tab
respectively. It works well and seem pretty natural. I suppose if I had a
giant monitor I could use more splits, but realistically more than two of them
feels very cramped. My .vimrc is here:
[https://raw.github.com/ipartola/ipartola-bash-and-
vim/master...](https://raw.github.com/ipartola/ipartola-bash-and-
vim/master/.vimrc)

The relevant parts are:

    
    
      " shortcuts
      map <Tab> <C-W><C-W>
      
      " Tabs
      map <C-t> <Esc>:tabnew<CR>
      map <C-l> :tabnext<CR>
      map <C-h> :tabprevious<CR>

~~~
grayrest
Splits came to vim way before tabs did as a result I use splits as vertical
pseudo tabs:

    
    
        nnoremap <c-j> <c-w>j<c-w>_
        nnoremap <c-k> <c-w>k<c-w>_
    

Which does both split navigation and split maximizing. I could probably switch
to tabs but I'm happy with how things work and I don't really see any
advantage over how I'm doing things now.

------
terhechte
In addition to that, I can heartily recommend the ZoomWindowPlugin:

[https://github.com/skeept/dotvim/blob/master/plugin/ZoomWinP...](https://github.com/skeept/dotvim/blob/master/plugin/ZoomWinPlugin.vim)

Basically, if you have multiple split windows, it allows you to do a cmd+O to
zoom the current split view (i.e. making it the only view visible). That's
neat if there's something tricky going on (say a weird bug) and you want to
focus just on this piece of code, without seeing anything else. Once you're
done, you can hit cmd+O again, and your old split setup comes back. I love
this plugin.

~~~
AlexSolution
Any advantage to this plugin over mapping one key to ':tabopen' and another to
':tabclose %'? I have this in my .vimrc and it seems to get the same results:

    
    
      nmap <F4> :tabedit %<CR>
      nmap <F5> :tabclose<CR>

~~~
leephillips
Splits are not tabs.

Edit re below: OK, good point. But I would still prefer the plugin (and plan
to install it) because I just don't use tabs, preferring to stick with splits.

~~~
mkenyon
True, but AlexSolution is saying that you can get the same effect without a
plugin.

Open your split as a new tab. The new tab (i.e. that split) takes up all of
vim. When you want to return to your splits, just close that tab.

------
dionidium
Also, I use dwm for tiled windows:

<http://www.vim.org/scripts/script.php?script_id=4186>

~~~
jlgreco
Awesome (another tiling WM), tmux with panes, and vim with windows gives me
three layers of organization. Really great if you are into self-imposed
structure.

------
randomdrake
Mastering window splitting and tabbing allows one to truly step into the flow
that Vim has to offer. However, it doesn't stop at simply dealing with opening
files in new panes.

    
    
       :vsplit .
    

I use this all the time when I need to find a file or I'm not sure where
something is. It just opens the file browser in a new window split. It's
extremely handy. Once you've got it open, it's important to note that you can
use your regular commands in the file browser as you would in Vim.

    
    
        /beginning of file name
    

This allows for a really nice and fast file browsing experience like when
you're searching through code. I prefer it much over cd'ing around on the
command line. I can see the directory or file I need to go to, type /, the
first few letters, and hit enter.

    
    
        vim .
    

From the command line, you can simply open Vim as a file browser and use it to
browse around your project. I do this to get projects in my head sometimes
when I know I'm going to be dealing with multiple parts of the system at once.
I can browse around a bit and get things open in tabs or splits and go to
town.

    
    
        set autochdir
    

This is a handy configurable I wish I would've picked up a long time ago that,
in my opinion, perfectly accents heavily using filesystem browsing in Vim. If
you're someone that splits or tabs around a lot in Vim, this is a really handy
change. Instead of your directory always staying relative to the first file
you opened, the directory changes depending on what file you're working with.

This means you can do something like:

    
    
        :vsplit ../../models/foo.php
    

And when you're working in foo.php, you realize you need to make a change to
the bar model, bar.php, you can simply do:

    
    
        :vsplit bar.php 
    

Instead of having to do:

    
    
        :vsplit ../../models/bar.php
    

Going beyond filesystem awesomeness with Vim and window management, you can
also use it to easily create new files as you're going along. This is
particularly useful for new projects where you're flowing along generating new
models. Working on a model and suddenly realize you need to create another
one?

    
    
        :vsplit newfilename.php
    

This will create the new file in your buffer and you can start typing. If you
realize you made a mistake, simply close the pane without writing at the file
will have never been written. This can also be handy when you need to type
something out or keep quick notes. Have a little pane off to the side with
notes about what you're working on. Maybe it's some numbers you need to keep
in your head or maybe someone interrupted you with a request while you were
knee-deep in terminal land.

    
    
        :vsplit notes
    

Let Vim be your flow and your editor.

~~~
wahnfrieden
Isn't there some way of using % to open a file relative to the current one
perhaps? To get the best of both worlds.

~~~
alphard
I'm using the following to expand "%%" in command mode to the full path of the
current open file's directory:

    
    
      cabbr <expr> %% expand('%:p:h')
    

You could also use :. instead of :p to get the relative path, see :help c_%
and :help filename_modifiers

~~~
wahnfrieden
Wow, elegant. Thank you!

------
indrekju
" Edit another file in the same directory as the current file

" uses expression to extract path from current file's path

map <leader>e :e <C-R>=expand("%:p:h") . '/'<CR><C-M>

map <leader>s :split <C-R>=expand("%:p:h") . '/'<CR><C-M>

map <leader>v :vnew <C-R>=expand("%:p:h") . '/'<CR><C-M>

------
5h
After years of abusing my little finger for using Ctrl & the RSI that goes
with it, I tend to use alt/meta with my thumb more and more nowadays, for this
I make it usable in insert mode also, this is probably one of the oldest bits
of my .vimrc

    
    
        "quick window movements
        map <a-w>  <c-w>
        map <a-l> <c-w>l
        map <a-k> <c-w>k
        map <a-j> <c-w>j
        map <a-h> <c-w>h
        imap <a-l> <esc><c-w>li
        imap <a-k> <esc><c-w>ki
        imap <a-j> <esc><c-w>ji
        imap <a-h> <esc><c-w>hi

~~~
graywh
Only problem with those imaps is that now you can't type ì, ë, ê, or è. Vim is
pretty dumb about utf-8.

I'd also suggest using <C-o> instead of the <Esc>...i dance.

Edit: "The way [Vim] stores alt+letter in its input queue collides with
UTF-8/Unicode handling" -- LeoNerd in freenode/#vim at 2013-04-15 15:22:29

~~~
leephillips
"Vim is pretty dumb about utf-8."

It might not work the way you prefer using your favorite input method, but vim
allows you to enter any utf-8 character, and even has mnemonic digraphs for
many you are most likely to want. To get è, for example, in input mode just
type <ctl>-k`e.

~~~
graywh
Using the digraph _will_ get around the insert-mode alt-key maps.

------
Nick_C
If you use splits a lot, you might be interested in the Golden Ratio plugin.
It resizes the active window to be about (user-definable) two-thirds of the
screen. So you jump between windows and get max screen space for the one
you're working on.

Sometimes you don't want it, so you can toggle it off.

<http://www.vim.org/scripts/script.php?script_id=3690>

------
jonzjia
I forgot where I found it, but someone uses | and _ to split windows in vim,
which I find more intuitive and easier to use:

    
    
        " Split window vertically
        nnoremap <Bar> <C-W>v<C-W><Right>
        " Split window horizontally 
        nnoremap _ <C-W>s<C-W><Down>
        " Use tab to toggle between windows
        nnoremap <Tab> <C-W>w

------
tammer
This is great, but I'm surprised it leaves out the single best split hotkey:

C-w C-w

This moves to the last used split, i.e. it's a quick way to jump between two.

------
crucialfelix
My happiest thing lately is to use NERDtree and then on selecting a bookmark,
folder or file to then hit t to open that in a new tab.

So I have tabs and those are then split. Each subject of work or exploration
has a tab with a few splits. I end up with much less clutter than sublime
text. Easy to close out a tab.

Also ctrl-w v is my favorite way to split.

------
daGrevis
I'm interested to increase "the speed" of resizing splits. It's annoying to
press `<C-w> <` a few times just to move split a few pixels...

~~~
BostX
I use Alt-numpad keys. In my case it works quite fast:

    
    
        "  Decrease height / width
        nnoremap <A--> <C-W>-
        nnoremap <A-/> <C-W><
        " Increase height / width
        nnoremap <A-+> <C-W>+
        nnoremap <A-*> <C-W>>

------
ramblerman
<http://agillo.net/simple-vim-window-management/>

------
tobiasu
au VimResized * exe "normal! \<c-w>="

~~~
graywh
You can change that to

au VimResized * wincmd =

