Jan
Jan

Reputation: 5162

Swap items in comma separated list

Frequently, e.g. when I write Latex code, I come across the task to rearrange items of a list that are separated by commas. This is quickly done if both items are not at the at the beginning or the end of the list. But if they are at the margins, one has to take extra care of the separating comma.

As an example consider

\cite{GirR84, Tar00, Tem77}.

Is there a smart way in vim to put, e.g., the last item to the front or to the middle position?

Upvotes: 9

Views: 4785

Answers (4)

Hongbo Liu
Hongbo Liu

Reputation: 3072

You could try the vim plugin vim-swap, quite powerful and useful!

Upvotes: 3

Peter Rincker
Peter Rincker

Reputation: 45147

I actually made a plugin to deal with a similar situation called argumentative.vim. (Sorry for the plug.)

Argumentative.vim provides the following mappings:

  • [, and ], motions which will go to the previous or next argument
  • <, and >, to shift an argument left or right
  • i, and a, argument text objects. e.g. da,, ci, or yi,

So with this plugin you move to the argument in question and then do a <, or >, as many times as needed. It can also take a count e.g. 2>,.

If you have Tim Pope's excellent repeat.vim plugin installed <, and >, become repeatable with the . command.

Upvotes: 13

Daan Bakker
Daan Bakker

Reputation: 6342

Another interesting way of swapping arbitrary things from the Vim Tips Wiki:

:vnoremap <C-X> <Esc>`.``gvP``P

Upvotes: 3

Thor
Thor

Reputation: 47169

I have the following mappings in .vimrc, I think I have them from this tip on wikia:

nnoremap <silent> gl "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><c-o>/\w\+\_W\+<CR><c-l>
nnoremap <silent> gh "_yiw?\w\+\_W\+\%#<CR>:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><c-o><c-l>

gh will swap word under the cursor to the left and gl to the right.

Upvotes: 6

Related Questions