Help with deleting in Emacs - emacs

I want to delete everything up to the cursor or after the cursor or everything but selected text.

What you want sounds a pretty infrequent thing to be done, but here are a few ways to do it:
First, put cursor on start of text you want to keep. Press C-space to set mark. Then, use M-< (beginning-of-buffer) and afterwards C-w (kill-region-x). Now, everything before text is deleted.
Next, go to end of text and set mark once again with C-space. Now, go to end of buffer with M-> and once again delete everything with C-w.
This is one way to do it.
Other way is to simply copy selected text and put it into new buffer. I'll leave it to you as an exercise.

To keep only the selected text:
C-w C-x h C-w C-y M-y
In other words, kill the selection, select what remains, kill that, then yank the previous selection.

Everything after "the cursor" (aka "point") is deleted using C-k (kill-line). To kill the line up to point, give a 0 argument like this: C-u 0 C-k.
I don't know if there's a way to kill only things that aren't selected. Maybe some kind of "invert-region" before hand?

Related

In emacs how can I kill multiple buffers without opening each individually

I have a lot (~30) of buffers whose names start with the same substring. Is there a way I can pass a regular expression to the kill buffer command so I can kill all of them in one go?
The command I was looking for M-x kill-matching-buffers.
Run M-x ibuffer (built-in command).
On each line with a buffer you want to delete press 'd'.
When finished press 'x' to commit changes.
A more interactive and versatile approach is with helm-buffers-list:
Type what you want to match.
Select all matches with M-m (helm-toggle-all-marks).
Optionally, refine your selection with C-SPC (helm-toggle-visible-marks),
using C-n/C-p to navigate.
Press TAB, select "kill buffers" from the available options.
Other options are query-replace / occur / grep / revert, and a bunch of others.
C-x C-b to list all buffers
k to mark buffers for kill (D appears next to buffer)
x to execute kill
Using helm:
Open helm using helm-mini. In my case I have it mapped to C-2
Scroll through the list and mark the ones you want to delete with C-Space
Now just press M-S-d / M-D
This deletes all selected buffers and closes helm-mini.

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.

In Emacs, how to go back to previous line position after using semantic Jump to Symbol?

