Replace x/y/z to x&z with Emacs? - emacs

I have a large file like this:
x/y/z
x2/y2/z2
...
How can I use Emacs's replace to change them to
x&z
x2&z2
so the '/y/' part is changed to '&'?

Assuming that you need to replace /y/ or /y1/ or /xxx/ to &, you can use replace-regexp.
M-x replace-regexp <RET> /.*/ <RET> & <RET>
This will replace the strings including slashes of both sides to &.
You can also use query-replace-regexp to replace the matched strings interactively.

Related

Replace text literally without regular expression in Emacs evil-mode

In vim, I could use :%sno/[abt]//g to remove all text of "[abt]" literally (as explained here).
I tried the same command in evil-mode, but it complains it doesn't understand the sno command, so how can I do the same thing in evil-mode?
To my knowledge, evil does not (yet?) support the "magic/no magic" regexp options (actually, it only does a smallish subset of ex functionality, so I don't think % will work either). As #Ehvince's answer suggests, the standard Emacs way to do the replace is with query-replace or query-replace-regexp. If you'd like to stick to evil, just escape the square brackets with a backslash:
:s/\[abt\]//g
NB: backslash escapes in Emacs often bite people coming from other environments or programming languages; have a look at the bottom of this manual node on the backslash for more information.
You would use the emacs command query-replace bound to M-%:
M-% [abt] RET <nothing> RET
and then approve each occurence with y or all with !.
The doc is at C-h f query-replace.
query-replace-regexp is bound to C-M-%.

how to replace extract word

all
I am new to emacs, I have a simple problem, when searching, emacs provides search by word, but when replace, how can I just replace the extract word, not a substring? I have searched on the internet, someone said to add \bfoo\b to match foo only, won't match foos but it doesn't work. Thanks.
For string-based search/replace, can run query-replace-word by typing: C-u M-%.
If you want regular expression search, then, indeed, you can enclose your regexp in either \b which matches word boundaries, or even \< and \> for beginning and end of word respectively. Make sure to use M-x replace-regexp in that case, not just M-x query-replace.

How to replace '_' by space in emacs

I have a text file which I am only able to look that there is an underscore between some words only using emcas editor but not other editors such as vi. I do not know how to use emacs but I wanted to replace these underscores "_" by space in the emacs editor automated fashion. How can I do that ?
I believe that those underscore aren't really underscore, but non breaking space (U+00A0 unicode char), that Emacs show as underscore with a different color. You probably don't need to replace them, but if this is really needed, just use M-x replace-string and kill and yank one of those non-breaking space in the string to be replaced.
Hit the M-x key-combination (that is, hold meta key - alt on windows - and hit x) type replace-string and hit enter. You can then type [underscore] enter [space] enter.
In Emacs notation:
M-x replace-string RET _ RET " "
Should the previous answer not solve it: Remember that as a coding system error. Check with C-x = if it's char 95.
If not, check variables coding-system-for-read, coding-system-for-write, buffer-file-coding-system
Finally, get emacs core developers at help-gnu-emacs#gnu.org

Extended command by regexp?

In Emacs - is there a way I can search an extended command by regex right in the minibuffer? That is - I want to filter commands by regexp and then choose the one I need using IDO or Icicles.
When using ido you can turn on regexp matching by pressing C-t. I use smex and ido together and it works beautifully.
While they are not regexps, the default completion mechanism accepts a * to stand for "anything", so you can do M-x foo*bar ? and it will list all the commands whose name looks like "...foo...bar...".
I've always suspected that Icicles provdies that.
M-x <command-regexp> [PageUp/PageDown]
to browse the commands mathching regexp, and
M-x <command-regexp> [Shift-Tab]
to see the list of commands matching regexp. More here.

How to replace space character with line break(LF) in emacs?

I want to use "replace-string" command to achieve this function,
but how to type in "LF" character?
it's confuse me so much...
You can use C-q C-j to search/replace a newline