Wikia

Vim Tips Wiki

Move function parameters with macro

Talk0
1,610pages on
this wiki
Tip 1077 Printable Monobook Previous Next

created December 13, 2005 · complexity intermediate · author Robert Ames · version 5.7


"Move after next comma
nmap ,mc "zdiWxf,a <Esc>"zp

"Move until next parenthesis
nmap ,mp "zdiWxf)hi, <Esc>"zpx

Given:

foo = compare( "first", second, theThird );

Putting cursor on "first" and pressing ",mc" will move the "first" parameter after the following comma:

foo = compare( second, "first", theThird );

Putting cursor on "second" parameter and pressing ",mp" will move it to the parenthesis:

foo = compare( "first", theThird, second );

This is similar to the old "xp" trick (which will transpose two characters) with a delete and a put. The parameter under the cursor is snipped with "diW" (delete Inner FullWord), which will include the comma. To put it after a comma is simply a paste in the right spot (f,p). To put it as the last parameter, you've got to add a comma to the last paramter and remove its last comma.

This is dependent on spaces between parameters or will require some tweaking to match how you work. BE CAREFUL AS IT WILL EAT YOUR CODE IF YOU DON'T WATCH IT CLOSELY!

This tip changes the "z" register. Also, it looks like the tip doesn't correctly deal with strings that have spaces.

CommentsEdit

Swapping characters, words and lines has some mappings for swapping words around most any non-word characters (comma, equals, etc).


Slide visual highlighted words (on a line) back and forth to beginning/end-of-line, or by character/word. These mappings have protection against "code eating".

"shift word forward/backward
vmap <C-Left> <Esc>`<<Left>i<Esc>gvx`^Pgv<Left>o<Left>o
vmap <C-Right> <Esc>`><Right>gvxpgv<Right>o<Right>o
vmap <C-A-Left> <Esc>`<<S-Left>gvx<S-Left>Pgv`[o`]o
vmap <C-A-Right> <Esc>`><S-Right>gvxi<S-Right><C-r>"<Esc>gv`]o`[o<BS>
vmap <C-Home> x0Pv`]o`[
vmap <C-End> x$pv`[o`]

Advertisement | Your ad here

Around Wikia's network

Random Wiki