David Wolever
David Wolever

Reputation: 154464

Insert complete lines with Emacs + Evil

In Vim, I often move lines by deleting them (either with dd or visual line mode), moving my cursor to the new position, then p to put them in:

first
second
third

And if my cursor is on the line second, I can use ddp to move it down:

first
third
second

But with Emacs + Evil mode, putting the line back doesn't work as expected: if, for example, my cursor is on the i in third when I hit p, I end up with:

first
thisecondrd

How can I make Emacs + Evil mode insert new lines when putting entire yanked lines?

Upvotes: 4

Views: 1401

Answers (3)

itsjeyd
itsjeyd

Reputation: 5280

If the main use case you are trying to address is moving lines up or down (as opposed to the more general question of how to "make Emacs + Evil mode insert new lines when putting entire yanked lines"), I suggest you try out move-text.

It is a very small add-on package that provides two commands (move-text-up and move-text-down) for moving lines up and down, respectively. You can be anywhere on a line and call these; there is no need to kill or yank anything, and they work for regions as well.

For example, calling move-line-down in this situation (point right after second):

first line
second| line
third line

will produce

first line
third line
second| line

As you would expect, moving the current line (or region) up or down n lines works by calling the appropriate command with a numeric prefix.

The commands are bound to M-up and M-down by default but you should be able to rebind them to key sequences of your liking via

(define-key evil-normal-state-map "mu" 'move-line-up)
(define-key evil-normal-state-map "md" 'move-line-down)

move-text is package-installable from MELPA.

Upvotes: 0

Brandon Rhodes
Brandon Rhodes

Reputation: 89405

If I find my cursor on a line that I want to move, my natural response is to first delete the line into the kill ring with either C-a C-k C-k or C-a C-space C-n C-w (either of which can also grab several-line sequences by duplicating either the C-k or C-n or prefixing the C-n with a numeric argument) and then travel to the beginning of the line where I want to paste and doing a C-y yank.

Note that Emacs considers a file to be a steam of characters, in which newline or carriage return is not special. Unlike in vi, you can C-f forward right over a newline exactly as though it is a normal character; backspace over it; or include it in a deleted and yanked buffer. It is exactly like any other character. Perhaps Emacs is for people who think of files as sequences of characters — some of which happen to be newlines — and vi is for people who think of their file as lines, that are magically separated by who-knows-what but it certainly is not like any other character.

Upvotes: 1

Sacha Chua
Sacha Chua

Reputation: 591

I use C-a to go to the beginning of the line (^ in evil-mode, probably) before yanking, if I want that behaviour. If you do this often, you can probably come up with your own thing for yank, although you have to figure out during the kill part if you're doing that. (Or you can check if the yanked thing has newlines, I guess?)

There's a transpose-lines command, by the way (C-x C-t in regular Emacs binding - someone suggested binding this to xtl - https://github.com/syl20bnr/spacemacs/blob/master/my-keybindings.el).

Upvotes: 1

Related Questions