Hacker News new | ask | show | jobs
by randomdrake 4810 days ago
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.
8 comments

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.