In Vim emulation mode (viper + vimpulse) << and >> are working similarly to Vim's. I would just like them to indent with 2 spaces. How do I do that? (I know, trivial. But useful)
After reading viper-cmd.el code (and testing), I determined that correct answer is:
(setq viper-shift-width 2)
I'm guessing that mode probably uses Emacs' tab-width or c-basic-offset settings. Both can be set with:
(setq tab-width 4)
(setq c-basic-offset 4)
(Of course, use whatever size you want in place of 4.)
In order to have space and not tab, add this to your .emacs :
(setq-default indent-tabs-mode nil)
To have Emacs indent with 2 spaces, do like mipadi told you, but I would use default-tab-width
(setq default-tab-width 2)
Related
I am unable to get emacs version 24.3 to indent two whitespaces when I hit the tab key in fundamental mode. I've read a number of other posts, such as set 4 Space Indent in Emacs. I'm pretty sure this used to work in older emacs versions:
(setq tab-width 2)
(setq indent-tabs-mode nil)
I'm now trying all this:
(setq tab-width 2)
(setq-default tab-width 2)
(setq indent-tabs-mode nil)
(setq-default indent-tabs-mode nil)
(setq indent-line-function 'insert-tab)
(setq tab-stop-list (number-sequence 2 400 2))
Now I find that tab is indenting to go right after the first white space block in the line
this is my first line
second line starts here
I can't figure out how to get it to simple to
this is my first line
second line starts here
Maybe the problem is my configuration? I have installed this new version of emacs in my own user home directory - it is not system wide version of emacs.
Actually, your "I'm now trying all this" config works just fine:
(setq-default tab-width 2)
(setq-default indent-tabs-mode nil)
(setq indent-line-function 'insert-tab)
(setq tab-stop-list (number-sequence 2 400 2))
If you reduce your init file to this, it should work as desired.
You may be clobbering the settings elsewhere in your config?
The first thing to do if you want to change the behavior "when I hit " is to do C-h k <foo>. That will tell you which command is run, and might give you some further hints about how to change its behavior. The behavior of the TAB key depends on the major mode, so making it do what you want depends on the major mode you're using. If you're using fundamental-mode, you'renot using Emacs in the normal way, so I'd recommend you try and fix that first.
I need to be able to set the tab settings for the following file types:
.rb: 2 soft spaces
.css, .html.erb: 4 space tabs
I have tried the following, but none of it seems to alter my default tab settings for each of the file types.
;; js-mode-hook has also been tried
(add-hook 'javascript-mode-hook
'(lambda()
(setq tab-width 4)))
(add-hook 'css-mode-hook
'(lambda()
(setq tab-width 4)))
(add-hook 'html-mode-hook
'(lambda()
(setq tab-width 8)))
I am pretty new to emacs so my knowledge of configuration is pretty low.
In emacs each mode has it's own indentation style. The main command to indent (bound to TAB) is indent-for-tab-command.
This command calls mode specific indentation function found in the variable indent-line-function. So each mode has it's own way of doing it.
For Ruby (for my emacs 2 is a default):
(setq ruby-indent-level 2)
For CSS (again, default is 4 for me):
(setq css-indent-offset 4)
Unfortunately SGML mode (on which HTML mode is based) has a very simple indentation mechanism and apparently the level is not configurable.
See the source code of sgml-calculate-indent function.
I personally find it weird. I am not writing HTML, but you can try to modify the sgml-calculate-indent function yourself :). Learn some lisp.
I am using js2 mode, and it indents perfectly by default. For js you have to search for js-indent-level or something similar.
Cheers.
Theres a number of aspects to how Emacs does indentation. Setting the tab-width only specifics how big a tab is if a literal tab is inserted. If you don't wish to use literal tabs for indentation, then you should first disable their insertion (from the manual
):
Emacs normally uses both tabs and
spaces to indent lines. If you prefer,
all indentation can be made from
spaces only. To request this, set
indent-tabs-mode to nil. This is a
per-buffer variable, so altering the
variable affects only the current
buffer, but there is a default value
which you can change as well.which you can change as well.
However, to specify the indentation levels, you'll also need to set the c-basic-offset value variable as well:
(add-hook 'html-mode-hook
'(lambda()
(setq c-basic-offset 4)
(setq indent-tabs-mode nil))
In your case, you may only need the c-basic-offset but try a few combinations and see what works best.
js-mode uses js-indent-level so put (setq js-indent-level 4) into your ~/.emacs (shouldn't have to be in a hook, even, but if you're wondering, it's js-mode-hook, not javascript-mode-hook).
If setting tab-width doesn't change your indentation level for a certain mode, it's often simplest to just open the source for that mode. I found this variable by doing C-h f js-mode, clicking the link "js.el", then searching for "indent", second hit from the top.
However, if you collaborate a lot with other people, it's often better to put a cookie at the top of the file. I typically do // -*- tab-width: 8 -*- in the file, and then I have stuff like this in my ~/.emacs:
(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'cperl-indent-level 'tab-width)
(defvaralias 'perl-indent-level 'tab-width)
(defvaralias 'js-indent-level 'tab-width)
so that I have less variables to deal with (and don't have to get warnings about the file-local variable being unsafe or whatever if the mode-writer forgot to declare it as safe)
If you are using ELPA's css-mode.el with emacs 23.1.1, you can parametrize the global setting for tab width for CSS files for the tab width by doing the following:
1) Type M-x customize-variable ,
2) Then type css-indent-level,
3) Then after you change the variable to your liking, you do "Save for future sessions".
For HTML and erb: if you are using web-mode (the mode provided by Spacemacs) it can be as simple as:
(setq-default
web-mode-code-indent-offset 2
web-mode-markup-indent-offset 2)
where markup-indent-offset refers to the actual tags and code-indent-offset refers to embedded Ruby in ERB, etc.
I am using emacs 23.1.1 on Ubuntu 10.04. I wish to program in Python with a 2-space indent. emacs looks to have a default mode for python (python.el?).
I put the following in my .emacs:
;; Only spaces, no tabs
(setq indent-tabs-mode nil)
;; Always end a file with a newline
(setq require-final-newline nil)
;; Don't know which of these might work
(setq-default tab-width 2)
(setq-default python-indent 2)
(setq-default py-indent-offset 2)
When I edit a Python file, it uses a 4-space indent. When I try C-h v python-indent it says:
python-indent's value is 4
Local in buffer webpage_cache.py; global value is 2
This variable is safe as a file local variable if its value
satisfies the predicate `integerp'.
Documentation:
Number of columns for a unit of indentation in Python mode.
See also `M-x python-guess-indent'
You can customize this variable.
That is, it is 4, not 2. Grr. I tried customizing the variable and saving, still 4. I tried customize-group indent, still 4.
How do I get emacs to pay attention?
You can put this into your .emacs file:
(add-hook 'python-mode-hook '(lambda ()
(setq python-indent 2)))
The reason why
(setq-default python-indent 2)
does not work may because this variable does not exit when .emacs is loaded. (But I am an emacs newbie. I am not sure about my explanation.)
However, PEP 8 -- Style Guide for Python Code recommends "4 spaces per indentation level" and I find 4 spaces more readable. I actually use this piece of code to force a 4 spaces indentation.
Either in you .emacs file or in a file referenced by your .emacs add:
(setq-default indent-tabs-mode nil)
(setq-default tab-width 2)
You can put in a hook if you want to localize
;; Python Hook
(add-hook 'python-mode-hook
(function (lambda ()
(setq indent-tabs-mode nil
tab-width 2))))
EDIT: I have found the following the following issues can mess with my settings:
setting the variable before the library is loaded
other packages/configs resetting the global variables
For both those issues I have found creating hooks and localizing the variables can help.
I just ran into this problem myself, and I think the help for python-indent contains a big clue that no one else mentioned:
See also `M-x python-guess-indent'
If you don't customize python-guess-indent by setting it to nil, then python.el will automatically set python-indent for each Python buffer (that contains indented text), making your python-indent customization ineffective. (On the other hand, when in Rome...)
In the end, this is what went into my .emacs file (ignoring all other custom variables):
(custom-set-variables
'(python-guess-indent nil)
'(python-indent 2))
The title of my question is a reference to sane tabs in emacs.
Basically what I want is to globally set tabs and indention to work in
some uniform way. I feel like emacs is so much better than TextMate
or BBEdit but really the way they handle indention is simple and great
for my purposes. In emacs if you use some tab/space scheme that's
different than the scheme enforced by a minor mode you use you're in
trouble.
When I press enter I'd like to be moved to the next line indented to
the right place using tabs. If I can have my cake and eat it too I'd
like to be indented using spaces if the rest of the file is composed
that way.
I've tried these also:
doing tabs in emacs
force emacs to use tabs
Thanks to anyone who can help me achieve this.
-Mike
Perhaps (global-set-key (kbd "RET") 'newline-and-indent) is what you want?
(Or reindent-then-newline-and-indent if that's available, or you could just hit C-j instead of the Enter key.)
For this part of your question:
If I can have my cake and eat it too I'd like to be indented using spaces if the rest of
the file is composed that way.
does this do what you want?
(defun dtrt-indent ()
(setq indent-tabs-mode
(save-excursion
(goto-char (point-min))
(search-forward "\t" nil t))))
(add-hook 'text-mode-hook #'dtrt-indent)
(add-hook 'c-mode-hook #'dtrt-indent)
; etc for all modes you care about
So if there's a tab anywhere in the buffer, indent using tabs; if there is no tab, indent using spaces.
if you do your setup as described:
(setq indent-tabs-mode t)
(setq-default indent-tabs-mode t)
(setq tab-width 4) ;; 8 is way too many
(setq default-tab-width 4) ;; 8 is way too many
(global-set-key (kbd "RET") 'newline-and-indent)
The indent-tabs-mode thing will tell emacs to create your indentation by using TABS and SPACES to make up the desired indentation (defined by the individual mode). This means, if you want to have a TAB inserted instead of TABS/SPACES you need to configure your mode to use tab-width as indentation.
For example if you use c-mode and select cc-mode as indentation style (select with C-c .) which uses 4 as indentation value, newline-and-indent will insert spaces.
To conclude:
Check that your mode uses tab-width as indentation
Check that your mode doesn't overwride indent-tabs-mode (python-mode seems to do this)
Although I personally don't like TABS good luck on your journey :)
The best strategy is to convince your programming mode of choice to
indent things the way you like. This is generally very easy; I am
picky about indentation and my emacs always does the right thing
automatically. (That means that "indent-region" also always does
what I want, which is very convenient.)
To set tabs in emacs I have this line in my .emacs:
(global-set-key (kbd "TAB") 'tab-to-tab-stop)
I'm looking for some way to make all modes show tabs in emacs as 4 spaces and have emacs save the tabs as tab characters (instead of saving them as spaces).
If I'm using c-mode that .emacs line will make tabs look like 8 spaces and save them as a tab character. But in ada-mode enter will auto-indent (which I'm ok with) and it will appear as 4 spaces in emacs and save as four spaces.
Does anyone know how to universally set tabs to insert one tab (and no spaces) when the tab key is pressed and have it appear on emacs as four spaces?
I've also tried:
(setq tab-width 4)
but I still had the same problem with ada-mode.
You can't really do it for all modes as there are mode-specific indentation variables, but you can set it it for all of the languages you care about. For C, something akin to the following in your .emacs should work for what you describe:
(add-hook 'c-mode-common-hook`
(lambda ()
(setq c-basic-offset 4)
(setq tab-width 4)
(setq standard-indent 4)
(setq c-tab-always-indent t)
)
)
That will set up tab stops at 4 characters and make 4 the default indentation level for all C-style modes. For other languages and their respective modes, you have to look up their indentation variables and set them accordingly in that mode's common hook. Some examples include 'sh-indentation, 'tcl-indent-level, and 'perl-indent-level. The easiest way to figure out what needs to be set is to run:
M-x describe-key [TAB]
That should send you down the rabbit hole.
Cheers!
Sean