Insert complete lines with Emacs + Evil - emacs

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?

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).

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.

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.

Related

How can I get C-c C-n to format the current line in proof-general coq-mode-map

I'm using proof-general to write Coq proofs.
When I use C-c C-n in a proof, my cursor is moved to the next line, but the current line is not formatted. So for instance, if I type:
intros n. <C-c C-n>
my cursor moves to the next line, but intros n. remains unindented. So I have to go back up a line, go to the end of the line and hit <RET> to auto-indent the coq statement. At which point, proof-general considers that statement changed and I have to re-run it.
Ideally, I would line C-c C-n to auto-indent the line it is running and to not go to the next line.
How can I make that happen?
You can do what you're describing using advice:
(advice-add 'proof-assert-next-command-interactive :before
(lambda (&rest args) (indent-for-tab-command)))
… but I suspect this is an AB problem: C-c C-n doesn't insert a newline, so if you're writing proofs you will in fact press RET roughly once per time you press C-c C-n. If you press it before pressing C-c C-n, then the line will be properly indented.
Worse, you shouldn't typically have an incorrectly indented intros in the first place, if you're using the default setup (electric-indent): whichever parent line is causing intros to need indentation should also cause it to be indented when its line is created.
If I misunderstood, please do update the OP with a concrete example of the scenario that leads you to needing this feature, and we can see whether there is already something in PG that might help.
I was also thinking about advice-add as suggested by #Clément, but actually I believe the OP's suggestion is rather a bug-fix than a feature wish…:
When we type C-c C-n just before adding a newline with RET, if the line is not already properly indented (which can occur if this line was edited not after typing RET previously on the line before, which can occur in practice), the C-c C-n action is distractedly removed… so we need to redo C-c C-n anew.
So I've just opened a tentative PR in https://github.com/ProofGeneral/PG/pull/604, #azani and #Clément: feel free to comment/review there when you have some time.

How to copy several lines from a buffer to another in Emacs?