I know how to go to a variable definition in Emacs using semantic-mode. It works well in a single file (I think it doesn't work if the definition is in another file). Using C-c , j, I can go to the definition of the variable, but, how do I jump back to the previous line? Currently I use display Symref C-c , g, and select the displayed symref.
Is there any straight method?
Use:
C-u C-space or C-u C-#
If you want to navigate back between buffers, you can use:
C-x C-space or C-x C-#
This makes Emacs jump to the mark (and set the mark from position popped off the local mark ring) which has usually been set by a previous jump command.

Can I keep the same item for yanks in Emacs?

Something I do often in Emacs is to cut a bit of text, and then replace another bit with the cut text. So, say I've got the text I want to yank as the last item in my kill-ring. I yank it into the new place, then kill the text that was already there. But now the killed text is the latest item in the kill-ring. So next time I want to yank the first item, I have to do C-y M-y. Then the next time there are two more recent items in the kill-ring, so I have to do C-y M-y M-y, and so on.
I'm guessing there's a better way to do this. Can someone enlighten me please?
Several alternatives:
Turn on delete-selection-mode, and use C-d or delete to delete region without touching the kill-ring.
Use C-x r s i to save text to register i, and later, C-x r i i to insert the saved text.
If the pattern of texts to be replaced can be captured in a regular expression, use query-replace-regexp (C-M-%).
You should use delete-region instead of kill-region.
delete-region deletes the region without putting it in the kill ring. It is bind to <menu-bar> <edit> <clear> by default.
If you only want to use default bindings without using the menu, you could use delete-rectangle with C-x r d but it works on rectangle. It could be fine to use it on a single line like delete-region.
One of the oldest and best kept secrets in Emacs -- dunno why: Emacs has a secondary selection.
And this is exactly what it is good for. It saves another selection of text for you to use, over and over.
Select some text, then yank the secondary in to replace it. Repeat elsewhere. Often this is more convenient, flexible, and precise than something like query-replace.
Please take a look, for your own good -- maybe it will stop being such a little-known feature...
http://www.emacswiki.org/emacs/SecondarySelection
I wrote this function to pop the newest item off the kill-ring:
(defun my-kill-ring-pop ()
"Pop the last kill off the ring."
(interactive)
(when kill-ring
(setq kill-ring (cdr kill-ring)))
(when kill-ring-yank-pointer
(setq kill-ring-yank-pointer kill-ring))
(message "Last kill popped off kill-ring."))
So after I kill something I don't want to keep, I hit a key that calls this.
Related to this is M-x browse-kill-ring. If you use M-x anything, you can also use M-x anything-show-kill-ring.

How to replace a region in emacs with yank buffer contents?

When I use VIM or most modeless editors (Eclipse, NetBeans etc.) I frequently do the following. If I have similar text blocks and I need to change them all, I will change one, copy it (or use non-deleting yank), select next block I need and paste the changed version over it. If I do the same thing in emacs (select region and paste with C-y), it doesn't replace the region, it just pastes at the cursor position. What is the way to do this in emacs?
Add this to your .emacs:
(delete-selection-mode 1)
Anything that writes to the buffer while the region is active will overwrite it, including paste, but also simply typing something or hitting backspace
Setting delete-selection-mode, as Michael suggested, seems the most natural way to do it.
However, that's not what I do :) Instead, I put the good stuff into a "register" -- for example, register "a" -- with C-x r x a. Then I kill the other copy, and copy the register into the same spot with C-x r g a.
This is convenient because killing doesn't affect the registers, so C-x r g a always inserts the good stuff.
The default way to do something like this is the not-entirely-elegant following:
Get your desired replacement text into the kill ring somehow (e.g., M-w).
Highlight the region to be replaced.
Delete it (C-w).
Replace it with the prior-copied region (C-y, M-y). This replaces the freshly deleted contents with the exact same text that you just deleted (C-y), and then re-replaces it with the next most-recently saved buffer in the buffer ring (M-y).
This would get to be a real pain if you wanted to do this 10 times with the same text, as the desired replacement would get pushed farther back in the kill ring each time you deleted a region, so you'd have to call M-w an increasing number of times each time you wanted to yank it.
I've also just discovered M-x delete-region, thanks to Emacs: how to delete text without kill ring?. As the question implies, this deletes the offending text without putting it into the kill ring, avoiding the problem of pushing your replacement text further down on the stack. And, as the relevant response mentions, you can bind this to a shortcut key of your choosing.
The way I do this is:
go to where you want the new stuff
paste the good stuff
your cursor is now between the new stuff and the stuff you want to get rid of
select forward until everything you want to get rid of is selected
delete it
It's a slightly different way of thinking about it. Paste what you want, then get rid of what you don't want, rather than replace what you don't want with what you do.
If you enable CUA-mode, this paste over selected region becomes the normal behaviour.
Use delete-selection-mode, so that pasted text replaces the active region.
Use the secondary selection, to paste the same text over and over, even if you alternately select a new region to replace.
See http://www.emacswiki.org/emacs/SecondarySelection.
For anyone landing on this who doesn't want to change the global setting like me, I use this function and key binding:
(defun replace-yank(beg end)
(interactive "r")
(kill-region beg end)
(yank 3))
(global-set-key (kbd "C-S-y") 'replace-yank)
I intentionally add the selected text to the kill ring incase I want it later, and yank what was previously first in the ring.
If you don't care to preserve the highlighted text in the kill ring you could use something similar to the answer above by ekneiling
(defun replace-yank(beg end)
(interactive "r")
(delete-region beg end)
(yank 1))
(global-set-key (kbd "C-S-y") 'replace-yank)
One advantage is the above can be done repeatedly.