3

To improve my vim skills, I had a look at this vim golf challenge

http://vimgolf.com/challenges/50048db8cdc4060002000004#

Entry #346 was this

:%s/.*/"\0",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ

I entered command mode and type this

%s/.*/"\0",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ

and hit enter

I don't think I did it right because my end file didn't in any way resemble the desired output

My start file looked like this

There
is
no
vertical
limit
for
vim
Ninjas

The expected end file

[ "There", "is", "no", "vertical", "limit", "for", "vim", "Ninjas" ]

My end file

  1 "There",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ
  2 "is",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ
  3 "no",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ
  4 "vertical",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ
  5 "limit",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ
  6 "for",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ
  7 "vim",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ
  8 "Ninjas",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ
  9 "",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ  
1
  • I find it very amusing, coming from a vimgolf site, that the "\0" can be replaced with the shorter "&" ... Dec 3, 2019 at 16:58

3 Answers 3

3

%s/.*/"\0",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ

You have to physically press Enter key and Esc key where you see <CR> and <Esc>.

Let's break it:

  1. %s/.*/"\0", will match everything put each word inside " " and after each line it will insert ,
  2. f,C ] says find (f) first , (now cursor on ,) and delete this and after that enter in insert mode and put ]. A shortcut for C ] is r] which will replace current char with ] and you don't have to press <Esc> after that.
  3. ggI[ go to start of file and enter in insert mode and put [.
  4. 8J - for next 8 lines make it one line.
  5. ZZ is equivelent with :wq
3
  • Thanks for your answer. Regarding your first point, I understand that .* matches everything, but what does \0 do? Is that a backreference to each new line match? how does it work? Dec 18, 2014 at 14:09
  • Sure. \0 means the whole match. Basically it will insert text matched by entire pattern. A backreference is \1 which will insert text by first backreference. Same for \2, \3 etc.
    – radubogdan
    Dec 18, 2014 at 21:32
  • Hm, try to read the details section for more information and examples: vim.wikia.com/wiki/Search_and_replace#Details. In my opinion it's a little bit hard to start with vim golf, but no doubt it's a best practice you can get. I struggled myself learning advanced xpath techniques and wished for a xpath golf to exist. Regards
    – radubogdan
    Dec 18, 2014 at 21:34
3

Instead of entering special characters like <CR> literally (as 4 different keys), you have to press the Enter key. This notation is common in Vim parlance, as it's also used for mappings. See :help key-notation for details and a full list.

Also, when you see something like ^[, that's a control sequence, in this case Ctrl + [, which is equivalent to Esc. You can enter those via Ctrl + V, followed by Ctrl + [ (or whatever it was).

1

You have to press the keys, so

:%s/.*/"\0",<CR>f,C ]<Esc>ggI[ <Esc>8JZZ

becomes

:%s/.*/"\0",Enterf,C ]EscggI[ Esc8JZZ

As in, when you read <CR> that means "press the enter key now" and same for <Esc>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.