Reverse every word in a line in vim

9

1

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.

Joe Z.

Posted 2015-03-28T16:54:34.067

Reputation: 30 589

Can the cursor be anywhere on the line or can we assume it's at the start? (Or somewhere else?) Will this be just one line in a larger text file or the only contents of the file? Will it be only printable ASCII or could the be tabs, say? If so, to tabs delimit words or not? Also, does this have to be limited to vim? Wouldn't the challenge make just as much sense in emacs or some other scriptable editor? – Martin Ender – 2015-03-28T18:05:34.833

The cursor can be anywhere on the line. This will be a single line in a larger text file. Only printable ASCII characters will appear on the line. For the purposes of this question it's vim only, but feel free to post the Emacs equivalent. – Joe Z. – 2015-03-28T18:06:49.273

1

"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.)

– Martin Ender – 2015-03-28T18:08:47.580

I admit that it seems that way, but I haven't seen anybody else post editor-agnostic puzzles, and vim-golf is generally a much larger community than, say, emacs-golf or nano-golf (the latter of which returns something entirely different when put into a Google search). – Joe Z. – 2015-03-28T18:10:27.350

"I haven't seen anybody else post editor-agnostic puzzles" doesn't mean you can't be the first person to make it better. Also it's not like you will lose the "much larger community" of vim golfers by making your challenge inclusive of other editors, too. – Martin Ender – 2015-03-28T18:11:48.900

I had a feeling you might say that. If you can post an Emacs solution that bests the winning vim solution, I'll consider it. – Joe Z. – 2015-03-28T18:16:37.573

27Why is RACECAR not reversed? – orlp – 2015-03-28T19:02:13.500

3Because it's a palindrome. Try reversing it yourself. – Joe Z. – 2015-03-28T19:02:54.090

2Wow, I'm stupid. Derp. – orlp – 2015-03-28T19:03:20.977

7@orlp Lol. I thought you were joking. – mbomb007 – 2015-03-29T02:12:39.280

@mbomb007 Sometimes your brain freezes for a second :) – orlp – 2015-03-29T02:13:05.670

Is the space at the end of the line intentional? If the space isn't there I can get it pretty short. – Caek – 2015-03-30T01:00:08.187

@Caek It would be unfair to me to remove it now - I had to work around it too. Changing the requirements during a challenge (assuming the change isn't necessary) basically invalidates all work by previous competitors and is frowned upon. – orlp – 2015-03-30T01:15:36.733

@Caek: Even if the space weren't there, your program should be able to work around initial and terminal spaces. – Joe Z. – 2015-03-30T01:19:42.013

@orlp I didn't mean to change the question I was just curious. JoeZ: noted. – Caek – 2015-03-30T01:29:17.057

Answers

9

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.

orlp

Posted 2015-03-28T16:54:34.067

Reputation: 37 067

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. – Caek – 2015-03-29T23:43:43.550

@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? – orlp – 2015-03-30T00:39:52.273

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. – Caek – 2015-03-30T00:46:50.780

try: :set ri<Enter>^qqct <C-r>"<Esc>W@qq@q for 23. – Caek – 2015-03-30T00:49:28.947

@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.

– orlp – 2015-03-30T01:13:49.313

Ah, I only tried it with the space at the end. Without a space, like in your example, I admit it didn't work. Can also save a character with :se ri – Caek – 2015-03-30T01:27:54.597

3

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.

James

Posted 2015-03-28T16:54:34.067

Reputation: 54 537