Removing Distractions From Vim

Updated: 30 Nov 2013

Sublime Text is a text editor that has rapidly grown in popularity. While I still prefer Vim, I love some of the features that Sublime Text has. Among them is something called Distraction Free Mode.

As one might expect, there are plugins that aim to bring similar functionality to Vim. VimRoom and Distraction Free Writing With Vim are the two that I am familiar with. I had issues with both of them, but they may work out for you.

Out of frustration, I ended up writing my own plugin called LiteDFM. I wasn't really trying to do anything complicated, but I ended up with a poor man's Distraction Free Mode (DFM).

My usual Vim session

My usual vim apperance

My Vim with LiteDFM on

Vim's apperance with LiteDFM on

Using LiteDFM

You can find the source up on GitHub.

If you're using Vundle, you can install it by adding this to your .vimrc and running :BundleInstall.

Bundle 'bilalq/lite-dfm'

For some added convenience, you may want to create a mapping to toggle DFM. The one I use is:

nnoremap <Leader>z :LiteDFMToggle<CR>i<Esc>`^

The i<Esc> at the end is there to immediately hide the message that shows the last command. Changing the mode and back is a bit of a dirty hack, but it gets the job done. The `^ keeps the cursor steady after the mode change, since leaving INSERT mode usually moves the cursor back a column.

If you're a tmux user, you may want it to toggle the status bar there as well:

nnoremap <Leader>z :LiteDFMToggle<CR>:silent !tmux set status > /dev/null 2>&1<CR>:redraw!<CR>

How it Works

In Sublime, DFM hides all UI chrome, centers text, and soft-wraps line lengths. What I have here falls a bit short of that.

Centering text turned out to be a bit challenging. However, there were some things that naturally offset text from the left: the line number and fold columns. By default, numberwidth is set to 4, and the largest it goes is 10. foldcolumn defaults to 0, and maxes at 12. By making use of these two, I was able to offset by 22 columns. When you activate LiteDFM, it applies this offset to each window.

Of course, I didn't want anything distracting in these columns, so I set the foreground and background colors for these such that they appeared invisible. This isn't centering, but it does get your code away from the left edge of the screen.

I even made it so that you can manually override the default 22 column offset. All you need to do is set a variable in your .vimrc with a value from 1 to 22:

let g:lite_dfm_left_offset = 16

Aside from line numbers, the statusbar and ruler are also UI elements that toggling LiteDFM hides. If your colorscheme doesn't hide those NonText ~ characters that show up, this hides those too. Upon toggling off LiteDFM, settings revert to what they were before.

This works for multiple windows as well. With a vsplit in fullscreen, it can even pass off as being centered.

Caveat

If you have a value of none for your Normal bg color, the plugin won't be able to figure out what color to use to hide your UI elements. To fix this, you can explicitly set the color to use.

comments powered by Disqus

This work is licensed under a Creative Commons Attribution 3.0 Unported License.