vimgolf: One number per line

Start file

- One number per line -
-----------------------
2,3,5,7,
11,13,17,
19,23,29,

End file

2
3
5
7
11
13
17
19
23
29

Deleting the first two lines

  1. dj : d{motion} delete, j moves down one line
  2. 2D : D delete the character under the cursor until the end of line and [count]-1 more lines

Joining the lines

  1. JxJx : Join and delete the extra character twice
  2. 3J : Joins three lines together
  3. JJ : Joins two lines together
  4. 3gJ : Same as 2, but doesn't remove indent or spaces

Replacement

  1. :s/,/\r/g : sed like replacement
  2. :s/,/<C-V><CR>/g<CR> : same as above but inserts a control character
  3. 0qqwr<CR>q9@q : goes to first char in line record typed characters into register 'q' wr: macro does a word movement and a char replacement 9@q : execute the contents of 'q' 9 times
  4. Iwr<CR><Esc>u9@. : similar to the one above but goes to current register
  5. V"=[<C-R><C-A>]<CR>p : V : visual line "= expression register [ ] list <C-R> cursor moves to the command-line <C-A> this normally adds to the number at the cursor, not sure what this C-A does p : put command <C-R><C-A> seems to get the word at the cursor of the selected text

Comments in vimgolf:

jdupont655: @chunleng: <C-R><C-A> is one of several ways to automatically insert some text on the command line, when this text is already under the cursor in the buffer.

Depending on type of text, you can hit different mappings:

  • <C-R><C-W> for a word,
  • <C-R><C-A> for a WORD,
  • <C-R><C-F> for a filepath,
  • <C-R><C-P> for an expanded filepath.

Here, when you hit <C-R><C-A>, your cursor is on one of the digits, so the WORD under the cursor is ——…2,…,29,. Besides, the number of minus signs in front of the number 2 is even (24; 1 at the end of the first line, then 23 on the second one). So, -—…2 evaluates to the positive number 2. Finally, you replace the line by putting the evaluation of an expression given through the expression register ("={expr}p). Here, the expression is a list ([2, …, 29]), and when you ask to put a list, Vim puts each of its item on a separate line.

Comments

Comments powered by Disqus