Replace text literally without regular expression in Emacs evil-mode - emacs

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

Related

Emacs lisp, How to make a newline in IELM?

Even the basic things seem to be hidden behind weird shortcut-combinations.
How can I (hopefully without too many c-m-x-c combinations) create a simple new line?
return evaluates the current line
With C-j. That said, if you have paredit enabled, it should do all the newlines you need automatically.
In my case it was M-j.
I'm currently trying out SpaceMacs, not sure if that has anything to do with it.
Even the basic things seem to be hidden behind weird shortcut-combinations.
If you don't want RET to have contextual behaviour, you can M-x customize-option RET ielm-dynamic-return.
How can I ... create a simple new line?
Only a newline? C-qC-j is the normal way to enter a newline in places where RET does something else. (More generally you can use C-q to insert any character that isn't self-inserting for the current buffer.)
If you want newline + indentation (which is what RET does when contextually appropriate), then M-j will do that (noting that if you are in a comment at the time then the new line will also default to a comment).

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

In Emacs, how can I exclude '-' from the common delimiters?

When coding in elisp, I find that I'm stopping at hyphens when moving by words, and would prefer to ignore them.
What's the simplest way to do this?
M-x modify-syntax-entry RET - RET w RET should do it. Or if you prefer an elisp snippet that you can add into a hook, (modify-syntax-entry ?- "w")
The syntax table for a mode contains information on what constitutes various syntactic classes (e.g. words, spaces etc.). These are used to determine the operation of commands such as forward-word etc. Modifying it change the behaviour of these commands.
Instead of changing Emacs' notion of words, it might be preferably to navigate by s-expressions (C-M-f, C-M-b) to skip whole identifiers. That way, you keep the convenience to be able to navigate by the partial words if you want to change an identifier.
You can use interactive regex search. Pressing just C-M-s SPACE should search for any whitespace (you might need to configure search-whitespace-regexp).

Modifying emacs forward-word / backward-ward behavior (to be like in vi/vim)

What would be the easiest way to have the same kind of behavior that is in vim for the word back and forth navigation? In vim when you press "w" it moves a cursor forward one word, where word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, eol). In emacs on the other hand it skips until the end of the next word and the word is defined per mode in the syntax table.
For example: having a cursor at the beginning of the line following shows where vim put a cursor when you do forward-word ("w") operation:
opt1.arg = opt2.arg
^ ^^ ^ ^ ^^ ^
In emacs it is like:
opt1.arg = opt2.arg
^ ^ ^ ^ ^
It really depends on a one's preference, but I tend to like the vim style better and I was wondering what is the easiest way to have the same in emacs. I guess I'm not alone who switched from vim to emacs so perhaps someone already has a solution, ideally for the kill-word and backward-kill-word as well :)
I know you can get something similar by combination of M-f, M-b etc., but that is not the point. I also don't want to start a discussion which approach is better - the topis is well discussed in here.
You can actually use 'viper-forward-word
(require 'viper)
(global-set-key (kbd "M-f") 'viper-forward-word)
(global-set-key (kbd "M-b") 'viper-backward-word)
Mostly a duplicate of this, which says:
(require 'misc)
Then bind whatever keys you want to forward-to-word and backward-to-word. For killing, create some simple functions that wrap these functions and do kill.
I don't know why jpkotta's answer was deleted, but here it is again:
I have a minor mode that changes word-based commands to operate on syntax changes (and also CamelCaseSubwords). It may be a bit too fine-grained for some tastes, but I find I basically ever use single character movement anymore.
https://bitbucket.org/jpkotta/syntax-subword
# mods, I don't know why this answer would be deleted, so if you choose to delete this answer too, I'd appreciate an explanation.

gnu screen - changing the default escape command key to ALT-X?

In GNU screen, I want to change the default command binding to Alt-s (by tweaking .screenrc) instead of the default C-a, the reason is I use emacs hence GNU screen binds the C-a key, sending "C-a" to the emacs becomes tedious (as #Nils said, to send "C-a" I should type "C-a a"), as well as "C-a" in bash shell, and I could change the escape to C- but some of them are already mapped in emacs and other combinations are not as easy as ALT-s . If anyone has already done a ALT key mapping, please do let me know.
It is possible to work around :escape command limitations using registers and :bindkey command. Just put this in .screenrc:
# reset escape key to the default
escape ^Aa
# auxiliary register
register S ^A
# Alt + x produces ^A and acts as an escape key
bindkey "^[x" process S
## Alt + space produces ^A and acts as an escape key
# bindkey "^[ " process S
See http://adb.cba.pl/gnu-screen-tips-page-my.html#howto-alt-key-as-escape
From my reading of man screen it seems like the only meta character that screen can use for the command binding is CTRL:
escape xy
Set the command character to x and the character generating a literal
command character (by triggering the "meta" command) to y (similar to
the -e option). Each argument is either a single character, a two-character
sequence of the form "^x" (meaning "C-x"), a backslash followed by an octal
number (specifying the ASCII code of the character), or a backslash followed
by a second character, such as "\^" or "\\". The default is "^Aa".
If there is some mapping that you don't use in emacs, even if it's inconvenient, like C-|, then you could use your terminal input manager to remap ALT-X to that, letting you use the ALT binding instead. That would be a little hackish though.
I'm an Emacs and screen user as well. Although I rarely use Emacs in a terminal -- and as such in a screen session -- I didn't want to give up C-a for the shell either (which uses Emacs key bindings). My solution was to use C-j as the prefix key for screen, which I was willing to sacrifice. In Emacs programming modes it is bound to (newline-and-indent) which I bound to RET as well, so I really don't miss it.
By the way: I know this is an advise rather than an answer, but I felt this would be valuable enough to post nevertheless.
To make Alt+X the default prefix for commands and free C-a, add the following lines to .screenrc:
escape ^||
bindkey "^[x" command
As a side effect C-| will be command prefix too. If you need this keys to be free too, then fix "escape ^||" accordingly.
Screen doesn't have any shorthand syntax for alt bindings, but you can give it the octal code directly. For instance on my machine, Alt-x has the hex code F8, or 370 octal, so putting
escape \370x
in my screenrc changed the escape code to alt-X
Tested and works with screen 4.00.03 on Linux.
You may have to change the escape, since I think this may depend on things like your language and codeset, etc: how I found out what my escape code was was to type
$ echo -n ^QM-x | perl -ne 'printf "%lo\n", ord($_)'
^Q is the quoted-insert command for readline (it inserts what you type directly without trying to interpret it) and M-x was a literal Alt-X.
Fellow emacs user here.
The best solution I've found is a ~/.screenrc file with the following:
# C-a :source .screenrc
escape ^gg
Live updated here: https://gist.github.com/1058111
See also: http://ubuntuforums.org/showthread.php?t=498675
Something I have had for years in my .screenrc:
escape ^Zz
which is now hardwired in muscle memory for me.
Somehow I ended up having to share a screen with someone else's config, and now I keep stopping processes all the time (bash ^Z)... Not funny...