Vim Workflow

Introduction

If you saw my last post about Vim you'll see that I've started using Vim as my main editor for development work.

But up until this point I had just been whimsically opening files and editing them and I realised quite quickly this wasn't going to 'cut it' in the long run. So I wanted to show you the work flow I'm currently using in my day to day life.

Starting up

First thing first, I've found the best way to work is to keep things simple (oh wow, that 'simple' philosophy just keeps cropping up everywhere doesn't it, must be a sign! Maybe keeping things simple is the way we should do things. What a novel idea!)

So to achieve this let's just have two panes open.
One to hold our code and one to handle our terminal.

It'll look something like this…

The idea being: in my Vim pane I can then split into two panes if I really need to, like so…

But anything more than that then you'll want to open a new tmux window as otherwise you have too much going on.

To do this I run the following commands…

  1. tmux
  2. Ctrl-b (or whatever your tmux prefix command is) and then Shift-" (to create a horizontal split pane)
  3. In the bottom pane I'll cd into whatever directory my project is
  4. Then in the bottom pane we'll run Ctrl-b and type :resize-pane -D 5** to change the height of the bottom pane
  5. In the top panel I'll run vim

That's it.

**if I'm on a large screen and not my laptop then I'll change the value from 5 to 10

Top Pane

So as I said, the top pane is there literally to just run Vim. But because I use the Ctrl-P plugin and Vim has been opened from the root directory of my project, I can just run a fuzzy file search using Ctrl-P to easily open up any file in my project work space. So I don't need lots of panes open as I can just keep jumping around my files really easily with fuzzy searching.

If for whatever reason I feel I need another file open side by side then I'll run tmux's prefix command followed by Shift-5 to create a vertical split pane, where I can then run vim and again use my fuzzy file search to locate the file I want.

I don't open any more panes than that (3 max).

What if there isn't enough space to work in one of the panes?

Just make the pane you're currently working in full screen by running Ctrl-b then z. Just run that same command again to go back to the normal pane size.

What if you need more than three panes?

If I do need more than three panes then I'll just open up a new tmux window. No point over loading the screen unnecessarily. Why make your life difficult, that's what windows are there for!

Why not just use Vim panes?

I don't use Vim's own panes. I prefer tmux's.

There are two reasons for this:

  1. If I used Vim's own panes exclusively (so all 3 panes were Vim panes) then I couldn't have the terminal running in the bottom pane. Yes I can run terminal commands either in Vim (or suspend Vim) but this isn't as nice as having a proper terminal screen running freely in the bottom pane.

  2. Also, if I open Vim in my top pane and then decide to create a vertical split, the file in the right side has to be handled by Vim. I can't just split the top pane in two and do what I want in the top right pane.

Downsides to using tmux panes?

UPDATE: there is a solution!

The reason this happens is because the default bindings for the arrow keys are setup with bind-key -r, specifying that they may be repeated.

So to resolve the issue you re-bind the keys without -r:

bind-key Up    select-pane -U
bind-key Down  select-pane -D
bind-key Left  select-pane -L
bind-key Right select-pane -R

Buffer lists

In my Vim pane, as I use the CtrlP plugin to open up multiple files, I'm able to jump between files really easily using its :CtrlPBuffer command, but I make this command quicker to access by configuring Vim to let me access the :CtrlPBuffer using the shortcut <leader>y (the <leader> key is a similar concept to the <prefix> key in tmux).

So if I want to jump back to a recently opened file then I'll just run the command \y (by default the <leader> key is a backslash \) and this will show me CtrlP's buffer list.

Once we have the buffer list open we can just select the file we want to re-open. This makes it really quick for jumping back and forth between two or more files we happen to be editing a lot.

You can configure your .vimrc file to use this shortcut like so: map <leader>y :CtrlPBuffer<cr>

Mapping NERDTree

I don't use NERDTree very often but when I do it's tedious so I mapped map <leader>' :NERDTreeToggle so it's a lot less annoying to open and close it.

Global find and replace using Ack

I like to use the Ack plugin for carrying out a global 'find and replace' function on my files.

Ack is the same concept as Grep (e.g. use either a string or a Regular Expression to find a match in a list of files). But Ack has been written to be much faster than Grep**.

**Technically, in the background (configured within my .vimrc file), I alias the Ack command within Vim so it uses The Silver Searcher instead, which has the same API as Ack but is blazingly fast (even more so than Ack!).

To use the plugin you simple run :Ack 'pattern' directory (note: if you leave off the directory then the plugin will use the current directory).

Note: when you use the plugin Vim will show the results inside of a Quickfix pane. Navigating this pane requires a few extra commands…

Conclusion

That's pretty much all there is to it (at the moment). If things change then I'll be sure to update this post but simplicity is the key.

Let me know your thoughts and how you do things. I'm interested to know how other Vim users work.


Links