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 · 1 min · Jake Worth

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 · 1 min · Jake Worth

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 · 2 min · Jake Worth

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 · 1 min · Jake Worth

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 · 1 min · Jake Worth

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 · 1 min · Jake Worth

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 · 1 min · Jake Worth

Delete a Line From Another Line

Today I was cleaning up a test with an extra empty line at the top of the file, away from my Vim cursor. I wanted to delete it… without moving the cursor. It seems like Vim almost lets you do this, with :[range]d. But it leaves the cursor on the deleted line, which isn’t very magical. This is the hack we found: :[range]d Then: '' '' returns the cursor to the last jump point. ...

November 9, 2015 · 1 min · Jake Worth

Explore Buffers with BufExplorer

I’m a huge fan of Vim buffers. I try to have my buffer list roughly mirror the files I am currently holding in my brain. The BufExplorer Vim plugin helps, and is included in the Hashrocket Dotmatrix. Today I learned to use the command \bs, which opens a colorful buffer list that you can navigate with Vim directions. https://github.com/jlanzarotta/bufexplorer

October 12, 2015 · 1 min · Jake Worth

Edit the Current File Always

The Vim command :e edits a file. Add a bang, :e!, to edit the current file ‘always’, discarding any changes to the current buffer. This is useful if you rename a file with a new file extension (i.e. .txt to .rb). :e! reloads the file in Vim, picking up any customizations you have in your configuration files such as syntax highlighting.

May 29, 2015 · 1 min · Jake Worth