How to Organize JavaScript Imports

The import statements at the top of a JavaScript component file can be a confusing, duplicative, churning mess. Is there a way to organize them that makes sense and scales? In this post, I’d like to share the way I handle this detail. Who Cares? Wait; who cares? Isn’t this like discussing the color of the bike shed? Years of programming with other humans has taught me that, for loosely-defined tasks like this, any convention is better than no convention. Without it, you’ll see in many, many codebases: ...

August 30, 2021

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. My Vim Configuration My .vimrc is broken up into groups. If you open this file in Vim, you’ll see why: it’s long, and so it’s folded by groups as a default. This is one of many tips I picked up reading Learn VimScript The Hard Way by Steve Losh, and the dotfiles of programmers I admire. ...

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. vim-termbux is a plugin by my friend Dillon Hafer that emulates the behavior of vim-turbux— sending test run output to somewhere outside of Vim– in iTerm. If you write a lot of tests in iTerm and Vim, but prefer to stay outside of Tmux for whatever reason (fingers don’t like the mappings, etc.), than this plugin is for you. ...

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. These three short chapters from ‘Learn VimScript the Hard Way’ help explain: Chapter 45 Chapter 46 Chapter 47 Here’s the short version: keyword assigns a variable name directly to a word or multiple words match assigns a variable name to a Vim regex region assigns a variable name to something between a “start” pattern and an “end” pattern For more info, check out the Vim help pages for each: ...

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