10
\$\begingroup\$

Your task is to build a vim script or provide a sequence of keystrokes that will operate on a single line of text with up to 140 printable ASCII characters (anywhere in a file, with the cursor starting anywhere in the line) and reverse every space-separated string in the sentence while keeping the strings in the same order.

For example, the input:

roF emos nosaer m'I gnisu a retcarahc-041 timil no siht noitseuq neve hguoht ti t'nseod evlovni .rettiwT RACECAR 

should return:

For some reason I'm using a 140-character limit on this question even though it doesn't involve Twitter. RACECAR 

The script with the fewest characters, or the sequence of the fewest keystrokes, to achieve this result is the winner.

asked Mar 28, 2015 at 16:54
\$\endgroup\$
15
  • 1
    \$\begingroup\$ "For the purposes of this question it's vim only" seems as arbitrary a language-restriction as posting a normal code golf challenge and asking only for answers in C. (And I don't seem to be alone with this opinion.) \$\endgroup\$ Commented Mar 28, 2015 at 18:08
  • 29
    \$\begingroup\$ Why is RACECAR not reversed? \$\endgroup\$ Commented Mar 28, 2015 at 19:02
  • 3
    \$\begingroup\$ Because it's a palindrome. Try reversing it yourself. \$\endgroup\$ Commented Mar 28, 2015 at 19:02
  • 2
    \$\begingroup\$ Wow, I'm stupid. Derp. \$\endgroup\$ Commented Mar 28, 2015 at 19:03
  • 7
    \$\begingroup\$ @orlp Lol. I thought you were joking. \$\endgroup\$ Commented Mar 29, 2015 at 2:12

2 Answers 2

10
\$\begingroup\$

(削除) 28 (削除ここまで) (削除) 25 (削除ここまで) 24 keystrokes

:se ri<CR>^qqct <C-r>"<Esc>f l@qq@q

Recursive macro, I assume that Ctrl-r counts as one keystroke.

The hardest part was to make sure the macro stays on the same line and does not destroy the rest of the file.

answered Mar 28, 2015 at 23:02
\$\endgroup\$
6
  • \$\begingroup\$ You could use cE instead of ct , if it wasn't ending the macro. But you can use W instead of f l to save 2 strokes. \$\endgroup\$ Commented Mar 29, 2015 at 23:43
  • \$\begingroup\$ @Caek Wrong x2. Guess what cE does when the cursor is at the beginning of a retcarahc-041? And guess what W does when we're at the end of the line? \$\endgroup\$ Commented Mar 30, 2015 at 0:39
  • \$\begingroup\$ Note the capital E. lowercase e would go until the dash, capital E would go until the next space. I just tried it to confirm. \$\endgroup\$ Commented Mar 30, 2015 at 0:46
  • \$\begingroup\$ try: :set ri<Enter>^qqct <C-r>"<Esc>W@qq@q for 23. \$\endgroup\$ Commented Mar 30, 2015 at 0:49
  • \$\begingroup\$ @Caek That won't work. And regarding E, I know what it does. I was referring that cE<C-r><Esc> would turn a retcarahc-041 into 140-character a, AKA it would swap the words. \$\endgroup\$ Commented Mar 30, 2015 at 1:13
4
\$\begingroup\$

24 keystrokes

ma:s/ /\r/g
V'a:!rev
gvJ

I know this question is very old, but I love vimgolf so I couldn't not post an answer on one of the few vim-specific challenges on the site. Plus this solution is tied with Orlp's.

Just like Orlp said, the hardest part was making sure that the rest of the buffer was unmodified. If it weren't for that restriction, we could simply do:

:s/ /\r/g
!{rev
V}J

(19 keystrokes) but we need a little bit more to keep it buffer-safe. This assumes a unix environment.

answered Feb 10, 2017 at 21:46
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.