I have two buffers. I want to copy the line 2, 5, 9 and 10 in the first buffer to the second buffer(just append to the second buffer). Except copy and past one line at a time, is there any elegant way to do so?
You can use the command append-next-kill, bound to C-M-w, to add more text to the latest entry on the kill ring. Use it right before a M-w command, something like this:
Mark and copy line 2: C-SPC, C-n, M-w
Move to line 5
Mark and append-copy it: C-SPC, C-n, C-M-w, M-w
Move to line 9...
...and so on.
When you yank the text with C-y, you'll get the lines you selected and appended to the kill ring entry all together.
For off-the-shelf functionality, I would recommend looking at multiple-cursors.el. It's particularly effective when you have a symbol/regexp that you need to select and modify in multiple places.
https://github.com/magnars/multiple-cursors.el
However, it can also handle specific lines/points through its generic add cursor function:
mc/add-cursor-on-click
But this means you have to bind it to a mouse event (as recommended in the Tips section):
(global-set-key (kbd "C-S-<mouse-1>") 'mc/add-cursor-on-click)
It means you have to reach for the mouse, but it looks pretty easy to create a new function mc/add-cursor-at-line:
(defun mc/add-cursor-at-line (&optional linenum)
(interactive "nLine number: ")
(save-excursion
(goto-char (point-min))
(forward-line (1- linenum))
(mc/create-fake-cursor-at-point))
(mc/maybe-multiple-cursors-mode))
Then, once you have multiple cursors setup on all your lines:
Copy all cursors:
C-SPC C-e M-w RET
Switch buffers and...
Yank Rectangle: C-x r y
A simple solution would be recording a macro that copies a line from one buffer to another ( f3 C-SCP C-n M-w C-x o C-y C-x o f4) and then execute the macro (f4)on every line you want to copy.
If this is a recurring scenario for you, you can save the macro and bind it to a key.
Depending on the exact use case, I would probably copy the whole region from the first line to the last line inclusively, and then kill those lines that I don't need in the new buffer.
1. move to beginning of first line to copy
2. C-space set mark to begin marking a region
3. move the end of the last line to copy
4. M-w copy region
5. C-x b buffer-name RET go to second buffer
6. move to insertion position in second buffer
7. C-y insert (yank) region
8. C-x C-x go to beginning of yanked region
9. C-n repeat until you get to first unwanted line
10. C-k C-k delete unwanted line
11. goto 9 until done
If there are longer stretches of unwanted lines to be delete in the new buffer, steps 9. through 11. should be replaced by something like:
9. move to beginning of longer stretch of unwanted lines
10. C-space set mark to begin marking a region
11. move to end of longer stretch of unwanted lines
12. C-w kill region
13. goto 9 until done

Emacs move-end-of-line, [END], or C-e not moving to end of line?

I've noticed that C-e <END> or M-x move-end-of-line doesn't always move the cursor to the end of the line.
Specifically this happens lines wider than the current window, it appears to move to some arbitrary point midway along the line.
Does anyone know if this is expected and more importantly, how to switch it off and make move-end-of-line, really move to the END of the line?
(Note: this is also happening in regular non-macro use.)
Emacs version in this example is GNU Emacs 23.1.97.1 (i386-mingw-nt6.1.7601)
Update.
The cursor is moving to the char that is on the edge of the window, (the display then re-centers around the cursor.)
Make sure visual-line-mode is off for the buffer.
Agree with #Slomojo here (it seems I cannot comment yet).
To add, here is the quote from the Emacs manual:
In Visual Line mode, some editing commands work on screen lines instead of logical lines: C-a (beginning-of-visual-line) moves to the beginning of the screen line, C-e (end-of-visual-line) moves to the end of the screen line, and C-k (kill-visual-line) kills text to the end of the screen line.
C-e is mapped to end-of-visual-line, the best solution isn't to deactivate visual-line-mode (don't do that especially if you're coding) but to remap C-e to end-of-line in your init file like this:
(global-set-key (kbd "C-e") 'end-of-line)
Of course I advise you to do the same for C-a and to remap it to beginning-of-line.
If you think you need to use end-of-visual-line and beginning-of-visual-line they're still mapped to end and home buttons respectively.

select a line to move it with emacs

I like the TextMate that can select a line to move it with a simple keystroke.
Command-shift L (select a line)
Control-Command Arrow(up and down) (move the selected line)
How can I do the same thing with emacs?
SO user, Sanityinc developed move-text.el (an extract of from basic-edit-toolkit.el - by emakswiki regular, Andy Stewart (lazycat))
See: Move line/region up and down in emacs
It does this task very nicely...
M-Up or M-Down to move the current line (or marked region's whole lines.)
I've added it to http://www.emacswiki.org/emacs/MoveText
Edit the bindings at the bottom to suit your preference.
I just delete the line, then yank it back in the new location. C-a C-k C-k (move to new location) C-y
move-line does that, except for the highlighting, which should be reasonably easy to add.
The standard command C-x C-t is bound to transpose-lines. This is useful for transporting lines via interchange in a few ways.
The most obvious is that exchanges the line the point is in with the previous line.
With an argument of 0 (i.e. M-0 C-x C-t), it swaps the line that has the mark in it with the line that has the point in it.
bbatsov's prelude (a kind of Emacs starter kit) includes this functionality (see this section of code):
(defun prelude-move-line-up ()
"Move the current line up."
(interactive)
(transpose-lines 1)
(forward-line -2)
(indent-according-to-mode))
and binds it to M-S-up and M-S-down.

Unable to clear a beginning of a line by C-u in Emacs

I run the following command in Emacs unsuccessfully
C-u
How can you clear the beginning of a line in Emacs?
You can also accomplish this by using a prefix arg for kill-ine (usually C-k). From the Emacs help (C-h k C-k):
With zero argument, kills the text before point on the current line.
So, you can do
C-u 0 C-k
or even better
C-0 C-k
You can set the mark, go to the beginning, kill till mark:
C-Spc C-a C-w
Try this:
M-0 C-k
Delete from beginning of line to point
C-u works in Bash "Emacs Mode", but not actually in Emacs. Here's what I usually do:
C-a C-k
But this is really only good if you want to kill the whole line. Svante's advice will clear from the beginning of the line to where your cursor was, as you asked for.
I don't know, where you read about C-u, but it is bound to the universal argument in Emacs.
If you want to kill the whole line, call kill-whole-line which is bound C-S-backspace. No matter at what column is the cursor, it will kill the whole line from beginning to end.
I have a small function bound to a key-chord:
(defun kill-start-of-line ()
"kill from point to start of line"
(interactive)
(kill-line 0))
(define-key global-map "\M-#" 'kill-start-of-line)
M-# is usually Alt-Shift-3, not a new DotNet language
I'm sure I saw this somewhere else, but didn't save the original reference.