I am trying to replace a character - say ; - with a new line using replace-string and/or replace-regexp in Emacs.
I have tried the following commands:
M-x replace-string RET ; RET \n
This will replace ; with two characters: \n.
M-x replace-regex RET ; RET \n
This results in the following error (shown in the minibuffer):
Invalid use of `' in replacement text.
What's wrong with using replace-string for this task? Is there another way to do it?
M-x replace-string RET ; RET C-q C-j.
C-q for quoted-insert,
C-j is a newline.
There are four ways I've found to put a newline into the minibuffer.
C-o
C-q C-j
C-q 12 (12 is the octal value of newline)
C-x o to the main window, kill a newline with C-k, then C-x o back to the minibuffer, yank it with C-y
Don't forget that you can always cut and paste into the minibuffer.
So you can just copy a newline character (or any string) from your buffer, then yank it when prompted for the replacement text.
More explicitly:
To replace the semicolon character (;) with a newline, follow these exact steps.
locate the cursor at the upper left of buffer the containing text you want to change
Type m-x replace-string and hit Return
The mini-buffer will display something like this:
Replace string (default ^ -> ):
Type in the character you want to replace. In this case, ;
and hit Return
The mini-buffer will display something like this:
string ; with:
Now execute C-q C-j
All instances of semicolon will be replaced a newline (from the cursor location to the end of the buffer will now appear)
There is a bit more to it than the original explanation says.
Switch to text mode:
M-x text-mode
Highlight the block to indent.
Indent: Ctrl + M </kbd>
Switch back to whatever mode...
Inline just:
C-M-S-% (if the binding keys are still the default) and then
replace-string ^J.
Related
I tried to follow this to replace a character with newline in Spacemacs but that key combination replaced it with a space instead of newline.
How to replace a character with a newline in Emacs?
How can this be accomplished in Spacemacs?
It is supposed to work exactly the same with Spacemacs (C-q C-j will replace with a newline).
You can check if your keys are correctly mapped with the describe-key command (C-h k):
C-q runs the command quoted-insert
and
C-j runs the command electric-newline-and-maybe-indent (result with my Spacemacs)
I have a block of code like this:
ec2_shell_exec(tag: "ls /
ec2_shell_exec(tag: "sudo yum install git-core
ec2_shell_exec(tag: "pwd
Whats the easiest way to add a ") to this block of code?
Please note, emacs runs in terminal mode aka -nw mode.
Interactively
C-M-% $ RET ") RET
don't forget to press 4 keys at the same time: Ctrl for C, Alt for M, and Shift-5 for %
if the above still does not work (e.g., you are in a terminal with emacs -nw), you can do M-x query-replace-regexp RET $ RET ") RET
Programmatically
(while (not (eobp)) ; you have to edit the condition!
(goto-char (line-end-position))
(insert "\")")
(forward-line))
A keyboard macro should be easy to type on most terminals:
Move to your first line
Start recording a keyboard macro with C-x (
Go to end of line with C-e
Type ")
Move down with C-n
Stop recording macro with C-x )
Type C-x e to replay once
Type e for each successive time you need to repeat it
Summarizing the two solutions here (for folks running emacs in terminal):
M-x query-replace-regexp RET $ RET ") RET
And
first highlight ec2_shell_exec(tag. And call mc/mark-all-like-this which is from multiple-cursor. You will see all ec2_shell_exec(tag being hightlighted. Now call end-of-line to move every cursor to the end. Finally, you can insert anything you want. Press C-g to end operation
video demo: https://www.youtube.com/watch?v=jNa3axo40qM
Thanks adobe and tom!
When using query-replace (with or without regexp) in emacs, the previous query-replace pair is remembered and suggested as default the next time query-replace is invoked. But I would like to be able to edit this default replacement to something similar without having to type the entire new variant.
Like this:
in a section of a long document I do a query-replace
M-% antidisestablishmentarianism-A [return] antidisestablismentarianism-B
later on in the same document I want to do
M-% antidisestablishmentarianism-A [return] antidisestablismentarianism-C
The command M-% on its own gives
Query-replace (default antidisestablishmentarianism-A -> antidisestablismentarianism-B):
Is there some magic key combination which makes it possible to change that final "B" to a "C" without retyping?
Yah, try M-p, something like this sequence
M-% M-p [return] M-p [DEL] C [return]
I can also use C-r. It shows me all the entries in minibuffer and I can select from that. I have C-r binded to M-x anything-minibuffer-history. After M-x query-replace, hit C-h b and search for major-mode in the Help buffer. That will give full list of command. Here is my bindings.
C-g abort-recursive-edit
TAB self-insert-command
C-j exit-minibuffer
RET exit-minibuffer
C-r anything-minibuffer-history
ESC Prefix Command
C-tab file-cache-minibuffer-complete
down next-history-element
next next-history-element
prior previous-history-element
up previous-history-element
M-n next-history-element
M-p previous-history-element
M-r previous-matching-history-element
M-s next-matching-history-element
I have a list of lines like this:
a+
b+
c+
d+
e+
f+
... you get the idea...
I want to end up with a+b+c+d+e etc
I was trying with emacs but couldn't work out how to do such a thing. anyone any ideas?
One thing that does work is
c-m-% [paste in selected after + on one line to beginning of next row] [nothing]
There must be something to insert for carriage return?
How about simply replacing EOLs by nothing?
M-%C-q C-jRETRET
Explanation:
M-% : query-replace
C-q : quote the following character
C-j : end-of-line character
first RET : validate the search string
second RET : validate the (empty) replacement string
Do you have a buffer with those lines in it? In that case, you could create a simple macro:
F3 ;; record macro
C-e ;; end of line
C-d ;; delete newline
F4 ;; save macro
Then either press F4 repeatedly until you're done, or do C-0 F4 to do it all in one swoop.
Have you tried just `M-q' ? The spacing is different, and it will use several lines if you have many of those thingies, but otherwise, it seems like a funny alternative.
M-x
replace-regexp
RET
C-q C-j
RET
RET
How can I detect CR and/or LF in Emacs? Since I want to i-search CRLF, I prefer simpler way (e.g. no or less regex). Only relevant web page I've found is this but c-q c-j didn't work.
Thanks!
Environment) GNU Emacs 22.3.1 (i386-apple-darwin9.8.0, Carbon Version 1.6.0), but I also want to know how to do this on linux (Ubuntu).
C-s to start i-search, then C-q C-j should work. Control-Q quotes the next character as a literal, and control-J is a literal linefeed ("LF" or newline).
If you're dealing with a file that has carriage returns (CRLF line endings), Emacs will automatically use a DOS buffer file coding system and convert them to just linefeeds when loaded, and reconvert them to CRLFs when saved. If you really want carriage returns in your buffer, run M-x revert-buffer-with-coding-system unix. Then carriage returns will be displayed in your buffer as ^M. In this case, you can search for them with C-s to start i-search, then C-q C-j C-q C-m.