n² likes = n advanced vim tricks
1/ `:%g/regex/cmd` runs command only over lines that match `regex`. IE

`:%g/^\d/d` → delete lines that start with a digit
`:%g/^\d/normal! A!` → add `!` to the end of every line that starts with a digit

`:%v` is the same, except NOT matching
2/ While `"ay` copies in to the `a` register, `"Ay` APPENDS to it.

When is this useful? With `:g`, of course!

`%g/^\d/y A` copies *every* line starting with a digit into the `a` register, so you can paste them all at once!
3/ I like relative numbers for normal mode, but absolute ones for insert/command mode (easier to do ranges).

au InsertEnter * set norelativenumber
au InsertLeave * set relativenumber

au CmdLineEnter * set norelativenumber | redraw
au CmdlineLeave * set relativenumber
4/ Vim keeps an undo *tree*. `g-` goes to the previous *chronological* version of your file, and `:earlier 5h` goes to the version five hours ago.

You can also navigate the tree visually with undotree: github.com/mbbill/undotree
5/ Vim also has a "jumplist" history, tracking jumps like `gg`, `#`, `:e file`, etc. You can toggle the last jump with `''` and go back and forth in the history with <ctrl-O> and <ctrl-I>.
5 (cont)/ you can read more about the jumplist with `:h jumplist`.
6/ Having trouble finding the right thing in help? Use `:helpgrep`! It'll dump the search results into the quickfix list, which you can open with `:cope`, then just scroll to the appropriate match and press enter.
7/ `:bufdo cmd` runs a command over every open buffer, `cdo` over everything in the quickfix, `argo` over the arglist. A common pattern of mine is

:THINGdo %s/find/replace/cge

To find/replace across many files

(see `:h s_flags` for the cge)
8/ `inoremap` lets you put custom bindings in insert mode. A couple I have:

inoremap ;r <c-R>+
inoremap ;d <c-R>=strftime('%D')<cr>

