in emacs, append-next-kill sometimes prepends - emacs

For example, place point at the beginning of a line with the text "foo bar". Then M-d C- C-e M-C-w C-w C-y produces " barfoo". This behaviour causes problems when I try to switch the order of text and when I combine a real kill with save-as-kill. No doubt prepending is often useful, but all the documentation I have found says that append-next-kill appends. How do I control emacs's choice between appending and prepending?

See the Emacs manual, node Appending Kills. It gives explicit examples, in particular an example that shows clearly what C-M-w is for and what it does. Here is part of that text:
If a kill command is separated from the last kill command by other
commands (not just numeric arguments), it starts a new entry on the kill
ring. But you can force it to append by first typing the command
`C-M-w' (`append-next-kill') right before it. The `C-M-w' tells the
following command, if it is a kill command, to append the text it kills
to the last killed text, instead of starting a new entry. With
`C-M-w', you can kill several separated pieces of text and accumulate
them to be yanked back in one place.
For C-M-w to append the next kill, that kill must immediately follow C-M-w. If you do something else in between then there is no appending. The command name might better have been append-next-kill-if-it-follows-immediately. ;-)

Related

Emacs: relation between keystrokes and keys

I am slightly confused by the difference between keys and key-bindings in emacs after reading the first chapter of "Writing GNU Emacs Extensions" (O'Reilly book).
The chapter starts by noting that in some emacs version the keys backspace and DEL, invoke the help menu instead of deleting. In other words, they invoke what is invoked by C-h.
The chapter then goes on to show how to change this behaviour using Lisp code. This is done by binding the keystroke C-h to the delete command.
This is a bit confusing to me. In my mind DEL, Backspace and C-h are three different keystrokes (the first two consisting of a single key).
Why does remapping C-h effect what DEL and Backspace does?
I would have thought to change what backspace does for example, you would remap backspace to another command, not the keystroke C-h.
Unless remapped by a low-level keybord driver, the effect of the Backspace key is to send the character with numeric code 8, which, in certain operating systems, is exactly the same code generated by pressing Control-h. You can verify this fact by simply writing anything on a unix (or linux) shell and then pressing Backspace and Control-h: both of them have the effect of erasing the previous character, since the character with numeric code 8 is interpreted by the operating system as the control character BS, backspace, used either to erase the last character printed or to overprint it (see wikipedia). Analogously, Control-J is equivalent to the RETURN key, etc.
The same Wikipedia page describe DEL as “originally intended to be an ignored character, but now used in some systems to erase a character”.
So, when you assign a command to a keystroke you are actually assigning a command to a character code, and if two or more keys generate the same code, by pressing them you are invoking the same command.
#Renzo answered your question about how these keys are related and how binding one can seem to affect another. Here is some more information about what's going on in this particular case.
See the Emacs manual, node DEL Does Not Delete.
There you will see this, following an explanation of the problem/confusion:
To fix the problem in every Emacs session, put one of the following
lines into your initialization file (*note Init File::). For the first
case above, where BACKSPACE deletes forwards instead of backwards, use
this line to make BACKSPACE act as DEL:
(normal-erase-is-backspace-mode 0)
For the other two cases, use this line:
(normal-erase-is-backspace-mode 1)
Another way to fix the problem for every Emacs session is to
customize the variable normal-erase-is-backspace: the value t
specifies the mode where BS or BACKSPACE is DEL, and nil
specifies the other mode. *Note Easy Customization::.
See also the GNU Emacs FAQ question about Backspace invoking help. There you will see, in addition to information similar to that above, information about how to remap DEL on UNIX - use this:
stty erase '^?'
Wrt C-j and RET (not mentioned in the question, but mentioned in #Renzo's answer): The default behavior of Emacs changed in most programming modes, in Emacs 24.4.
Starting with that release, electric--indent-mode is turned on by default, which means that RET inserts a newline character and indents, whereas C-j just inserts a newline character. Prior to that release, these keys had the opposite behaviors. If you prefer the old behavior then do this, to turn off electric-indent-mode:
(when (fboundp 'electric-indent-mode)
(electric-indent-mode -1)) ; Use classic `C-j' and `RET'.

how could I using query-replace like isearch-forward while typing something

