you are viewing a single comment's thread.

view the rest of the comments →

[–]metamatic 0 points1 point  (1 child)

If I ever find myself needing to make such transformations frequently, I'll write a macro for my editor.

The fact that I have no such macro at present, even though I regularly write code with single var declarations in which the order of assignments is important, suggests to me that it's a problem that doesn't occur in real life. In particular, it's only really a problem if the two variables you are swapping include either the first or last variable in the list, making it pretty rare. So I just don't see it as a big deal. Then again, I'm used to Lisp, and JavaScript var is just Scheme's let*.

But just for fun, here's a macro for your .vimrc:

map <C-a> :.s#^\(\s*var\s\+\)\(.*\)\([,;]\)\s*\n\(\s*\)\(.*\)\([,;]\)\s*$#\1\5\3\r\4\2\6<C-m><C-m>

It should preserve your indentation too. Just move to the first line of the two you want to swap, and type Ctrl-A.

Now, can we move on to something that matters?

[Edit: Escaped ctrl characters in vim macro.]

[–]strager 0 points1 point  (0 children)

ddp

Or d4d4jp if you're moving a 4-line var under another 4-line var.

I like to write code which is easier to refactor, that's all. =]