Swap Arguments


If you’re writing code in Vim, you’ve probably come across the need to swap the order of items in a list. Those items could take many forms: arguments to method or function, items in an array, attributes in an HTML tag, or CSS declarations. You might have one item at the end that you wish to nudge over in the order. Yes, you can Vim-Golf your way to some succinct solutions to this text-manipulation challenge. But this is a common enough pattern that you might wish to try an excellent extension that makes it effortless: vim-sideways.

After installing the plugin with your choice of plugin manager, add the following mappings to your .vimrc:

nnoremap <c-h> :SidewaysLeft<cr>
nnoremap <c-l> :SidewaysRight<cr>

This will map <Control-h> to the command that moves the argument left, and it will map <Control-l> to the command that moves the argument right. This makes moving and reordering items in any kind of list a simple two-key press.

The plugin supports a huge array of file types, many programming languages, plus HTML attributes, CSS declarations, even some tables. Checkout the README for all kinds of useful tidbits buried in this very thorough plugin.

To see this in action, watch the Vvdeo demo below. In it, I have two files open: a ruby file and an HTML file. Each of these contains items in a list: method arguments and HTML attributes. To move to the argument under my cursor to the right, I use <Control-l> and to the left, I use <Control-h>.

List this tip format? Check out our courses:

Our courses are delivered over email in succinct tip form similar to these email tips. We’ve had a blast writing Git Better with Vim over the few weeks — there is a TON to learn about using Git inside Vim! Try them out:

How useful was this post?

Average rating 3.7 / 5. Vote count: 85

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

61 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

Leave a reply

Your email address will not be published. Required fields are marked *