How do I get TextMate style quotes in Emacs? - emacs

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.

Related

Disable parenthesis matching in emacs

When I'm typing in emacs, in particular when I'm editing a LISP file, emacs automatically matches parentheses for me. This would be fine, except that it doesn't let me delete a parenthesis if it would cause a parenthesis imbalance. I suppose there might be people out there who find this a pleasant feature, but to me it's just terrible. I'm fine with it automatically writing a closing parenthesis for me, but I want to be able to delete a parenthesis when I want. It does something similar for quotes as well. This is really obnoxious! How do I stop it?
Emacs doesn't do that by default, that sounds like ParEdit. You might be able to derive from the installation description how to get rid of it.

New to Emacs. When I type ", \" is automatically inserted

As the title states, I'm relatively new to Emacs. I tried out several starter kits but went with Prelude and changed a lot of things around.
Anyway, I've been getting a good handle on everything...until this morning I was working and I typed double-quotes. Normally Emacs would insert a second double quotes right after ("") due to the auto-completion, but I must have accidentally changed something with a keystroke and now when I type ", I get \"\".
Thoughts?
Thank you.
This seems to be an issue with smartparens which prelude installs by default (see the file prelude-programming.el. This behavior is described in detail on smartparens wiki. To ensure that smartparens is causing problems you can can do C-h k" this would print about the command acutally run when " is pressed, if the command is sp--self-insert-command then the following should work
Paste this (setq sp-autoescape-string-quote nil) to your *scratch* buffer, go to the closing parenthesis and do C-xC-e, this will disable the behavior for current emacs session.
To disable the behavior for all future emacs session, assuming that you are using prelude, you will need to add the following to your personal config (basically some file inside /path/to/prelude/personal/).
(setq sp-autoescape-string-quote nil)
This will disable the auto-escaping of the string quotes, completely. If you like this behavior and do not want to disable it completely you can do what #steckerhalter suggests C-q" will insert just one parenthesis.
If the above does not solve the issue then try providing following info in your question which may help us debug the issue,
1) The list minor modes active (this can be obtained by doing C-hm).
2) Output of C-hk"
Hope that helps
this sounds a lot like smartparens (https://github.com/Fuco1/smartparens) which is included in Prelude. usually when you are inside "" then it will escape the quotes:
"hahah \"\" bah"
if you want to get a normal " inside quotes you have to use C-q " or disable smartparens with M-x smartparens-mode
If, as you say in a comment, " is bound to self-insert-command, then when you type " what happens is that a (single, unescaped) " character is inserted.
However, I suspect you have some mode turned on that does something additional whenever a " char is inserted. You mention automatic insertion of a second ", for example. That kind of behavior comes from a mode such as is provided by library smart-parens or electric-pair.
And you mention Prelude.
To find out what part of your init file (~/.emacs) is causing the behavior you see, bisect your init file recursively (first comment-out half, to see which half is responsible, then 3/4, to see which quarter is responsible,...). Then, if you still have a question about the responsible code, ask here, providing that info.
When you describe your problem here, it is important to be specific: what Emacs version, what mode(s), what libraries have you loaded,... Whatever might be pertinent. But first narrow down the problem by bisecting your init file to find the culprit.

How to remove the matching character?

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?

How can I modify emacs' Search and Replace to perform a more complicated task?

total Emacs noob here. So right now I'm working on a fairly big LaTeX project in Emacs in which there are couple of places where I need to index some words, using the makeidx package. Because I also wanted indexed words to be bold, I created my own command \ind{} which would make the argument go bold and indexed. But right now I'm dissatisifed with this command so I'd like to change every instance of \ind{whatever} in my text by \textbf{whatever}\index{whatever by default}.
The thing is I know exactly what I want :
Go through the text, look for any instance of \ind{ and replace by \textbf{ using search-and-replace
Save the argument of \ind ("whatever" in this case) in memory
Ask me the user what should the argument of \index be. By default (by striking enter), it should be the first argument, but I can also change my mind and enter something different ("whatever by default" in this case). If there's no input (only a space " " for example) stop the program.
Write down \index{, the new argument and }.
Go to next occurance in the text.
But, alas!, I know not how to achieve this, so I need someone's help. If it should take too much time to explain how to do such a thing, would you please send me some tutorial about writing my own functions?
I hope I'm being clear, and thanks for your patience!
This approach seems vaguely unorthodox to me, but it works and seems sufficient for a one-off job...
In the replacement text for replace-regexp and query-replace-regexp (C-M-%), one newer escape sequence is \,(...), where ... can be any Lisp expression. There's a Lisp function read-from-minibuffer which reads arbitrary text typed by the user, with an optional default. Therefore:
C-M-%: Start query-replace-regexp.
\\ind{\([^}]+?\)}: The pattern to search for.
\\textbf{\1}\\index{\,(read-from-minibuffer "index content? " \1)}: The replacement text. The user will be prompted for the text to put in the braces following the \index{} element, using the original text between the braces following the \ind{} element as a default.
Note that when using query-replace-regexp, you'll have to confirm each choice by typing y after each. Use M-x replace-regexp if you want to avoid this step.
Vlad give you the LaTeX answer to your problem. An Emacs solution is the key-macro: start with
C-x (
to define a new macro, then do one step of your change, say:
C-s \ind{
<left>ex
Then copy and paste the argument in the \textbf macro... You have to be careful to move in a way that will be repeatable. Once the standard modification is done, you let the cursor after the whatever by default and end the definition by
C-x )
now C-x e will call the macro you just define, letting your cursor at the correct place to change the part you want to change You can also repeat the e to call the macro several time at once.
Why not just redefine the \ind so that it can get an optional argument?
For example:
\newcommand{\ind}[2][]{%
\def\first{#1}%
\ifx\first\empty
\textbf{#2}\index{#2}%
\else
\textbf{#2}\index{#1}%
\fi
}
This way you can use \ind{whatever} or \ind[whatever-else]{whatever}.

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.