I use Emacs in my Mac OS Terminal. It appears that when I type open (shift+9) or close (shift+0) parenthesis, besides the expected display of parenthesis, it also unexpectedly insert many spaces. I've included the effects before and after I type parenthesis as below:
before typing parenthesis
after typing parenthesis
I'm a newbie to Emacs. So, I'm unsure what the bug is about. Could anyone give me any ideas of how should I proceed to fix this bug? Thanks a lot, guys!
The "ObjC" in the mode-line means that Emacs decided to use the Object-C mode for that buffer. This is likely because the .m file extension is normally the extension used for Objective-C files.
The Objective-C mode by default reindents the current line for you when you hit some punctuation characters like (, {, ; and a few others. You can disable it, of course, but in your case that'd be working around the real problem which is that your file is not written in Objective-C mode, so you should use another mode, which will not only solve this immediate problem, but will give you many other benefits.
I'm not sure what language you're using but I think all you need is one of (or a combination of):
install the Elisp package which provides support for your language (e.g. mathematica-mode)
change the extension of your files to that expected by Emacs for that language.
change auto-mode-alist to tell Emacs which mode to use for files ending in .m, e.g.:
(add-to-list 'auto-mode-alist '("\\.m\\'" . octave-mode))
Related
Just FYI, I am new to the .emacs file.
I would lik to set up my .emacs file to auto-indent and auto-pair a certain way to make writing code a little faster. I have found some info as to how to do these things independently but I'm not sure how to put it all together for the emacs version that I have. Ultimately, I would like to set up these definitions specific to which ever language I am coding in. Just to get me started I will use java as an example.
Obviously auto-pairing for ", (, ' are pretty straigforward. I would just like it to auto insert a closing ", ), ' and place the cursor in the middle.
For {, I would like it auto insert two newlines and the closing } whith the cursor in the middle.
Example
while (true) {
<--- cursor would be here with auto-indent of 2 spaces
}
I would also like this to work for nested curly braces which the appropriate indentation.
Example
while(true) {
if (...) {
}
}
Here is what I have so far in my .emacs file:
(defun java-autoindent ()
(when (and (eq major-mode 'java-mode) (looking-back "[{;]"))
(newline-and-indent)))
(add-hook 'post-self-insert-hook 'java-autoindent)
Obviously this just inserts a line and auto indents, but I also want the closed } to be included on the line below. I also tried using electric-pair but that didn't work.
My wish list may be a little unrealistic. I'm not even sure that this is possible, but I would be happy with the closest that I could get.
Any help to get me going in the right direction would be greatly appreciated.
Emacs defines modes for each type of language you code in. Some modes are derived from others and there is a mode called prog-mode which most programming modes are derived from.
The mode for a language is where things like indentation are defined because these tend to be language specific. The rules for indentation can be quite complicated, which is why people often use a mode with similar indentation style as the parent and derive a new mode from that.
Have a look at modes and derived mode in the emacs elisp manual.
With respect to adding matching/closing delimiters, have a look at electric-pair-mode (I think it was in emacs 24.4 - I'm running 25 and forget when it was introduced).
With respect to your requirement to enter some code, some newlines and position the cursor in a specific place, you probably want to look at one of emacs' template solutions. yasnippet is a popular choice and it is easy to define new templates in it. There are also many existing packaged yasnippet templates you can download/install. If you don't like yasnippet, google emacs template and have a look there are quitre a few frameworks.
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.
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.
I have tried emacs on and off for a while now and every time I start emacs, I go through the same routine. Customizing. The first one is binding return to newline-and-indent. (g)Vim does this by default. Showing matching parenthesis is also done by default on (g)Vim. It is grea that I can customize emacs to my heart's content but why doesn't emacs have nice and easy defaults? For reference, I am now using Emacs 23 on a RHEL5 box.
Probably because RMS didn't want it, that and because changing long-standing defaults is just an issue of politics. Like vi, Emacs has a hard-core following and basic changes like these are minefields.
Note: if you saved your customizations, then you wouldn't have to re-do them every time...
To have those nice and easy defaults, install Emacs Starter Kit. It enables by default a bunch of useful and convenient features make even the advanced Emacs users more productive.
Otherwise, as TJ pointed out, Emacs Customization Mode (type M-x customize) allows you to save permanently any of the settings. You can even store them in a separate file from your dotemacs―(setq custom-file "~/.emacs-custom.el")―so you can use it in every computer you work on.
The title of your question doesn't really reflect what your question is (and has been answered by Trey and Torok), but I'll tell you why I like it being bound to just newline: useless whitespace. Say you are nested inside a conditional in a function etc. and hit return a couple times to leave a blank line. The blank line now has a bunch of space chars on it. Yes, you can (and I do) remove trailing whitespace before saving, but I also have visual whitespace mode on and I can see it there taunting me.
Emacs reindents the current line whenever I type certain things, like ";" or "//". This is pretty annoying, since there are a whole lot of places where it isn't smart enough to indent correctly.
How do I disable this feature? I still want to be able to indent the line with TAB, but I don't want any source code I type to cause it to reindent.
(I'm using Dylan Moonfire's C# mode, but this probably applies to any cc-mode.)
Try running c-toggle-electric-state to turn off the electric action of these characters.
You can do this as part of a c-mode-common-hook, or toggle the state manually by hitting C-c C-l.
most likely caused by the inline-and-indent 'feature' of c-mode and derivatives. emacswiki has several solutions.