tags:

views:

145

answers:

3

Hello, (NB : my first post)

In a Vim file, how may I swap - in one move - line 15 and line 33 (e.g.) ?

Thanks in advance

ThG

+4  A: 

Two ways I can think of. With Vim there are probably more!

:33 | delete | 15 | put | 15 | delete | 32 | put

...or...

13ggdd15ggPjdd33ggP

...which is fewer keystrokes but a little less comprehensible when written down!

Johnsyweb
This will only move - not swap. For swapping you've to do `:33 | delete | 15 | put | 15 | delete | 32 | put`
Amarghosh
You're right, I failed to finish the job. Updated answer accordingly.
Johnsyweb
Thank you both for your answers : I tested the 2 of them (I am not a programmer). Both worked of course perfectly ; the only problem is to remember the code, the command being easier (with the twist of 32 instead of 33)Thank you againThG
ThG
You're very welcome!
Johnsyweb
A: 

I'm frequently using the following:

" Tip #470 : Piet Delport & Anthony (ad_scriven)
vnoremap <silent> g" <esc>:call <sid>SwapVisualWithCut()<cr>

function! s:SwapVisualWithCut()
  normal! `.``
  if line(".")==line("'.") && col(".") < col("'.")
    let c = col('.')
    normal! gvp```]
    let c = col('.') - c
    normal! ``
    :silent call cursor(line("."),col(".")+c)
    normal! P
  else
    normal! gvp``P
  endif
endfunction

The idea is to delete something anywhere, then go select characters elsewhere, and hit g" to swap the delete characters with the newly selected ones.

Luc Hermitte
Bonjour,J'ai lu votre réponse et vous en remercie (au fait, est-il correct d'écrire en français sur ce forum ?). Je suis néanmoins à mes tous débuts dans VIM ; je compte l'utiliser (si je progresse) pour écrire un livre (ici chaque ligne est en fait l'équivalent d'un paragraphe).Mais votre réponse est pour l'heure encore trop "calée" pour moi, ce que je déploreThanks again anyway (that's english !)ThG
ThG
This code has to go either into your .vimrc as it is, or into a plugin -- actually this is already defined into a plugin of mine: http://code.google.com/p/lh-vim/source/browse/misc/trunk/plugin/vim-tip-swap-word.vim. Then all you have to remember is how g" is meant to be used. Somehow, it's less arcane than remembering the commands `:put` and `delete`
Luc Hermitte
Regarding whether it's OK to speak French here. Well as it usually pisses me off when interesting answers are given in English speaking fora but in a tongue I don't speak, I avoid doing it myself. Try the mailing-list vim-fr (yahoo), or dvpz, or even the sdz if you prefer answers in French.(Transl: Concernant le fait de parler français ici. Comme généralement ça m'agace de voir des réponses intéressantes dans des langues que je ne comprends pas, sur des forums où j'étais sensé pouvoir comprendre, j'évite de le faire moi-même. Pour des questions sur vim en français, il y a vimfr, dvpz, le sdz.
Luc Hermitte
+1  A: 

Fastest way is to move lines:

:33m 15|15m 33

Move line 33 below line 15, then moves line 15 below line 33.

It's best to do it 'move high-number below low-number' first, otherwise you have to adjust offsets:

:15m 33|32m 14

searlea