Like most people that spend a lot of time working in source code, I'm a huge fan of Sublime Text. Sure, for years @antisnatchor and @wadealcorn used to try and convince me to shift to RubyMine, or other IDEs, particularly when working on things like www.beefproject.com where debugging issues would get significantly easier. But as most of my work was done on an older model 11" Macbook Air, as soon as I opened RubyMine, the performance hit was too much, and I found myself back in familiar Sublime with a few terminal windows open for debugging, tailing logs and so on.

During my brief period of not working last year, I thought to myself: everything I do now in Sublime, while feeling very natural to how I work, is probably something I could do in Vim. Having now been motivated by @0x1c's transition to a full-fledged Vim-thought-leader, I thought I should write down some of the things I used to do in Sublime, which I now do in Vim, and the benefits I'm seeing.

I should disclaim though, I'm no Vim expert. I learn new things every week, and do have a few bad habits that I'm struggling to change. But this is one of the things I like about Vim, and there's a few people I know (in addition to @0x1c) that always do things that I see and go: whoa.

The other benefit of this period of transition to Vim is that in my new setup, I use a Macbook Pro for all my desktop work, but I have a dedicated Linux workstation with way more oomph than I need, which I do 90% of my development work on. While there are tricks that allow you to use Sublime directly on remote files via SSH (see Rsub), and NoMachine is great for responsive remote desktop, I've found it's much easier to simply use Vim over SSH. And this is where I introduce my second life-saver, tmux.

Similar to my transition to Vim from Sublime, my transition from using screen to tmux is something that I've only done recently (in the past couple of years). When I first started playing around with Linux, if you ever needed to leave a task or app running and wanted to be able to disconnect from the server, screen was the defacto way to achieve this. Tmux to me feels like screen on steroids. Particularly when it comes to window and pane management in a terminal environment. Using tmux is relatively straightforward, after you've opened up your terminal (I use iTerm2 on OSX), or SSHed into your server, you simply start tmux (or reattach to a previous session with tmux attach). By default tmux uses the Ctrl+b command for it's hotkey, a lot of people remap it to Ctrl+a to work the same way as screen, but I've just adjusted to Ctrl+b. So for instance, to detach from tmux you hit Ctrl+b then d. To see a list of available keys you can hit Ctrl+b then ?.

Tmux commands I frequently use:

  • Ctrl+b c - create a new window
  • Ctrl+b 1-9 - changes window
  • Ctrl+b n - changes to the next window (p changes to the previous)
  • Ctrl+b , - renames the window
  • Ctrl+b % - splits the current window vertically
  • Ctrl+b " - splits the current window horizontally
  • Ctrl+b z - zoom the current pane fullscreen - I use this a LOT to focus on a particular Vim session

View post on imgur.com

Tmux also has a command interface available in Ctrl+b then : Once in this mode you can execute longer tmux commands (of course you can re-map these too). One of the commands I frequently use within here is the movew -r command, if I happen to have exited out of a window and the numbers are out of order this renumbers the available windows in sequential order.

In general what I'll end up with is two iTerm windows, one with a tmux for my local machine, the other with a tmux for my workstation. To differentiate between them I've adjusted the status background colour to red on my workstation. This is done within tmux's config file, located at ~/.tmux.conf. On each of these I'll have multiple windows, usually named in the project or workspace I'm currently working on. More often than not, the window will be filled with Vim, but when I need to tail files, or run other commands, I'll use tmux split commands to split the window in half, or quarters etc. Moving around tmux panes is as simple as Ctrl+b h, j, k or l, similar to moving in Vim, mapped within my tmux.conf file. When I need to focus on just the development work I'll zoom that pane (Ctrl+b z). Tmux is great because it'll indicate in the window's name whether a pane is zoomed or not. This window splitting, focusing & zooming is primarily how I'll work within a particular project.

Enough about tmux, let's talk about Vim.

Firstly, the power of Vim, even without plugins, is astounding. Just look at some of the ridiculous things you can do here. Now, I'm not going to cover too much of the basics, but let me just say that over time, while I used to really enjoy the ‘tab' interface within Vim, I realised that it was just a poor-person's way of using Vim's buffers. To help navigate buffers, I'll usually use the CtrlP plugin (see below), but I've also installed the Buffergator plugin. Buffergator provides a <Leader> b command shortcut to open a simple list of open buffers. I'll often then use the Ctrl+n or Ctrl+p commands within Buffergator to quickly change the buffer in the active pane. And if you haven't played around the Leader commands, you can read more here (Thanks to @hipikat for talking to me about this years back).

To help install plugins, I use Pathogen, which allows me to simply git clone Vim plugins into ~/.vim/bundle folder.

The first functionality I liked in Sublime that I wanted to cater for in Vim was the file explorer. Now, I know that Vim has an in-built explorer, but I've settled on the Nerdtree plugin. Nerdtree is great, is opened/closed with Ctrl+n (in my config), and behaves much like a file explorer should. Within Nerdtree, I'll often use s to open the highlighted document in a vertical split, or i for a horizontal.

With regards to splits, I've mapped Ctrl+h or j or k or l keys to navigate Vim windows. Other window commands I'll use include window resizing, for instance to evenly size windows with Ctrl+w =, or to resize windows with commands such as Ctrl+w + or - to change the height, or Ctrl+w > or < to change the width. You can prepend a number in front of the +, -, > or < signs to change the size but that amount.

For those who have used Sublime for a while have likely come across Ctrl+p (or Command+p), also known as the Goto Anything capability. Great feature, and makes it very quick to open files in the current workspace or directory structure. Luckily, the ctrlp plugin for Vim recreates this functionality, plus a few extra things I use a lot. First and foremost is the fuzzy file searching, by simply hitting Ctrl+p and start typing the filename. I've also mapped ; to open ctrlp in buffer mode. This allows a quick way to find open buffers. From within the ctrlp interface, you move up and down the available options with Ctrl+j or k, and then open with Enter, or Ctrl+v for vertical split (x for horizontal). Ctrlp also allows you to select multiple documents, and then open them all.

Another great feature in Sublime is the search all file feature. This is where the ag plugin comes to the rescure. Ag is actually a frontend to The Silver Searcher, a code searching tool similar to Ack. Ag is a quick way to find references or other text quickly within the current Vim folder (you can see where this is by the :pwd command). To execute, you run :Ag! <searchterm>. I use the ! to not automatically open the first selected document, my preference is to use the Quickfix to navigate up and down with j and k, and then v or h to open the selected document.

The final feature which I occasionally use is Tagbar, this opens and dynamically builds a tags sidebar, allowing you to quickly see functions, methods and so on. The use of tags, and ag come into play is where I need to quickly find reference code, or where functions are defined. If you're fortunate and have all the libraries and dependencies in the same workspace, you can generate your ctag references from the shell ($ ctags -R .) and then simply Ctrl+] on functions to find their definitions. Often if this doesn't work, what I'll often do is yank the selected word or function into a register, and quickly drop that into ag for searching. For instance, I'll shift the cursor to the start of the word (b can quickly move the cursor for you), then yank the word into the unnamed register with yw. Then entering :Ag! Ctrl+r " <enter> will execute the Ag command, against the last item in the register. You can see what's in all your registers with the :reg command. Registers are super handy if you want to copy and paste different content around.

I'm likely to look back on this post in a year or so and have drastically changed my approach, but that's one of the great things about Vim.

Not that I keep it up to date, but some of my config is cloned to https://github.com/xntrik/dotfools

Enjoy!