Skip to content

Instantly share code, notes, and snippets.

@csswizardry
Last active August 2, 2022 20:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csswizardry/685119f90eeccb6c089d6107f7d09758 to your computer and use it in GitHub Desktop.
Save csswizardry/685119f90eeccb6c089d6107f7d09758 to your computer and use it in GitHub Desktop.
Vim Calculator

Given the following dataset:

Run 1 Run 2 Run 3 Run 4 Run 5
1.991 1.871 1.885 2.007 2.114
2.009 2.211 2.199 2.281 2.094
2.018 2.091 2.115 2.241 2.020
2.782 2.105 2.506 2.660 2.273
1.501 1.885 1.921 1.235 1.873

…and the following series of Vim commands:

gg qq j0 :s/⎵/⎵+⎵/g y$ A ⎵=⎵ <C-r>= (<C-r>")/5⏎ <Esc> q @⁠q @@ @@ @@ :2,6sort /[=]/⏎
  • gg: Jump to the top of the file
  • qq: Begin recording a macro into register q
  • j0: Drop down a line and back to its first character
  • :s/⎵/⎵+⎵/g: Substitute every space on the current line for a plus sign
  • y$: Yank (copy) from the beginning of the line to the end
  • A: Append text onto the end of the current line
  • ⎵=⎵: Type an = sign with a space either side
  • <C-r>=: Enter the expression resgister (calculator)
  • (<C-r>")/5⏎: Put the contents of the pasteboard inside of (<pasteboard>)/5 and run it
  • <Esc>: Leave insert mode
  • q: Stop recording macro
  • @q: Run macro q on the next line
  • @@: Run the last macro (i.e. q) again
  • @@: Run the last macro (i.e. q) again
  • @@: Run the last macro (i.e. q) again
  • :2,6sort /[=]/⏎: Sort lines 2–6 based on the value that appears after the = sign

That will leave you looking at this:

Run 1 Run 2 Run 3 Run 4 Run 5
1.501 + 1.885 + 1.921 + 1.235 + 1.873 = 1.683
1.991 + 1.871 + 1.885 + 2.007 + 2.114 = 1.9736
2.018 + 2.091 + 2.115 + 2.241 + 2.020 = 2.097
2.009 + 2.211 + 2.199 + 2.281 + 2.094 = 2.1588
2.782 + 2.105 + 2.506 + 2.660 + 2.273 = 2.4652
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment