Helder S Ribeiro
Agent of User

Agent of User

How to set keybindings to move lines up/down on Spacemacs

Helder S Ribeiro's photo
Helder S Ribeiro

Published on Jun 5, 2021

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Spacemacs already comes with the drag-stuff package, but it's bound to a transient mode (SPC x . opens it), so if you want to have quicker access you can do this:

  • Go to your Spacemacs dotfile (either .spacemacs or .spacemacs.d/init.el; you can open it by pressing SPC f e d)

  • Then inside the expression starting with:

(defun dotspacemacs/user-config ()
  • Add the lines:
(global-set-key (kbd "s-J") 'drag-stuff-down)
(global-set-key (kbd "s-K") 'drag-stuff-up)

To load the new settings, you can reload the entire dotfile with SPC f e R or set the cursor ("point") inside each of the two expressions and press , e c, which evals the expression the cursor is currently inside.

I'm on macOS and s corresponds to the Command key. Note that the J and K are capitalized, so the keybindings correspond to Command Shift j and Command Shift k. Feel free to change to whatever you like best.

Markdown

If you're editing markdown, Spacemacs already comes with two easy keybindings to move items up and down: M-k and M-j, respectively.

That is a markdown-mode keybinding that uses outline and has the added advantage of moving the whole item up and down, so if you're on a multiline list item or one the has nested items inside it, it moves the entire thing without breaking formatting.

Source: github.com/syl20bnr/spacemacs/pull/14449

 
Share this