I customize Emacs for myself (v 24.5.1). I need the function to move a block of code (for example line or region) up / down found here: https://www.emacswiki.org/emacs/move-text.el
The function works correctly both with the line (line) and with the block (region), but there is a slight problem when moving the line up, namely, if you become a point per line and you call the move-text-up function. The line moves up once, the point remains on the same line as it was. Respectively, pressing again only returns the line that needed to be moved a few lines higher.

Below I cite only a specific fragment with functions, the full text of the file can be found at the link above.

 (defun move-text-internal (arg) (cond ((and mark-active transient-mark-mode) (if (> (point) (mark)) (exchange-point-and-mark)) (let ((column (current-column)) (text (delete-and-extract-region (point) (mark)))) (forward-line arg) (move-to-column column t) (set-mark (point)) (insert text) (exchange-point-and-mark) (setq deactivate-mark nil))) (t (let ((column (current-column))) (beginning-of-line) (when (or (> arg 0) (not (bobp))) (forward-line) (when (or (< arg 0) (not (eobp))) (transpose-lines arg)) (forward-line -1)) (move-to-column column t))))) ;;;###autoload (defun move-text-down (arg) "Move region (transient-mark-mode active) or current line arg lines down." (interactive "*p") (move-text-internal arg)) ;;;###autoload (defun move-text-up (arg) "Move region (transient-mark-mode active) or current line arg lines up." (interactive "*p") (move-text-internal (- arg))) ;;;###autoload (defun move-text-default-bindings () "Bind `move-text-up' and `move-text-down' to M-up and M-down." (global-set-key [M-up] 'move-text-up) (global-set-key [M-down] 'move-text-down)) (provide 'move-text) 

PS Yes, look for errors in someone else's code is not necessary. Perhaps there is a better solution.

    1 answer 1

    I recommend using the shift-text module from melpa. I used your snippet for a while, but it has other problems.

    From my config :

     (use-package shift-text :ensure t :bind (("CMn" . shift-text-down) ("CMp" . shift-text-up) ("CMf" . shift-text-right) ("CMb" . shift-text-left) ("CMj" . shift-text-down) ("CMk" . shift-text-up) ("CMl" . shift-text-right) ("CMh" . shift-text-left))) 

    (besides the standard, for navigation I use vim-style HJKL)

    • Thanks, I will try, in the evening. - Iurii Golskyi
    • one
      added a config in response - 11111000000
    • Great, just tried it - it works as it should. And thanks for the config, I’m thinking of a vim-style once to start using - Iurii Golskyi