Visual move line down

Aaron Jensen aaronjensen at
Sun Mar 6 17:07:14 CET 2016

Hi there,

I’m trying to reimplement this handy vim snippet:

vnoremap J :m '>+1<CR>gv=gv

vnoremap K :m '<-2<CR>gv=gv

What it does is it moves visually selected lines down with J and up with K, reindenting them as it goes and maintaining the same visual selection so it is easily repeatable w/ J/K

So far, this is what I have (ignoring the reindent portion for now):

(evil-define-operator my-move-line-down (beg end type)

  "Move selected lines down"

  (evil-move beg end (+ 0 (line-number-at-pos end)))

  (call-interactively evil-visual-restore)


(define-key evil-visual-state-map (kbd "J") 'my-move-line-down)

It has a few problems:

evil-visual-restore doesn’t work like it does in vim, in particular, it is restoring to include the original first line of the selection as well as the text that has moved. In other words, if I select 2 lines, then hit J, 3 will be selected afterwards, the selection expands down instead of moving down.

I can’t figure out how to get the count such that 3J would move down 3 lines. I’d like to replace the 0 with the count.

I’m not sure if I’m supposed to use evil-move in this way, it seems to work, but I don’t know if it is abusing a command. Originally I tried to do this by programmatically executing an ex command but apparently that is a no-no.


-------------- next part --------------
An HTML attachment was scrubbed...

More information about the implementations-list mailing list