syl20bnr / spacemacs

A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
http://spacemacs.org
GNU General Public License v3.0
23.64k stars 4.89k forks source link

Move selection up and wown #12563

Closed pistacchio closed 2 years ago

pistacchio commented 5 years ago

Hi, one thing that I always configure my editor to have is Ctrl-Cmd+Up / Ctrl-Cmd+Down to move the current line where the caret or the current selection up and down. See the gif for what I mean.

g

Ho to do this in Spacemacs? Thanks

duianto commented 5 years ago

There's a Move Text Transient State under SPC x J for move down and SPC x K for move up.

It's using the https://github.com/emacsfodder/move-text package

If your using the Emacs editing style, then it probably should work as expected.

But if your using the Evil (vim) editing style, then you'll have to use another package. The author said:

I don't explicitly support eVil mode so if this is something you'd​ like to see done quickly, please submit a patch / pull-request

source: https://github.com/emacsfodder/move-text/issues/11#issuecomment-309384232

If your using the Evil editing style, then you might want to use the drag-stuff package. https://github.com/rejeep/drag-stuff.el

There's no mention of evil support on the first page, but here's a merged PR where they fixed something evil-mode related: Fixes evil-mode dragging and adds evil-mode recipes https://github.com/rejeep/drag-stuff.el/pull/13

I just did some quick testing and it seems to move any lines with either of these types of selections on them:

It doesn't seem to work with C-v (evil-visual-block) selections.

To install the drag-stuff package, add it to the dotspacemacs-additional-packages variable near the top of your .spacemacs file.

   dotspacemacs-additional-packages '(drag-stuff)

Then in the dotspacemacs/user-config section add:

    (drag-stuff-mode t)
    (global-set-key (kbd "<C-S-up>") 'drag-stuff-up)
    (global-set-key (kbd "<C-S-down>") 'drag-stuff-down)
nicobao commented 4 years ago

Thanks @duianto drag-stuff works great. Just a remark:

(kbd "<C-S-up>")
(kbd "<C-S-down>")

should be:

(kbd "C-S-k")
(kbd "C-S-j")

instead, because:

Cheers, Nicolas

duianto commented 4 years ago

@nicobao

< is not recognized and throws an error in Spacemacs

I'm not getting any errors on either the Spacemacs master or develop branches, with:

    (global-set-key (kbd "<C-S-up>") 'drag-stuff-up)
    (global-set-key (kbd "<C-S-down>") 'drag-stuff-down)

I also tried binding them like this, and it worked as well:

  (global-set-key (kbd "C-S-<up>") 'drag-stuff-up)
  (global-set-key (kbd "C-S-<down>") 'drag-stuff-down)

Spacemacs seems to have had keys bound using angle brackets for at least 3 or 4 years (that seems to be when the following lines where last touched), for example, zooming with the mouse wheel:

master branch: https://github.com/syl20bnr/spacemacs/blob/d46eacd83842815b24afcb2e1fee5c80c38187c5/layers/%2Bspacemacs/spacemacs-ui-visual/packages.el#L390-L392 develop branch: https://github.com/syl20bnr/spacemacs/blob/1f6e39ea8ab3e7b7cb0a89889dec307523dfc6b4/layers/%2Bspacemacs/spacemacs-visual/packages.el#L133-L135


using k and j is more idiomatic in Evil mode.

  (global-set-key (kbd "C-S-k") 'drag-stuff-up)
  (global-set-key (kbd "C-S-j") 'drag-stuff-down)

That's a good suggestion, it reduces the need to move the right hand to the cursor keys. 👍


Windows 1903
#### System Info :computer:
- OS: windows-nt
- Emacs: 26.3
- Spacemacs: 0.300.0
- Spacemacs branch: develop (rev. 1f6e39ea8)
- Graphic display: t
- Distribution: spacemacs
- Editing style: vim
- Completion: helm
- Layers:
```elisp
(autohotkey auto-completion command-log dap emacs-lisp git helm html imenu-list javascript
            (markdown :variables markdown-live-preview-engine 'vmd markdown-command "vmd")
            multiple-cursors nim
            (org :variables org-agenda-files
                 '("~/org/notes.org"))
            (shell :variables shell-default-shell 'shell shell-default-height 30 shell-default-position 'bottom)
            spell-checking
            (syntax-checking :variables syntax-checking-enable-by-default nil)
            treemacs version-control)
```
- System configuration features: XPM JPEG TIFF GIF PNG RSVG SOUND NOTIFY ACL GNUTLS LIBXML2 ZLIB TOOLKIT_SCROLL_BARS THREADS LCMS2

rnoennig commented 3 years ago

Another great solution imho came from VanLaser to use evil commands for movement, which also supports moving a selection n lines at once:

(define-key evil-visual-state-map (kbd "M-<down>") (concat ":m '>+1" (kbd "RET") "gv=gv"))
(define-key evil-visual-state-map (kbd "M-<up>")   (concat ":m '<-2" (kbd "RET") "gv=gv"))
(define-key evil-normal-state-map (kbd "M-<down>") (concat ":m +1" (kbd "RET") "=="))
(define-key evil-normal-state-map (kbd "M-<up>")   (concat ":m -2" (kbd "RET") "=="))

But I'd be happy either way as long as spacemacs doesn't come with a broken transient state if you chose to use evil.

duianto commented 3 years ago

A PR has been opened: Replace move-text with drag-stuff #14449

If it's accepted, then the drag-stuff package don't need to be added to the additional packages list.

duianto commented 3 years ago

The PRs changes have been applied to the develop branch: Replace move-text with drag-stuff #14449

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!

robertarles commented 2 years ago

Another great solution imho came from VanLaser to use evil commands for movement, which also supports moving a selection n lines at once:

(define-key evil-visual-state-map (kbd "M-<down>") (concat ":m '>+1" (kbd "RET") "gv=gv"))
(define-key evil-visual-state-map (kbd "M-<up>")   (concat ":m '<-2" (kbd "RET") "gv=gv"))
(define-key evil-normal-state-map (kbd "M-<down>") (concat ":m +1" (kbd "RET") "=="))
(define-key evil-normal-state-map (kbd "M-<up>")   (concat ":m -2" (kbd "RET") "=="))

But I'd be happy either way as long as spacemacs doesn't come with a broken transient state if you chose to use evil.

Thanks for sharing. This is excellent. I’m using this modified to use the j and k keys, but otherwise perfect for me.