How to efficiently switch arguments in vim

I come upon one scenario when editing a file in vim and I still haven't found a way to do it quickly in vim way. When editing a call of a function, I offently put my arguments in a wrong order.

anyFunction(arg2, arg1)

When arriving on this situation, I have to find arg2 / delete it / append it before the ')' / deal with the ', ' / etc.

Isn't it a better way to this task quickly ? I am open to any idea (macro/ shortcut / plugin) even if I'd rather have a 'vim only' way of doing this


You need two things:

  • A text object to quickly select an argument (as they aren't always that simple like in your example). argtextobj plugin (my improved fork here) does this.
  • Though you can use delete + visual mode + paste + go back + paste, a plugin to swap text makes this much easier. My SwapText plugin or the already mentioned exchange plugin both do that job.

  • I would recommend a plugin: vim-exchange for that:

    https://github.com/tommcdo/vim-exchange


    put this mapping in your _vimrc.

     " gw : Swap word with next word 
     nmap <silent> gw :s/(%#w+)(_W+)(w+)/321/<cr><c-o><c-l>
    

    then in normal mode with the cursor anywhere in arg1 type gw to swap parameters

     anyFunction(arg1, arg2)
    

    Explanation:-

    arg1 the separator (here a comma) and arg2 are put into regexp memories 1 2 3

    the substitute reverses them to 3 2 1

    Control-O return to last position

    Control-L redraw the screen

    Note that the separator is any non-alphanumeric character or string e,g whitespace

    链接地址: http://www.djcxy.com/p/49470.html

    上一篇: 如何在视觉模块模式中选择整个列?

    下一篇: 如何在vim中有效地切换参数