Override Vim's Filetype

Vim’s filetype auto-detection is great for effortless syntax highlighting, but what if a certain kind of file (i.e. Ruby) contains lots of another kind of code (i.e. SQL)? The Ruby code will be highlighted and readable, the SQL a large monochrome blob. Hard to read and reason about. We can do better! Override the automatic assignment with: :set ft=sql This command with no assignment returns the current setting: :set ft filetype=lua We can easily revert to the auto-detected extension as needed. ...

April 22, 2021

My Annotated Vim Configuration File

I love Vim. Folks who’ve programmed with me, or attended a Vim Meetup when I was an organizer, can attest. When I was learning to code, getting fast at Vim changed everything for me. After almost a decade using this editor, here are my personal configurations. ...

November 26, 2020

Vim Nonrecursive Mappings

My first PR to a new Vim plugin was merged this week, check it out, adding non-recursive Vim mappings to vim-termbux. ...

December 17, 2016

Vim Regex Word Boundaries

Today while writing a Vim regex to change every instance of it (ignoring larger matches like itemized), we stumbled upon Vim regex word boundary matching. Given this file: foo foobar The following Vim regex will match on both the first and second line: :%s/foo/baz/g But with word boundaries, we’ll only match (and change) the first line, because only the first foo is a standalone word: :%s/\<foo\>/baz/g

September 20, 2016

Keyword, Match, and Region in VimScript

After a recent talk I gave that included VimScript, an attendee asked a question about the differences between keyword, match, and region following syn in a Vim syntax highlighting file. Here’s my answer. ...

April 20, 2016

Vim Buffer Problem

A few weeks back I tackled the following programming challenge from my colleague Josh Branchaud: Vim Buffer I open up a new Vim buffer and type all the numbers 1 to 10,000, separated by spaces. Then, my cat walks on the keyboard and somehow activates a substitution command that replaces all the ‘0’ digits (zeros) with spaces. If I now sum up all the numbers in the buffer, as delineated by spaces, what is the total? ...

March 6, 2016

Count Links in a Markdown File

Today I learned a great way to count the links in a README or link collection. Vim Regex! :%s/- \[//n In Vimspeak: On every line, find all of the times where - [ (the beginning of a Markdown bulleted hyperlink) occurs, count them, and report the number of matches. See :help substitute in Vim for more information. h/t Josh Branchaud

February 14, 2016

Get help with Pathogen

Today I found a great command in Pathogen.vim, :Helptags. This is a riff off :helptags {dir}, a Vim feature that builds helptags for directory {dir}. :Helptags does the same for all the directories in your runtimepath, which defaults on Unix and Mac OS X to: "$HOME/.vim, $VIM/vimfiles, $VIMRUNTIME, $VIM/vimfiles/after, $HOME/.vim/after" The use case here is when you’ve just loaded a new plugin into your Vim bundle, you open up Vim, and :h {my_new_plugin} isn’t ‘helping’ you out.

January 13, 2016

Change Inner Tag Block

Vim has excellent commands for changing the contents of parentheses (ci(), brackets (ci[), and squiggly braces (ci{) (as well as single quotes, double quotes, backticks, etc.). But what about tags? Place your cursor anywhere inside the tags, type cit in Visual mode, and this: <div>Begone</div> Becomes: <div></div> d works too, but I prefer c because it puts you in Insert mode at the end of the opening tag, ready to type. ...

January 7, 2016

Increment and Decrement Numbers

Vim has commands built-in to increment and decrement numbers: CTRL-A and CTRL-X, respectively. Combining this with a macro was a technique we tried today to build a large database migration. We ended up finding a more efficient solution, but it’s still a very magical key combination. Check out :help CTRL-A for more info. h/t Josh Branchaud

January 6, 2016

Don’t miss my next essay

Hear from me immediately when I post: no ads, unsubscribe anytime.