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.
-
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\$Martin Ender– Martin Ender2015年03月28日 18:08:47 +00:00Commented Mar 28, 2015 at 18:08
-
29\$\begingroup\$ Why is RACECAR not reversed? \$\endgroup\$orlp– orlp2015年03月28日 19:02:13 +00:00Commented Mar 28, 2015 at 19:02
-
3\$\begingroup\$ Because it's a palindrome. Try reversing it yourself. \$\endgroup\$Joe Z.– Joe Z.2015年03月28日 19:02:54 +00:00Commented Mar 28, 2015 at 19:02
-
2\$\begingroup\$ Wow, I'm stupid. Derp. \$\endgroup\$orlp– orlp2015年03月28日 19:03:20 +00:00Commented Mar 28, 2015 at 19:03
-
7\$\begingroup\$ @orlp Lol. I thought you were joking. \$\endgroup\$mbomb007– mbomb0072015年03月29日 02:12:39 +00:00Commented Mar 29, 2015 at 2:12
2 Answers 2
(削除) 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.
-
\$\begingroup\$ You could use
cEinstead ofct, if it wasn't ending the macro. But you can useWinstead off lto save 2 strokes. \$\endgroup\$Caek– Caek2015年03月29日 23:43:43 +00:00Commented Mar 29, 2015 at 23:43 -
\$\begingroup\$ @Caek Wrong x2. Guess what
cEdoes when the cursor is at the beginning ofa retcarahc-041? And guess whatWdoes when we're at the end of the line? \$\endgroup\$orlp– orlp2015年03月30日 00:39:52 +00:00Commented Mar 30, 2015 at 0:39 -
\$\begingroup\$ Note the capital
E. lowercaseewould go until the dash, capitalEwould go until the next space. I just tried it to confirm. \$\endgroup\$Caek– Caek2015年03月30日 00:46:50 +00:00Commented Mar 30, 2015 at 0:46 -
\$\begingroup\$ try:
:set ri<Enter>^qqct <C-r>"<Esc>W@qq@qfor 23. \$\endgroup\$Caek– Caek2015年03月30日 00:49:28 +00:00Commented Mar 30, 2015 at 0:49 -
\$\begingroup\$ @Caek That won't work. And regarding
E, I know what it does. I was referring thatcE<C-r><Esc>would turna retcarahc-041into140-character a, AKA it would swap the words. \$\endgroup\$orlp– orlp2015年03月30日 01:13:49 +00:00Commented Mar 30, 2015 at 1:13
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.