Reorder delimited items.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
autoload
doc
macros/swap
plugin
test
.gitignore
.travis.yml
README.md
appveyor.yml

README.md

vim-swap

Build Status Build status

A Vim text editor plugin to swap delimited items.

What for?

Sometimes I want to reorder arguments of a function:

call func(arg1, arg2, arg3)

Indeed, it is not difficult to swap arg2 and arg3. Just cutting , arg2 and pasting it following arg3. However it is annoying work to swap arg1 and arg3 because no way to avoid repeating cut&paste. Anyway I feel bothering in both cases. It should be automated such an boring processes!

How to use

This plugin defines three key mappings in default, g<, g>, gs. These all functions can be repeated by dot command.

g<

g< swaps the item under the cursor with the former item. Moving cursor on the arg2 and pressing g<, then it swaps arg2 and the former one, arg1, to get:

	call foo(arg2, arg1, arg3)

g>

g> swaps the item under the cursor with the latter item. Moving cursor on the arg2 and pressing g>, then it swaps arg2 and the latter one, arg3, to get:

	call foo(arg1, arg3, arg2)

gs

gs works more interactive. It starts "swap mode", as if there was the sub-mode of vim editor. In the mode, use h/l to swap items, j/k to choose item, numbers 1 ~ 9 to select nth item, u/<C-r> to undo/redo, and as you know <Esc> to exit "swap mode". gs function can be used also in visual mode. In linewise-visual and blockwise-visual mode, this plugin always swaps in each line. For example, assume that the three lines were in a buffer:

	foo
	bar
	baz

Select the three lines and press gsl<Esc>, then swaps the first line and the second line.

	bar
	foo
	baz

textobject

The following lines enables to use textobjects to select "swappable" items.

omap i, <Plug>(swap-textobject-i)
xmap i, <Plug>(swap-textobject-i)
omap a, <Plug>(swap-textobject-a)
xmap a, <Plug>(swap-textobject-a)

Those textobjects work well with [count] assignment.

Demo

swap.vim