Skip to content

Instantly share code, notes, and snippets.

@skalnik
Created April 5, 2012 03:26
Show Gist options
  • Save skalnik/2307726 to your computer and use it in GitHub Desktop.
Save skalnik/2307726 to your computer and use it in GitHub Desktop.
<Leader>al to move down a line and insert delta of spaces
map <Leader>al :call MoveDownLine()<CR>
function! MoveDownLine()
let cols=col('.')
execute "normal! j"
let next_cols=col('.')
let delta_cols=cols-next_cols
if(delta_cols > 0)
if(next_cols==1)
let delta_cols += 1
endif
execute "normal! " . delta_cols . "A "
endif
endfunction
@alindeman
Copy link

Close, but I actually wanted the line to move. I ended up with this, thanks only to you giving me a head start. Thoughts?

map <Leader>al :call MoveDownLine()<CR>

function! MoveDownLine()
  let cols=col('.')
  execute "normal! Do"

  let next_cols=col('.')
  let delta_cols=cols-next_cols

  if(delta_cols > 0)
    execute "normal! " . delta_cols . "A "
  endif

  execute "normal! p"
endfunction

@skalnik
Copy link
Author

skalnik commented Apr 5, 2012

When it comes to Vimscript stuff, my thoughts usually end up at "if it works, it works!" since it's hard to test and things this small don't really have a way of doing it "right" that I know of. Looks good to me! 👍

@alindeman
Copy link

Slightly more fancy?

map <Leader>o :call MoveLine('o', 'k')<CR>
map <Leader>O :call MoveLine('O', 'j')<CR>

function! MoveLine(open_motion, post_motion)
  let cols=col('.')
  execute "normal! D" . a:open_motion

  let next_cols=col('.')
  let delta_cols=cols-next_cols

  if(delta_cols > 0)
    execute "normal! " . delta_cols . "A "
  endif

  execute "normal! p" . a:post_motion
endfunction

@skalnik
Copy link
Author

skalnik commented Apr 5, 2012

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment