view on CentOS

When editing a file, it’s often handy to have a related file open for reference. For example, if I’m using a library function I might have the function definition open in a separate window. Or if I’m editing a configuration file on one server, I might want to see what the same configuration file looks like on a different server.

Vim has a read-only mode (vim -R) that is perfect for this purpose. From the vim man page:

Read-only mode. The ‘readonly’ option will be set. You can still edit the buffer, but will be prevented from accidently overwriting a file. If you do want to overwrite a file, add an exclamation mark to the Ex command, as in :w!. The -R option also implies the -n option.

The -n option stands for “no swap file” — since you will not be editing the file, there is no reason to create a swap file. This is a helpful because it allows you to open a file in read-only mode multiple times and vim will not complain.

Instead of typing vim -R, I typically invoke vim using the view command, which does the same thing:

Vim behaves differently, depending on the name of the command (the executable may still be the same file).

vim
The “normal” way, everything is default.

view
Start in read-only mode. You will be protected from writing the files. Can also be done with the -R argument.

On Ubuntu, vi, vim, and view are all symlinks managed by the alternatives system. When the vim package is installed, all three commands point at the real binary, /usr/bin/vim.basic. Since all three commands invoke the same binary, the behavior is consistent across commands (except view invokes vim in read-only mode, as expected).

On CentOS, the situation is more complicated. The vim-minimal package provides /bin/vi and /bin/view, and the latter is just a symlink to the former. The vim-enhanced package provides /usr/bin/vim, then sets up a bash alias vi=vim in /etc/profile.d/vim.sh. Unfortunately, there is no special treatment for the view command, so when you invoke vi or vim you get the enhanced vim, but when you invoke view you get the minimal vim. This can be annoying — for example, if you open a file using vi or vim you will get syntax highlighting, but if you open a file using view, you won’t.

Fortunately, the fix is fairly simple. Just add the following lines to your .bashrc:

if [ -x /usr/bin/vim ]; then
    alias view='vim -R'
fi

This sets up an alias for the view command, so vi, vim, and view all invoke the same binary and the behavior is consistent across all three.

Womprat Colorscheme for Vim

My main “IDE” is vim in a terminal. For years, I used the default 16-color colorscheme, but recently I was inspired by a few blog posts about vim colorschemes and solarized in particular. I tried solarized for a bit, but I didn’t like that I had to change my entire terminal color scheme, and I didn’t like the blue background (just weird) and grey text (too low-contrast).

After trying out a few other colorschemes, I decided to create my own. I started out with David Liang’s wombat256mod, which is in turn based on wombat by Lars Nielsen. With a few hours of tweaking, I had something that (IMHO) looked pretty good on CoffeeScript, PHP, Scala, and diffs.

Continue reading

githublink.vim Plugin

I use Vim as my primary editor. I also use GitHub for a lot of my projects. On more than one occasion, I have found myself wanting to share the line under my Vim cursor with a collaborator in the form of a github.com link. The manual process went something like this:

  • Go to github.com in a browser
  • Navigate to the correct repository and branch
  • Drill down to the file I was working on
  • Find the line in the file and click on the line number
  • Copy the URL and paste it in the email or instant message

Wouldn’t it be nice if you could just hit a hotkey and have Vim figure out the URL for you? I thought so too, so I started hacking on Vim plugin to do exactly that.

To use the plugin, install it, then start editing a file that’s part of a GitHub-hosted repository. Press \ g (backslash, followed by g) to display the URL of the current line. You still have to copy and paste the URL, but everything else is done for you. Update: the plugin now copies the URL to the clipboard for you if pbcopy is available.

Some of the constructs for the plugin are borrowed from rubytest.vim, so thanks to janx for the rubytest plugin. This is my first foray into Vim script, so if there are better ways of doing things, please share!

Vim Anti-Patterns

I saw this blog post on Twitter today. Some of these I already avoid, like moving a single line at a time and pressing d then i instead of just c. Others I still do, like hitting Esc. I use Ctrl+C sometimes, but I don’t think I’ve fully broken the Esc habit. Looks like I should be using Ctrl+[ anyway. There are lots of gems that I didn’t even know about, like using two backticks (``) to go back to where you were, and using ? to search backwards. Definitely worth a read!

Vim Text Objects

They say it takes 10 years to master something, whether it be programming, photography, etc. I think Vim is an exception to this rule. I’ve been using Vim longer than I can remember, but every once in a while I learn something new about it that makes me feel like I’ve just begun to scratch the surface.

Today it was Vim text objects. I use motions like dw (delete word) and ct) (change to ) character) all the time, but motions require that my cursor be in the right place when I issue the command. If I do a dw in the middle of a word, it will only delete from my cursor to the end of the word. With text objects, I can do a daw with my cursor anywhere in the word, and it will delete the whole word. Or sentence. Or paragraph. No more wasting keystrokes navigating to the beginning of the paragraph!

Even better, there are programming language-oriented text objects, and they search the entire line! So you can do ci' anywhere on the line, and Vim will find the first single-quoted string, delete the contents, and put you in insert mode. There are even text objects for HTML markup (at, a>)!

Go read the guide now. In the meantime, I have some new muscle memory to practice.