Is there an easy command in vim to cyclically rotate arrays?
For example, selecting "1, 2, 3" in the text and pressing a shortcut should replace the selected text to "2, 3, 1".
Ideally the command should allow for arbitrary cyclic shifts.
You mean something like:
function! s:Rotate(list, rot)
let res = a:list[a:rot :] + a:list[: (a:rot-1)]
return res
endfunction
xnoremap <silent> <c-x>r s<c-r>=join(s:Rotate(split(@", ', *'), v:count1), ', ')<cr><esc>gv
Visually select your list. Hit CTRL-X then r and tada!. If you want to rotate more than of one element at a time, type first the number of shifts you want (the number must be in between -len(list) and +len(list)) before CTRL-X_r
Argumentative.vim plugin allows shifting arguments. The idea being you shift the first argument with an overly large count, e.g. 99.
99>,
This will move the first argument to the end of the array. Then you can get back to the first argument with a similar trick using a boundary motion, [,
99[,
You may also want to take a look a sideways.vim which provides similar functionality to argumentative.vim.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With