(I like prefixing them with `;` because there's never a reason to, in writing/coding, follow `;` with anything besides a space)
9/ You can load custom syntax and filetype code in the after/ directory (:h after-directory). Do some custom stuff! A few I have for markdown:


syntax keyword todo TK


nmap <buffer> <leader>` ysiw`
nmap <buffer> <leader>~ ysiW`
9 (cont)/ I use `nmap` and not `noremap` because those are triggering the vim-surround plugin github.com/tpope/vim-surr…
10/ Syntax highlighting all messed up? Correct it with `:syntax sync fromstart`! I have this mapped to <F12>.
11/ `ma` sets a file-local mark, `mA` sets a global mark. `'a` will jump to the `a` mark in the file, `'A` will jump to the file marked `A`. You can use this as a simple bookmarking system!

See marks with `:marks`

(I find file-local marks are best used for macros)
12/ <c-V> starts block selection. You can use this to cut blocks and paste them in the middle of other blocks. See screenshots

Also, while in block mode, `A` appends to the end of every line in the block
13/ Macros are also stored in the copy-paste registers, meaning vim is homoiconic I guess. This also means that you can paste a macro, edit it, and copy it back into the register. Try adding `ZZ` to the end of a macro!
14/ You can open your command history with `q:` and your search history with `q/`. Then you can search, run `g`, copy to your hearts content
15/ With `json_decode` and `join(keys(foo), "\n")`, you can make a tab-complete for your custom commands with `command! -complete=custom,s:a_function`. I use this a LOT: functions with custom datafiles
16/ `:put expand('%')` pastes the filename into the buffer. expand('%:p') is instead the absolute path, '%:p:h' the directory path, '%:r' is the filename without extension, and '%:e' is just the extension.
17/ If you have lots of wrapped lines, try

noremap <expr> k v:count == 0 ? 'gk' : 'k'

Pressing k in the middle of a multiline paragraph will make it go up one *display* line UNLESS you're using a number (like `4k`), in which case it behaves normally.
18/ `gu` lowercases a motion, `gU` uppercases, `g~` toggles case, and `g?` rot13s. In all cases you can repeat the second letter to make it apply linewise, ie `guu` lowercases the entire line. I mostly use `guiW` and `gUiW`, but I'm sure there's SOME use for `g?`
19/ If you're on neovim (which is great) you get a few really nice benefits:

1. You can write functions and config in lua instead of vimL
2. `inccommand` shows you `:s` find-replace updates dynamically, including with capture groups and stuff
19 (cont)/ another cool thing in Neovim: you can use it as part of pipelines with the -Es flag. Ie

seq 10 | nvim -Es +"%norm! A1" +"%p"
INTERMEZZO: If you're enjoying this, there's a couple bigger pieces I've written on vim:

VIM IS TURING COMPLETE: no, not vimscript, vim *keystrokes* are Turing complete! includes a brief overview of Turing completeness and a demonstration.

AT LEAST ONE VIM TRICK YOU MIGHT NOT KNOW: a vim article I did a couple years back. I'm trying not to reuse any tricks between that and this tweetstorm but it's haaaaaaaard

Okay back to the tricks

20/ You can dump the most recent error to the file (for copy-paste searching or whatevs) with `:put =v:errmsg`.

You can dump all recent messages with `:put =execute('messages')`.

(You need SINGLE quotes, not DOUBLE quotes)
21/ There's a ton of good stuff in `:h pattern`. You can search for the first x AFTER a y with `s/y/;/x`. `\zs` and `\ze` set the start and end of match, so `s/(\zscat\ze)/dog/g` will change (cat) to (dog) but leave {cat} alone

• • •

Missing some Tweet in this thread? You can try to force a refresh

Keep Current with Hillel

Hillel Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!


Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @hillelogram

13 Oct
It's marketing for their satellite launch businesses. 20 years ago it cost like 15k/kg to launch a satellite, now it's closer to 2k/kg. That's why satellites-in-orbit have tripled in the past decade aerospace.csis.org/data/space-lau…
William Shatner weighs what, 70kg? Blue Origin inked a deal to launch tens of thousands of kg of telesats. One of these things is incredibly lucrative, the other is a PR stunt telesat.com/press/press-re…
I will keep screaming this from the rooftops until people realize that ruthless capitalist industry do, in fact, want to make money
Read 4 tweets
12 Oct
Last night @arsduo and I got drunk and used the Art Machine (is.gd/artmachine) to generate waifus

I give you


with a bouquet of flowers for some reason
Read 9 tweets
12 Oct
If any companies want to pay me just 500k/year to be a full-time thought leader I'm available and happy to sell out my principles
Consider this:

1. I am very popular
2. As an independent consultant, I make less annually than your junior engineers
3. I am easily corrupted
Also I know Python
Read 4 tweets
12 Oct
CITE YOUR FUCKING SOURCES. This is from , with the methodology at docs.google.com/document/d/1WO…. This was somebody's passion project, the very least you can do is accredit them.
Oh, also! You see how many people in the comments are arguing about why Europe has so many battles? That's directly answerable by the methodology. The dude just looked for stuff with the Wikidata "battle" tag, and Wikidata has incredibly spotty coverage, even for just the en wiki
To whit the creator found 12,000 articles tagged battle, but the English wiki alone has 19,000 warboxes, so there's likely a lot of battles that aren't tagged in wikidata en.wikipedia.org/wiki/Template:…

(And, again, the English wikipedia is the most well-covered)
Read 4 tweets
11 Oct
After a three month hiatus, I've finally written a new blog post! It's 4000 words on... "how to solve the sudoku puzzle with programming".

Yes, really.

yesssssssss Image
I also wrote a short thing on my motivation with this essay, given how different it is from my usual writing style buttondown.email/hillelwayne/ar…
Read 4 tweets
5 Oct
Test Driven Development (TDD) is defined as

1. Write a failing test
2. Write the minimum code that passes the test
3. Refactor, keeping all tests passing.

What are the alternative methods?

Well, that's a complicated question. Alternative to *what*?

There are two main ways people see TDD:

1. As a *design* technique
2. As a *verification* technique

The trend these days is to say it's all (1) and not (2), but it's always been hybrid. I'm going to focus on alternatives to (2), so as to keep this tweetstorm under 500 tweets.
Now there's two parts to TDD as a verification technique:

A. The red-green-refactor (RGR) process
B. The primary means of verification in RGR, which is unit tests.

Alternatives to (A) are process alternatives, alternatives to (B) are tech alternatives. Let's do (B) first.
Read 30 tweets

Did Thread Reader help you today?

Support us! We are indie developers!

This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!