while isearch-forward run as a command, the context I had typed will hightlight in the current buffer, but while run query-replace don't hightlight that, how can i make it hightlight?
Use isearch-query-replace. It highlights the string to be replaced.
It sounds like you are saying that query-replace does not highlight all of the matching occurrences. Is that right? It should highlight them. If it does not, then try starting Emacs without your init file: emacs -Q. If that shows no lack of highlighting then recursively bisect your init file to find the culprit.
#Rocky mentioned isearch-query-replace. That doesn't change highlighting (which should already be turned on), but what it does do is let you start query-replacing while you are isearching, using the last search string as the pattern for the text to be matched by query-replace.
An alternative to query-replace, useful especially if you have relatively few replacements you want to make and there are lots of matches, is to use on-demand replacement while isearching. For that you need library Isearch+.
To replace any given search hit on demand, just hit C-M-RET. With a prefix arg, C-M-RET prompts you for the replacement text (the default is to replace with no text, which means to delete the hit). You can thus change the replacement text anytime, within the same Isearch invocation.
After replacing the search hit, C-M-RET moves to the next one. So you can just use it repeatedly if you want to replace several successive search hits. Or use C-s to skip replacing the current hit and move to the next one.
On-demand Isearch replacement works also for regexp searching, and just as for query-replacing, the replacement text can be either inserted literally, as is, or interpreted as in query-replace-regexp. In the latter case, you can use \&, \=\N, \#, \, and \?. You can use C-M-` anytime during Isearch to toggle whether replacement text is used literally or interpreted per the special regexp-replacement constructs.
The following packages provide live highlighting and replacement previewing for query replacing, as well as additional features:
https://github.com/syohex/emacs-anzu
https://github.com/benma/visual-regexp.el
https://github.com/benma/visual-regexp-steroids.el
I currently use visual-regexp-steroids.el.
All three packages can be installed from MELPA.

Is there a way to make regions in term modes respect line wrapping?

When using a term mode derivative (like ansi-term or multi-term), I often want to select a region and copy it someplace else. If that region includes a line which wraps at the edge of the terminal window, pasting that region in another buffer always inserts a hard newline at the place where term wrapped the line. This means I often have to go back and clean up pasted text. Is there a way to avoid doing this? I tried both term-line-mode and term-char-mode; both do the same thing.
I do not want to write a yank hook which strips out all newlines, since I want to preserve existing hard newlines in the original content.
This works for me:
(setq term-suppress-hard-newline t)

How to temporary select and edit some lines in a buffer?

I'm stuck on this problem. I have a file with almost 10k lines. Some of them should be deleted and I can get an hint from their content, e.g. if they contains a certain pattern "xyz", maybe they must be deleted. So, my idea is to:
* select only those lines containing each pattern in my list
* manually inspect them and change the ones actually to be deleted prepending them a "--" (or similar)
* ...and at the very end, delete all the lines starting with "--".
As keep-lines actually delete all the un-kept lines (TIL), is there any other way to do what I need? Thanks in advance.
One idea would be to use M-x occur to select possible candidates. Then, use "e" in the Occur buffer to enter "Occur Edit Mode". In this mode, edits in the Occur buffer are reflected into the originating buffer. So maybe you can just delete lines there altogether.
You might like to try M-x all. It's like M-x occur except that buffer changes get propagated to the original buffer. all can be installed from GNU ELPA (i.e. via M-x list-packages).
It sounds like you could do this with query-replace-regexp.
M-C-% (or M-x query-replace-regexp)
Enter ^.*xyz.*$ as your search term and leave the replace term blank (hit enter)
Inspect each matching line in turn, hit space to delete or n to leave. Make sure you start at the beginning of the buffer to catch everything.
You can get hide-lines and use M-x hine-lines-not-matching. Since it's still the same buffer (just displayed differently), you can edit the visible lines as you wish.

Multiline sentence in emacs

If I have a multiline sentence in Emacs it naturally overflows onto the the following lines. Now, if my cursor is at the beginning of such a sentence and I press the DOWN ARROW key, the cursor is placed at the beginning of the next sentence (which might be at 4-5 lines down), rather than on the next line itself (which other editors do). Same is the behavior of the END and HOME keys.
Is there a way in which I can change this behavior and get the cursor on the next line instead of the next sentence?
I haven't yet tried it myself, but I think what you are asking for is the default behavior for emacs 23. What version are you running?
You might want to check out the page Move By Visible Lines page on the emacswiki.
You might want to try auto-fill-mode or longlines-mode. To get either use M-X then type the command you want. Toggle them off the same way.
If that doesn't work you may want to examine the binding that is being applied to your down arrow. Type C-h k then hit the down arrow key.
It sounds as though the text is wrapping, so by definition (a line being a group of characters separated by a carriage return), it is moving down to the next line.
I agree it is a pain, however a lot of other editors also have this behaviour.
One way is to turn off wrapping:
M-x toggle-truncate-lines
You wont be able to see all of the text in the editor, however it will move down to the next line.