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).
Related
Is there a module to do multiline search in Emacs ?
I have used grep, pt and now ag, and they are great. But sometimes when exploring a codebase you search for things that are on multiple lines (and therefore get nothing), but reducing the search to fewer words yields a lot of results.
Is there a way to get multiline search in Emacs in a whole project ?
Of course there is. There are multiple ways.
You don't say what kind of search you are trying to do: single file? multiple buffers? regexp? Do you need a fixed list of search hits or do you want incremental search?
Here's one simple answer:
In a Dired buffer, mark some files you want to search, then hit A.
Type a regexp to match. Use C-q C-j to enter a newline char to match. Hit RET to enter the regexp.
That searches through the marked files, stopping at each search hit in turn. Use M-, to go to the next hit, etc.
For example, to search for doc strings of variable definitions, search with this regexp or similar:
Search marked files (regexp): (defvar.*
.*"Non-nil
There's a newline char after the first .*, which you enter using C-q C-j.
There are lots of other ways to search in Emacs. The best place to start is the Search and Replace category of Emacs Wiki. There you can find ways to search broken down by main characteristics and described.
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.
I would like to use "Ctrl+'" to replace "\" (i.e., the backslash key). How should I do that? Originally I did it by kbd macro, but it is still different. For example, for the package smartparens, if one types "\{", a "\}" will be automatically typed. However, keyboard macro "ctrl+'"+"{" is not the same as "\{" for this case.
So I am just wondering if there are other ways achieving this? Some global keybinding tool (works for the whole X system) is also ok. But so far I cannot find one that can do such keybinding (two keys for one key).
Thanks!
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.
in gedit it's possible to define so-called "snippets" for simpler input.
For example, there is a snippet while. This means: If you type while -> (-> stands for tab key). And gedit automatically converts it to the following (including correct indentation):
while (condition){
}
In vim (in conjunction with latex-suite) I saw the following: If you type (, vim inserts just a (. If you type ( a second time, vim automatically converts it to \left( \right).
I found abbrev-mode but this mode doesn't place the cursor properly (i.e. between parentheses or inside the while loop).
I managed to create custom emacs keybindings/macros that do just the same (without having to press the tab key), so I know it's possible.
However, is there already and package where you can define such "snippets" without much effort? Or are there even any serious reasons not to use such things?
See yasnippet. It provides snippets for most major languages, and it is easy to add new ones or modify the old ones.
Yes, yasnippet is probably the way to go. But make sure you learn the major mode you're using for your editing - when writing in LaTeX, learn auctex. Major modes can contain functionality that makes some snippets pointless, and do the same thing even better. So instead of using a begin/end-snippet in a LaTeX buffer, try C-c C-e in auctex. Etc :)
Don't forget abbrev-mode.