I'm using the most recent version of Emacs on Windows 7. Let's say I type the following code in my .emacs:
;test|
| means the cursor position. Now if I press Enter, the text will be moved to the right and it will look like:
;test
How to disable this feature?
This is done in accordance with the Emacs Lisp style guide:
Comments that start with a single semicolon, ';', should all be aligned to the same column on the right of the source code. Such comments usually explain how the code on that line does its job. For example:
(setq base-version-list ; There was a base
(assoc (substring fn 0 start-vn) ; version to which
file-version-assoc-list)) ; this looks like
; a subversion.
If you use two or more semicolons you will see other behaviour:
Comments that start with two semicolons, ';;', should be aligned to the same level of indentation as the code. Such comments usually describe the purpose of the following lines or the state of the program at that point.
...
Comments that start with three semicolons, ';;;', should start at the left margin. We use them for comments which should be considered a “heading” by Outline minor mode.
...
Comments that start with four semicolons, ';;;;', should be aligned to the left margin and are used for headings of major sections of a program.
The automatic indentation is done by electric-indent-mode. If you wish to disable it entirely, put something like
(electric-indent-mode -1)
in your init file. You could also disable it for specific modes using something like
(electric-indent-local-mode -1)
in the appropriate init hooks.
Simply using two semi-colons as suggested by the style guide should also prevent the behaviour, which will let you benefit from electric-indent-mode on other code.
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.
I have a file open called test.scss, and when I press RET on a line, emacs will add 2 spaces to the current line and 4 extra spaces to the next line.
I've highlighted what the file looks like with whitespace-mode.
before pressing RET
after pressing RET
You can see that the .my-element row was auto-indented by 2 spaces, and the new line is indented by 4 spaces too many.
I want the output to look like this instead
desired output
What can I do to make emacs produce my desired output?
Here is the output of describe-mode:
Enabled minor modes: Auto-Composition Auto-Compression Auto-Encryption
Electric-Indent File-Name-Shadow Font-Lock Global-Eldoc
Global-Font-Lock Line-Number Menu-Bar Tooltip Whitespace
(Information about these minor modes follows the major mode info.)
SCSS mode defined in `css-mode.el':
Major mode to edit "Sassy CSS" files.
In addition to any hooks its parent mode `css-mode' might have run,
this mode runs the hook `scss-mode-hook', as the final step
during initialization.
Although in this case I'm in scss-mode, I see similar behavior with most of the other modes I use, such as ruby-mode, sgml-mode, js.el mode and others. I'd like to make the behavior match the desired output shown above.
Each mode can handle indentation in its own way, and you may have to look for mode-specific settings to get two-space indentation working everywhere.
For starters, you can set css-indent-offset, which should cover css-mode and scss-mode:
(setq css-indent-offset 2)
You can set the basic indenting of many other modes similarly. ruby-mode seems to use ruby-indent-level, sgml-mode uses sgml-basic-offset, and js-mode uses js-indent-level.
I decided that I was ready to try something new, after a few years of using gEdit for most of my coding needs, and try to learn using Emacs. I knew this would be difficult, as I have heard how complex Emacs can be, but I was lured by its power. The hardest thing has been getting used to writing ELisp in the .emacs file to change things about the editor. I can't currently do it myself, but I have found a few helpful snippets here and there to change some options.
One thing I have been having a lot of problems with is getting Emacs to remember the text I have selected after a command. For instance, I commonly highlight a section of code to mass indent it. However, if I do this in Emacs, it will move the selected text only once before unselecting all of the text. Does anyone know a way around this?
Anyway, I apologize for what seems to me to be an easy question, but after an hour of Google searching and looking around here on SO, I thought it was worth asking. I have a few more questions about Emacs, but I will save them and ask separately after I get this straightened out. Thanks!
UPDATE
A few people have asked about what mod I am using and what type of text I am entering. While I don't know much about Emacs modes, I am editing a pure text file at the moment. Something like this:
Hello, I am a simple text file
that is made up of three separate
lines.
If I highlight all three lines and hit TAB, I get this:
Hello, I am a simple text file
that is made up of three separate
lines.
This is great, however, if I use C-x C-x like some suggest below to reselect the text and hit TAB again, I get this:
Hello, I am a simple text file
that is made up of three separate
lines.
I hope this helps!
FWIW, here is the reason for the behaviour of your newly-added example. (I'm not 'solving' the issue here, but I'm posting it to demystify what you're seeing.)
This was determined with emacs -q which disables my customisations, so the following is default behaviour for emacs 23.2.
You are in text-mode. You should see (Text) or similar in the mode line at the bottom of the screen, and C-h m will tell you (under the list of minor modes) "Text mode: Major mode for editing text written for humans to read." Emacs decides (by way of the auto-mode-alist variable) that it should switch to text-mode if you visit a filename matching certain extensions (such as .txt).
In text-mode pressing TAB with a region highlighted causes indent-according-to-mode to be called on each line of the region in sequence. The slightly convoluted path to finding this out starts at C-h k TAB, which tells us that TAB is bound to indent-for-tab-command, which in this instance calls indent-region -- that function name is not stated explicitly in the help, but can be seen in the code -- which checks the buffer-local indent-region-function variable, which is nil, and: "A value of nil means really run indent-according-to-mode on each line."
indent-according-to-mode checks the indent-line-function variable, which has the buffer-local value indent-relative.
Use C-h f indent-relative RET to see the help for this function. (Read this).
Although you probably won't yet have had the experience to know how to check all that (or necessarily even want to!), and fully understand everything it tells you, this is an example of how the self-documenting aspect of Emacs enables a user to figure out what is going on (which then makes it feasible to change things). I essentially just used C-h k (describe-key), C-h f (describe-function), and C-h v (describe-variable) to follow the documentation. Looking at the source code for indent-for-tab-command was as simple as clicking the file name shown as part of its help page.
I suggest doing the following to help see what is happening when indent-relative runs on each line:
M-x set-variable x-stretch-cursor t
M-x set-variable ruler-mode-show-tab-stops t
M-x ruler-mode
Now for each line in turn, put the cursor at the very start of the line and press TAB. You'll end up with all three lines indented to the first tab-stop ('T' in the ruler).
Now repeat this -- again, ensure you are at the very start of each line, in front of the existing indentation.
The first character of the first line (which is currently a tab) is once again indented to the first tab-stop, as there is no preceding line for it to examine.
Next, the first character of the second line is indented to match the position of the first non-white-space character of the preceding line. Because the first character of the second line is also a tab, the actual text of the second line is pushed one tab further along.
The third line follows suit. Its first tab character is lined up with the first non-white-space character of the second line, with the same relative effect as before, giving you the final state in your example.
To emphasise, note what happens if you now put enter the line "a b c" above the existing lines, then move back to the start of the next line (what was previously the first line) and press TAB. The first tab character will now be indented in line with the 'b'. Provided that the indent-tabs-mode variable is true (meaning you have actual tab characters), then this will have no practical effect on the position of the words in the line, as 'indenting' a tab with spaces will not have an effect until the number of spaces exceeds the width of the tab (but that's another kettle of fish entirely!)
All this really means is that text-mode in Emacs doesn't behave the way you'd like it to in this situation. Other major modes can do completely different things when you press TAB, of course.
As is invariably the case with Emacs, things you don't like can be changed or circumvented with elisp. Some searching (especially at the Emacs Wiki) will frequently turn up useful solutions to problems you encounter.
Try typing C-x C-x after Emacs unselects it.
Then, instead of hitting tab (I never knew that tab does what you said! That's totally whacked.), do M-8 C-x C-i. Pity it's so many keys, but it ought to do what you want -- namely, shove everything over 8 columns. Obviously replace the M-8 with something else if you want some other number of columns.
What I usually do is simply type C-x C-x (exchange-point-and-mark) after a command that deactives the region.
How are you indenting, and in which mode?
The indentation rules in any programming mode should generally just get it right. (If they don't, that's probably more indicative that you want to configure the rules for that mode differently, but I suspect that's a different question which has been asked already).
If you're in text-mode or similar and just using TAB, then I can see the problem.
Note that if you're using indent-rigidly (C-x C-i, or C-x TAB which is the same thing) then you can repeatedly indent the same region simply by repeating the command, even if the highlighting has disappeared from view.
You can also use a prefix arg to indent-rigidly to make it indent many times. e.g. C-u C-u C-x C-i (easier to type than it looks) will indent 16 spaces (4 x 4, as the prefix arg defaults to 4, and it multiplies on each repeat). Similarly, M-8 C-x C-i indents 8 spaces. This is fine in some circumstances, and way too cumbersome in others.
Personally I suggest putting (cua-selection-mode 1) into your .emacs and using that for rigid indentation. Trey Jackson made a handy blog about it. With this, you can C-RET to start rectangle selection, down as many lines as you need, TAB repeatedly to indent the lines, and C-RET to exit the mode.
While the rectangle is active, RET cycles through the corners. For left-hand corners, typing inserts in front. For right-hand corners, typing inserts after. For the single-column rectangle, bottom counts as 'left' and top counts as 'right' for this purpose.
Trey's blog lists all the available features (or look in the source file: cua-base.el)
Be warned that indentation in Emacs is generally an unexpectedly complicated topic.
You can do this with something like the following:
(add-hook 'text-mode-hook (lambda ()
(set (make-local-variable 'indent-region-function)
(lambda (s e)
(indent-rigidly s e tab-width)))))
Then selecting a region and hitting TAB. will indent the region by a tab-width. You can then exchange point and mark with C-x C-x and hit TAB again to repeat.
I do, however, agree with the previous answers that suggest using indent-rigidly directly.
I am trying to diagnose this problem. TAB creates 4 spaces instead of a 4 col TAB like I want. But I don't think it should because C-h v indent-tabs-mode on the buffer in question says it is set to t. When I check my keybindings, TAB is set to c-indent-line-or-region. Does this function ignore my tabs-mode?
Tabs and indentation in Emacs is a considerably more complex subject than most people anticipate. I highly recommend spending some time reading about it -- it will almost certainly save you some confusion in the long run.
The following page at the Emacs Wiki groups together most of the relevant discussion:
http://www.emacswiki.org/emacs/CategoryIndentation
There's quite a lot there, but it's worth looking through.
One or other of the TabsAreEvil and SmartTabs configurations is quite likely to be appealing to you, btw, depending on your personal opinions on the subject!
Make sure you read the page on the tab-stop-list variable. It's tucked away near the bottom of that list of links, but it's critical to understanding the behaviour of tabs in the absence of automated-indentation rules, along with things like 'tabify'.
ruler-mode is useful here as well. I enable it automatically with text-mode:
;; Use ruler in text-mode
(add-hook 'text-mode-hook
(function (lambda ()
(setq ruler-mode-show-tab-stops t)
(ruler-mode 1))))
I figured out the problem. It was inserting a tab character after all. It turns out I thought it wasn't because when I hit backspace that key is bound to c-electric-backspace, which looks at the variable c-backspace-function which was set to backward-delete-char-untabify, which IMO defeats the purpose of having tabs.
Check tab-width variable. If it is 8 (the default), then Emacs of course has to insert four spaces since a tab would be "too much".
Check of the file for Emacs "File Local Variables". These specially formatted lines can override your settings when that file is loaded.
Here is an example from the bottom of a bit of Ruby code, forcing indent to 2 spaces, and tabs converted to spaces:
# Local Variables:
# tab-width: 2
# ruby-indent-level: 2
# indent-tabs-mode: nil
# End:
Be sure to take a look at the first line of the file as well. If you see something like
// -- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil --
This line will override any global or mode settings.
Is there an Emacs minor-mode (or piece of elisp code) that lets you selectively hide/show environments while in LaTeX mode? For instance, I would like to move to the beginning of a long \begin{figure} block, hit a keystroke, and have the contents of that figure environment hidden from view. Similarly with \begin{proof} and so on, and ideally even with \subsections.
Is this possible? I just tried hs-minor-mode, allout-mode, and outline-minor-mode, but most of them don't recognize LaTeX's environments, e.g. hs-minor-mode fails with "scan error: unbalanced parentheses". I would prefer not to have to enter explicit folding marks like {{{ as in folding-mode.
[Ideally it would be great if the folding were persistent, but I see that that question doesn't have an accepted answer yet.]
AUCTeX does folding: http://www.gnu.org/software/auctex/manual/auctex.html#Folding
A popular complaint about markup languages like TeX and LaTeX is that there is too much clutter in the source text and that one cannot focus well on the content. There are macros where you are only interested in the content they are enclosing, like font specifiers where the content might already be fontified in a special way by font locking. Or macros the content of which you only want to see when actually editing it, like footnotes or citations. Similarly you might find certain environments or comments distracting when trying to concentrate on the body of your document.
With AUCTeX’s folding functionality you can collapse those items and replace them by a fixed string, the content of one of their arguments, or a mixture of both. If you want to make the original text visible again in order to view or edit it, move point sideways onto the placeholder (also called display string) or left-click with the mouse pointer on it. (The latter is currently only supported on Emacs.) The macro or environment will unfold automatically, stay open as long as point is inside of it and collapse again once you move point out of it. (Note that folding of environments currently does not work in every AUCTeX mode.)
In order to use this feature, you have to activate TeX-fold-mode which will activate the auto-reveal feature and the necessary commands to hide and show macros and environments. You can activate the mode in a certain buffer by typing the command M-x TeX-fold-mode RET or using the keyboard shortcut C-c C-o C-f. If you want to use it every time you edit a LaTeX document, add it to a hook:
(add-hook 'LaTeX-mode-hook (lambda ()
(TeX-fold-mode 1)))
If it should be activated in all AUCTeX modes, use TeX-mode-hook instead of LaTeX-mode-hook.
Once the mode is active there are several commands available to hide and show macros, environments and comments...