How to copy several lines from a buffer to another in Emacs? - 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

Related

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

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.

Emacs Command to Delete Up to Non-Whitespace Character

I often want to make a multiline function call and reduce it down to one line. For example, convert...
function_call(
'first_arg',
'second')
to
function_call('first_arg', 'second')
Does emacs have some commands to help with this. Specifically, is there a command that will delete all whitespace from the point to the first non-whitespace character?
You might try delete-indentation, my favorite command for joining multiple lines into one line. In your example, put the cursor on the line with "second" and hit M-^ twice. Here are the docs:
M-^ runs the command delete-indentation, which is an interactive compiled Lisp function in simple.el.
It is bound to M-^.
(delete-indentation &optional arg)
Join this line to previous and fix up whitespace at join. If there is a fill prefix, delete it from the beginning of this line. With argument, join this line to following line.
Take a look at the fixup-whitespace function. It comes with Emacs, in simple.el. Its docs are:
Fixup white space between objects around point.
Leave one space or none, according to the context.
A similar function, just-one-space, that
Deletes all spaces and tabs around point, leaving one space
is typically bound to M-SPC.
Specifically, is there a command that will delete all whitespace from the point to the first non-whitespace character?
There's a command that does almost that:
M-\ runs the command delete-horizontal-space
which is an interactive compiled Lisp function in `simple.el'.
It is bound to M-\.
(delete-horizontal-space &optional backward-only)
Delete all spaces and tabs around point.
If backward-only is non-nil, only delete them before point.
You can always use M-z to delete upto a character.
For eg in your case:
M-z ' to delete upto the single quote (unfortunately this will delete the single quote as well, but that is a minor inconvenience).
I use the following macro to "pull" the next line onto the end of the current line, compressing whitespace.
(defun pull-next-line()
(interactive)
(move-end-of-line 1)
(kill-line)
(just-one-space))
This is exactly the opposite of #jrockway's move-line-up and of delete-indentation, which I find more natural. The just-one-space command in the macro is exactly #Mike's M-SPACE.
I bind pull-next-line to M-J (in analogy with Vim's J, for "join", command) using the following in my .emacs.
(global-set-key (kbd "M-J") 'pull-next-line)
Example. Calling pull-next-line on the first line of
function_call(
'first_arg',
'second')
yields
function_call( 'first_arg',
'second')
Calling it a second time yields
function_call( 'first_arg', 'second')
Alt-space will reduce a string of whitespace to a single space character, but it won't delete the newline. Still, that should help a little.
To delete everything from point to the first non-whitespace (or newline), type a non-whitespace char, Alt-space, backspace (to remove final whitespace char), then backspace (to delete the char you added.
To turn the multi-line function declaration into a single-line declaration, use a combination of Alt-space, backspace, and Alt-E (goto-endofline) commands.
A rather drastic way of doing this is Hungry-Delete mode:
Hungry-Delete is a minor-mode that causes deletion to delete all whitespace in the direction you are deleting.
A slightly different approach would be creating a keyboard macro to do the job for you.
so, for creating the macro stage a general scenario like so:
foo
bar
[a line with "foo" then a couple of lines later and with some white spaces, write "bar"]
then standing anywhere between foo and bar, do the following:
C-x ( ; start recording macro
M-b ; move backwards to the beginning of foo
END ; move to the end of foo
C-space ; place mark
C-M-f ; move to the end of bar
HOME ; move to the beginning of the line
C-w ; yank out all the white space
M-SPACE ; leave only one space
C-x ) ; end recording the macro
M-x name-last-kbd-macro ; name it, call it jline or something
Now you can always remove all whitespace between two words with M-x one-line
Make sure you remember to save your keyboard macro by issuing M-x insert-kbd-macro somewhere in your .emacs file - this is how it looks:
(fset 'jline
[?\M-b end ?\C- ?\C-\M-f home ?\C-w escape ? ])
I do this:
(defun move-line-up ()
"Removes leading spaces from the current line, and then moves
the current line to the end of the previous line."
(interactive)
(let (start end)
(save-excursion
(beginning-of-line)
; get first non-space character, only look on this line
(let ((search-end (save-excursion (end-of-line) (point))))
(re-search-forward "[^[:space:]]" search-end))
(setq end (1- (point)))
(previous-line)
(end-of-line)
(setq start (point))
(delete-region start end))
(goto-char start)))
(defun move-next-line-up ()
"Moves the next line to the end of the current line"
(interactive)
(next-line)
(move-line-up))
And bind these as:
(global-set-key (kbd "C-x ,") 'move-line-up)
(global-set-key (kbd "C-x .") 'move-next-line-up)
So to solve your problem, on the line that says "second)", just run C-x , C-x ,
If you want all of your deletes to act that way, you might check out greedy-delete.