1

When writing code, I often have code of the form

variableA operator variableB

and I would like to edit it to be

variableB operator variableA.

Right now, I do this by deleting/pasting variableB to be in front of variableA and then deleting/pasting operator to be in between them. Is there a slicker way to do this in Vim?

  • Count how many keystrokes you need to do what you're doing. In my case, it's diW2WviWpBBhP which is 13. I would argue it's enough for getting by. Otherwise, create a leader mapping. – klaus Apr 15 at 13:56
2
variableA operator variableB
   ^ cursor here
diwmmwwvep`mP

explanation

  • diw delete variableA into register "
  • mm mark current position
  • ww move cursor to variableB
  • vep replace variableB with variableA, also set register " to variableB
  • `m go back to marked position
  • P paste variableB

It's awkward to swap this way. I recommand tommcdo/vim-exchange

With that plugin all you need is

cxiwww.
  • That seems like so much work! This is why I started learning the ex commands better – D. Ben Knoble Apr 15 at 13:18
  • Yes, i never do it manually, ex commands also won't do you any good during daily swaping. – dedowsdi Apr 15 at 13:22
  • disagree. With practice it’s a fine technique and applicable broadly. – D. Ben Knoble Apr 15 at 13:29
  • 1
    No, in this case, you can't beat github.com/tommcdo/vim-exchange. how do you compete with cxiwww. ? it even has a "." repeat. – dedowsdi Apr 15 at 13:31
  • 1
    Disagree, this is not rare, it's swaping, everyone swaps, it's also not easy without plugin. I highly doubt one can create Mappings or macros that work in a more natural way than github.com/tommcdo/vim-exchange. – dedowsdi Apr 15 at 13:36
2

Yes, via search and replace:

:s/\v(variableA) (operator) (variableB)/\3 \2 \1
  • \v is for very magic to make the parens easier
  • The parens make capture groups, referenced in the substitution to flip the order

You can add a range at the beginning of :s (e.g., :%s... to operate on the entire file) and flags at the end (e.g., ...\1/g to operate on all matches in each line).

Pattern needs adjusting if situation is more complex. See :h pattern

  • Nice use of capture groups, but it is not practically any faster than writing out variableB operator variableA, because I need to write out (variableA) (operator) (variableB)! I can't even yank and paste into the command line, because I still need to go back and add the parentheses. – The_Anomaly Apr 15 at 13:24
  • @The_Anomaly two suggestion: (1) yank it and open the command line window (q:) instead, so you can add parens faster; and (2) use vim-surround from tpope (on github) to make adding said parens really fast. Combine with repeat.vim for maximum effect. Finally, note that by making the regex smarter (\v(\k+) (operator) (\k+), for example) you do save typing time. – D. Ben Knoble Apr 15 at 13:28
  • Very nice. And wow, q: is blowing my mind, not sure how I did not know about this... – The_Anomaly Apr 15 at 13:40
  • 3
    @The_Anomaly, if you didn't know about q: I'm pretty sure you are also gonna like :h c_CTRL-F – klaus Apr 15 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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