Set python indent to 2 spaces in emacs 23? - emacs

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))

Related

In Emacs, while editing a CMake file, how to turn off smart indentation?

I just want each line to get the same indentation as the previous line, and that TAB would indent 4 spaces. For C++ I managed it with:
(setq-default indent-tabs-mode nil)
(setq-default c-syntactic-indentation nil)
(setq-default c-basic-offset 4)
In my .emacs, but for CMake files Emacs just indents the lines automatically according to its own rules, and TAB has no effect at all.
What you probably need to disable is known as electric-indent-mode which is the function which indents your code when you press return. To disable it for cmake-mode, as in this answer is to include the following line in your init
(add-hook 'cmake-mode-hook (lambda () (electric-indent-local-mode -1)))

how to control tabs and whitespace with emacs version 24.3?

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.

Emacs modeline at the end of file in one line

I am working on a project where some people use vi, some use emacs and some others (including gedit). The most simple yet global way (although not perfect) to enforce (at least visual) style was to add the following lines to the end of each file:
...
return 0;
}
// Editor modelines - generated by http://www.wireshark.org/tools/modelines.html
// Local variables:
// c-basic-offset: 4
// tab-width: 4
// indent-tabs-mode: t
// truncate-lines: 1
// End:
// vim:set ft=cpp ts=4 sw=4 sts=4 nowrap: cindent:
the question is: how can I convert the emacs portion in a "one-line" code (as vim can)? and yet keep it at the end of the source file (not at the top).
(Probably this can be recasted as Lisp question but I am not familiar with it)
Directory Local Variables are probably a better approach.
http://www.emacswiki.org/emacs/DirectoryVariables
http://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html
The single .dir-locals.el file will be processed by everyone, and no need for file local variables at all.
vim may well have a similar mechanism?
You can use the eval: declaration, but Emacs will ask you to confirm that it is safe to evaluate. If you tell Emacs to accept it permanently, it won't ask about that expression again (it stores it in safe-local-variable-values in the custom-set-variables section of your init file).
;;; Local Variables:
;;; eval:(setq c-basic-offset 4 tab-width 4 indent-tabs-mode t truncate-lines 1)
;;; End:
You can wrap multiple expressions in progn:
;;; Local Variables:
;;; eval:(progn (setq c-basic-offset 4) (message "hello"))
;;; End:
Or use any other constructs (I don't think there are any restrictions).
You would need to use the // Local Variables: and // End:. The rest can be made into one line as in // eval: (setq c-basic-offset 4 tab-width 4 indent-tabs-mode t truncate-lines 1).
Looks like you might be SOL:
Specifying File Variables: http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html
Variables in Emacs:
http://www.gnu.org/software/emacs/manual/html_node/emacs/Variables.html#Variables
Unless you use Phils's eval style.
(setq c-basic-offset 4 tab-width 4 indent-tabs-mode t truncate-lines 1) add this to your .emacs. If you wish to do this for specific file types you can always use the add-hook call, such as (add-hook 'c-mode-common-hook 'your-func-here) where your-func-here could be a function that simply sets those variables.
Refer to phils method of using Local Variables and eval to accomplish this with the first line of code given above. Sorry I didn't quite understand that this was only for a single file or very few files.
I'm going to turn the question around? Do you really need any file-local variables? If you use the same style throughout all source files, it might be better to define a C indentation setup and distribute this among all developers.
(defconst my-c-style
'((c-basic-offset . 2)
(c-offsets-alist
. ((substatement-open . 0)
(statement-case-open . +)
(inline-open . 0)
(arglist-cont-nonempty . (c-indent-operator-lineup-arglist-operators
c-lineup-arglist)))))
"My indentation style")
(defun my-c-mode-common-hook ()
(interactive)
(c-add-style "my" my-c-style t))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

How can I set Emacs tab settings by file type?

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.

How to adjust >> and << behavior in Emacs (Vim emulation, indent, dedent)?

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)