Hacker News new | past | comments | ask | show | jobs | submit login
Vim Splits – Move Faster and More Naturally (thoughtbot.com)
139 points by adarshpandit on April 18, 2013 | hide | past | favorite | 65 comments



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


I use buffers and splits.

My work session has about a hundred or two "open" files in the buffer, and I split and un-split windows often so that I can view multiple files at once.

But my most common way to change current file is :b <first few letters + tab complete>

I occasionally prune my buffers for files that have been deleted, but it's mostly unnecessary to. When I close it, I just save the session in a file and re-open the session.


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).


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.


:ls works too.


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


You should use `nnoremap` instead of just `map`: http://learnvimscriptthehardway.stevelosh.com/chapters/05.ht...

HTH


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.


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.


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.


+1 For evil-mode. I'm still using MacVim and Emacs+Evil side by side because many things that my vim does (via plugins etc) my emacs/evil doesn't do yet and I lack the time to research it all. But having elisp for extension is such a joy to use. I've dabbled a bit with evil, too, and I really like how easy it is to extend it.


Funny, I just started using evil too. You might want to checkout prelude/projectile if you haven't yet. Also, less obviously for a vimmer, keychord.el. I'm able to use 'jk' for ESC, for instance. For others interested, the brief evil manual is worth peeking at: https://gitorious.org/evil/evil/blobs/raw/doc/doc/evil.pdf. Motions, text objects, and operators in theory are easy to add in elisp, which is cool. For this reason, evil seems unlike other vim 'emulators' that I'm aware of (could just be ignorance, obviously). I'm really digging evil though.


Yes, I feel similar. I've tried way too many vim 'emulators', and the only other one which comes close to that 'vim' feeling is XVim for XCode. However, neither that one, nor all the others feel as easily extendable (with the exception of Vico, a native Mac vim clone that uses Nu (a cocoa lisp) for extension, but Vico even though it's easily extendable lacks a ton of vim commands)


I have somewhat similar, though I map the <C-w>hjkl keys to just the arrow keys themselves with no modifiers. The only problem is when other people are poking around at one of my vim sessions it trips them up.


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...

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>


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.


To switch b/w tabs, I find Ctrl + PageUp/PageDown a lot more convenient, since it's also standard in Browsers/other text editors. :D


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

https://github.com/skeept/dotvim/blob/master/plugin/ZoomWinP...

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.


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>


I already have a fixed set of tabs open for different use cases, and switch between them frequently. Adding a new tab to that temporarily would probably confuse me. I may try the tabedit / tabclose solution nevertheless though, I hadn't thought about doing it that way. The advantage being that I'd have one plugin less :)


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.


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.


A single mapping instead of two.



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.


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.


It's worth noting that :vsplit can and should be shortened to :vs

Also, depending on how you like to work, keeping a stable working directory can be very useful:

    :vs **/bar<Tab>
is somehow less mentally taxing than:

    :vs ../../models/bar.php


Can you point me to docs on in file searching? Those symbols are predictably hard to google. Based on a little messing around, it appears that * is what I expect from bash, and can also include directory separators.


Google is not the right place to search for help: Vim comes with an extensive and easily searchable documentation.

    :h **
is all you need to get your answer.


Google helps when you don't exactly know what's the name of the thing you're looking for. That's in my case 85% of all searches.


You could try :helpgrep foo next time.


Wow. I did not think vim help would deal with symbols well, either. Thanks!


    :h starstar
roughly means the same thing as -r or -R.


    :vsplit .
On this point, it's worth noting that users of the NERDTree plugin[1] can set it to work with the above style of use as well:

    " Replace netrw with secondary NERDTree
    let NERDTreeHijackNetrw=1
[1] https://github.com/scrooloose/nerdtree/


Thanks for this, I was searching for this exact thing the other day and couldn't find it.


I love using splits, but your `set autochdir` tip is amazing. I hadn't know about it before today. Thank you!


If you like 'set autochdir', then you should try this plugin https://github.com/amiorin/vim-project


Note that you can abbreviate :vsplit as just :vsp .


You can even drop the p (and spaces if you are using arguments) So to open a vertical split containing a file browser in the current dir:

    :vs.
I use this all the time.


My .vimrc used to have:

    nnoremap - :Sexplore<CR>
Which opens netrw when you hit the - key. I now have it mapped to ctrlp instead (ctrl+enter in ctrlp to split open).


Nice! Someone shared a comment on the blog, `:Vex` and `:Sex` open a new split with an explorer, much like `:vsplit .`

`set autochdir` is very nice. Thanks for sharing!


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


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


Wow, elegant. Thank you!


I use:

    map <leader>e :e <C-r>=expand("%:p:h") . "/" <cr>
you can make one that splits:

    map <leader>v :vs <C-r>=expand("%:p:h") . "/" <cr>


Your last tip is a gem! Combined with :10sp, it's the perfect recipe for a mini-TODO list.


" 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>


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


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


I think I solved that problem quite elegantly :) However I still have a small TODO regarding printing from cygwin:

    set encoding=utf-8
    " TODO check if printing of utf-8 chars works with 'set fileencoding=utf-8'
    set fileencoding=utf-8


    " {{{ Diacritic characters: <Leader>char (keyboard switching doesn't work in cygwin)
    " Use the same keys as on a keyboard
    inoremap <Leader>; ö
    inoremap <Leader>: ö
    inoremap <Leader>' ä
    inoremap <Leader>" Ä
    inoremap <Leader>[ ü
    inoremap <Leader>{ Ü
    inoremap <Leader>- ß

    " Comment/Uncoment following as you need:
    "inoremap <Leader>`A Á
    "inoremap <Leader>`Ae Ä
    "inoremap <Leader>`C Č
    "inoremap <Leader>`D Ď
    "inoremap <Leader>`E É
    "inoremap <Leader>`I Í
    "inoremap <Leader>`Ll Ĺ
    "inoremap <Leader>`L Ľ
    "inoremap <Leader>`N Ň
    "inoremap <Leader>`O Ó
    "inoremap <Leader>`Ou Ô
    "inoremap <Leader>`R Ŕ
    "inoremap <Leader>`S Š
    "inoremap <Leader>`T Ť
    "inoremap <Leader>`U Ú
    "inoremap <Leader>`Y Ý
    "inoremap <Leader>`Z Ž
    "inoremap <Leader>`a á
    ""inoremap <Leader>` ä " defined already
    "inoremap <Leader>`c č
    "inoremap <Leader>`z ž
    "inoremap <Leader>`e é
    "inoremap <Leader>`i í
    "inoremap <Leader>`l ľ
    "inoremap <Leader>`n ň
    "inoremap <Leader>`o ó
    "inoremap <Leader>`ou ô
    "inoremap <Leader>`r ŕ
    "inoremap <Leader>`s š
    "inoremap <Leader>`t ť
    "inoremap <Leader>`u ú
    "inoremap <Leader>`y ý
    "inoremap <Leader>`z ž
    " }}}


"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.


Using the digraph will get around the insert-mode alt-key maps.


or `, delete, e if digraphs are activated (IIRC)


I code on a Macbook, and I actually use say... Option+i, e in order to get ê. That works inside Vim, too, I just found out.

Obviously not very helpful to people not on a Mac, but I don't have to go through Alt mappings at all.


The point of my comment is that with an imap on <a-j>, you can't input the character ê because it will trigger the mapping. This includes option+i, e on OS X.


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


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


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.


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.


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...


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>>


As with most vim commands you can type a number first to repeat your command



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


You can change that to

au VimResized * wincmd =




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: