sponsor Vim development Vim logo Vim Book Ad

basic Tip #321: Centura swap with upper/lower line behavier

 tip karma   Rating 14/5, Viewed by 1613 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   August 27, 2002 14:52      complexity:   basic
author:   Simon "neoneye" Strandgaard      as of Vim:   6.0

I was once forced to use a windows development suite called "Centura".
The only good thing i remember was its swap current_line with upper/lower line.

function! MySwapUp()
    let cur_col = virtcol(".")
    normal ddkkp
    execute "normal " . cur_col . "|"
endfunction

function! MySwapDown()
    let cur_col = virtcol(".")
    normal ddp
    execute "normal " . cur_col . "|"
endfunction

" swap lines and preserve cursorx
" todo: in visual mode, perform swap with line before/after the selection
noremap <S-Up> :call MySwapUp()<CR>
noremap <S-Down> :call MySwapDown()<CR>

 rate this tip  Life Changing Helpful Unfulfilling 

<< Borland pageup/down behavier | text template with placeholders >>

Additional Notes

salmanhalim@hotmail.com, August 27, 2002 19:53
Instead of "ddkkp", how about simply "ddkP"?  One less keystroke!  :)  (Saving keystrokes is one of the biggest reasons why people use vi-like editors in the first place.)
salmanhalim@hotmail.com, August 27, 2002 20:02
Forgot to mention this, but it might be better to make the mappings <silent> so the effects can't be seen on the command-line.  Also, swapping up when the cursor is on the first line doesn't appear to be quite what is desired (or the first TWO lines if the ddkkp format is used instead of ddkP), so a check to make sure that line( '.' ) > 1 for the up and < line( '$' ) for the down might not be a bad idea.

I DO like this idea and, with the above modifications, am adding it to my list of mappings so please don't take the suggestions the wrong way.
salmanhalim@hotmail.com, August 27, 2002 20:09
*Sigh* Try swapping up when on the last line. . .  Need to add yet another check in there; my final version:

function! MySwapUp()
  if ( line( '.' ) > 1 )
    let cur_col = virtcol(".")
    if ( line( '.' ) == line( '$' ) )
      normal ddP
    else
      normal ddkP
    endif
    execute "normal " . cur_col . "|"
  endif
endfunction

function! MySwapDown()
  if ( line( '.' ) < line( '$' ) )
    let cur_col = virtcol(".")
    normal ddp
    execute "normal " . cur_col . "|"
  endif
endfunction

noremap <silent> <S-Up> :call MySwapUp()<CR>
noremap <silent> <S-Down> :call MySwapDown()<CR>
littledragon@altern.org, September 2, 2002 2:48
Use normal! instead of normal... Who knows what people might have mapped on d, k or P :)
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo