I realized the other day that, as much as I’ve read through the vim documentation and sought out solutions to specific problems, I’m still constantly learning things about it almost accidentally as I stumble across how person x or y approached some specific task. It occurred to me that a lot of people post their
.vimrc files online1, and flipping through a bunch of these could prove insightful. So I googled ‘my vimrc,’ I searched github, I poked around… a lot. It’s worth noting that some of my observations here are biased in that my vim use is primarily prose (generally in markdown), followed by HTML/CSS/JS, followed by recreational code. I don’t deal in major coding projects consisting of tens of thousands of SLOC for production systems. What works for me is almost certainly atypical.
Something that I’ve been meaning to write about is my aversion to things that make any given setup of mine less portable – and that includes things like keyboard mappings that simply give me muscle memory that only works on my configuration. I see a lot of this sort of stuff in the
.vimrc files of others, and for the most part it’s just stuff where I’d rather take the efficiency hit but know how to do it in a portable way. For example, a lot of people map something to the ‘oh shit I forgot to
sudo vim’ sequence,
!sudo tee % > /dev/null. I fully understand how that sequence works, but to me it’s such an oddball use of
tee that I feel like if I got too accustomed to not typing it, I might accidentally do something really weird on a system that isn’t my own2.
Similarly, I see a lot of mappings like Ctrlh to jump left a window instead of Ctrlwh. This sort of thing saves you one keystroke, while completely demolishing one of the key points of using vim – that of context and modality. Ctrlw means ‘get ready to do some stuff to some windows’, be it moving, resizing, closing, whatever. It’s not a ‘mode’, per se, but it fits vim’s modal model.
I know there’s a huge community of vim plugin development, but I was still a little surprised to see so much reliance on plugins (and plugin managers3). There are a few plugins that I consider rather essential, like
surround.vim, but again I largely try to do things the native way when possible, so I try not to extend vim too heavily.
I don’t strictly adhere to the aforementioned policy, particularly for things that I know I won’t forget how to do in a portable way (like
autocd in the shell), or things that are purely conveniences (like mapping CtrlL such that it works in insert mode). One clever idea that I saw along these lines was remapping Enter to clear the search highlight before doing its thing. Which, I don’t think I’ll adopt, but it is a handy idea – those highlights can get a little distracting.
I see a lot of mappings of j to gj which again just feels so un-vimlike. Up/down movements corresponding to screen lines instead of content lines is something that actually bugs me in other editors. Worse, this mapping makes counts tricky to use in a . or macro situation, which is particularly weird when a lot of the same people use
:set relativenumber. Another common mapping is to do gv after > or <, so that you can repeatedly hit it and have the same visual block selected. But… the vim way would be to use a count instead of mashing the button four or five times.
<Leader> to , a lot, which to me feels even more awkward than \. I’ve seen weird insert-mode mappings to jump back to normal mode, like jj, which is fair – Esc is kind of a ways away. But the real trick to that is twofold: first, remap your useless Caps Lock to Ctrl system-wide, and then train yourself to use Ctrl[ instead of Esc.
Doug Black’s post about his
.vimrc has two good pieces of advice: don’t add things to your
.vimrc that you don’t understand, and don’t use the abbreviated forms of settings/commands4. I see a lot of files that don’t seem to conform to this rather basic advice. Things like hardcoding
t_Co without performing any checks – at best it’s merely not portable, but it reads like ‘a thing that I did that solved a problem’ vs. a setting that the user actually understands.
I did have some positive takeaways from this little journey. While I don’t use macros much (I opt for
:normal more often), I learned about
:set lazyredraw which speeds up macro execution by waiting until the end to redraw the screen. I had somehow forgotten that vim supports encryption, and that it defaults to the laughable pkzip scheme, so
:set cryptmethod=blowfish2 is making its way into my
.vimrc. Someone added syntax for two spaces after a period, which is a smart idea – I would link that right to
Error. It would be better (perhaps) to add that as a wrong/bad spell, but I think a highlight would work.
Curious to me was the number of people who put things in their
.vimrc files that are specific to filetypes (etc.). This is stuff that I generally relegate to
.vim/ftdetect/. For instance, I have some folding rules in place for markdown files used in my blog. I add the filetype
hugo with a
ftdetect script, and then lay out some syntax rules in
.vim/ftplugin/hugo_folds.vim. I don’t know if my approach is better or worse – it definitely makes for a big pile of files. Is this more or less organized than just maintaining it in a tidy
.vimrc? Something to think about.
This adventure down the dotfile rabbit hole taught me more than anything, I suppose, about how other vim users twist vim’s arm to make it less vimlike. Interestingly, I ran into a couple of files that were updated over the years, and the users seemingly adapted to more vimmy ways. I suspect a lot of these things come of a sort of feedback loop – a vim beginner sees a
.vimrc file online with
map <C-K> <C-W>k and thinks ‘why not shave off a keystroke?!’ They end up posting their
.vimrc file a year down the road when they feel they’ve perfected it, and another novice stumbles across it, thinking ‘why not shave off…’ Regardless, it’s pretty neat just how many
.vimrc files are floating around there, given how customizable and extensible vim is. Even approaches that are totally opposite one’s own likely have something previously unknown or unthought of.
- In no order, a handful of the
.vimrcfiles that I looked at: amix, JeffreyWay, Chris Yeh, sts10 and sts10, Doug Black, vinchi777, Jake Gordon, Jay Pipes, MisoF, Taylor Hornby, Janis Miezitis, abbood, Isaac Sloan, Steve Francia ↩︎
- I guess this is a pretty out-there hypothetical, a system in which I’m on the
sudoerslist but cannot or have not swapped in my own
.vimrc. Still, I’d rather just know how to do this vs. mapping it to a single keystroke. It really shouldn’t come up often enough that such convenience is necessary anyway. ↩︎
- I really don’t understand vim plugin managers. You have to use way too many plugins for this to even approach necessity, and even then… When people distribute vim plugins, they lay everything out in the appropriate structure to just drop in your
- The weirdest is when people do something like
:set scs "smartcase. Just… use the descriptive name instead of using the short name and a descriptive comment? ↩︎