Skip to content

Instantly share code, notes, and snippets.

@lleirborras
Created February 25, 2014 10:51
Show Gist options
  • Save lleirborras/9206774 to your computer and use it in GitHub Desktop.
Save lleirborras/9206774 to your computer and use it in GitHub Desktop.
Vim/Mvim/Gvim/NeoVim macro to swap lines with shift-[up | down]
function! s:swap_lines(n1, n2)
let line1 = getline(a:n1)
let line2 = getline(a:n2)
call setline(a:n1, line2)
call setline(a:n2, line1)
endfunction
function! s:swap_up()
let n = line('.')
if n == 1
return
endif
call s:swap_lines(n, n - 1)
exec n - 1
endfunction
function! s:swap_down()
let n = line('.')
if n == line('$')
return
endif
call s:swap_lines(n, n + 1)
exec n + 1
endfunction
noremap <silent> <s-up> :call <SID>swap_up()<CR>
noremap <silent> <s-down> :call <SID>swap_down()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment