I wrote a simple function, that makes emacs add matching quotes (so when I type ", I get "{cursor}"). But now, it's a major annoyance when I delete the first quote but the second is still remaining.
Is there a way to "listen" for character deletion events, and it the next character is matching the deleted one, delete the next character as well?
There are existing minor modes for this. See AutoPairs at the EmacsWiki.
autopair.el has the specific functionality you're describing.
(electric-pair-mode in Emacs 24 does not, it would seem).
Related Q&As (mentioning autopair):
Intelligent auto closing matching characters
Is it possible to auto-complete parentheses or quotation marks in emacs?
How do I get TextMate style quotes in Emacs?
Automatically closing braces in Emacs?
Related
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.
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).
To quote emacs help, M-/ is:
(dabbrev-expand ARG)
Expand previous word "dynamically".
Expands to the most recent, preceding
word for which this is a prefix. If no
suitable preceding word is found,
words following point are considered.
If still no suitable word is found,
then look in the buffers accepted by
the function pointed out by variable
`dabbrev-friend-buffer-function'.
In other words, if somewhere in program there is a variable called reallyLongVariableName and I type reaM-/ then emacs automatically fills in the rest. If more than one match exists (ex, reallyUglyVariableName) then M-/ cycles thru them.
I believe it exists since eclipse3.1 and was tracked by bug 11668
("Add emacs-style "Alt-/" hippie auto completion").
Alt+/
Text editors now support word completion.
In text editors you can complete a prefix to a word occurring in any of the currently open editors or buffers.
The default key binding for word completion is Alt+/ (Ctrl+. on the Mac).
Note: if you repeat the Alt+/ key combination, it will cycle amongst all the word beginning with the current prefix, starting with the closest one from the current edit position.
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 textmate, when there's a current selection, I hit the " key and the selection gets surrounded by quotes. The same thing happens with other balanced characters like (, {, [ and '.
Am I missing something obvious in Emacs configuration that would enable similar behaviour when using transient mark mode, or do I need to break out elisp and write something?
wrap-region.el from this guy's blog post will do what you're looking for.
Paredit will complete the TextMate-style quoting. When you type one part of a matched pair (quotes, brackets, parentheses, etc), the second will be inserted and the insertion point is moved between them, much like TextMate.
Try http://autopair.googlecode.com
You should check out these older, very similar, questions:
Automatically closing braces in Emacs?
Emacs typeover skeleton-pair-insert-maybe
Although the correct answer is Joao's above; I'm about to go and change my answer to those questions, to point to autopair.