I would like to change the indentation style when I edit C++ files in my .emacs to 4-spaces instead of what seems to be the default current 2-spaces. I don't want the style to change for other languages, only for C++. How can I define this in my .emacs configuration?
Use this:
(add-hook 'c++-mode-hook
(lambda ()
(setq c-basic-offset 4)))
Related
I’ve just started writing Go programs in Emacs. How can I turn off tabs highlighting in go-mode buffers? I use «whitespace» for whitespace chars highlighting. Go btw is the only mode where I don’t want tabs highlighted since tabs are standard formatting in Go.
Sincerely, Pavel.
To be clear, you're doing something like
(require 'whitespace)
(global-whitespace-mode t)
right? You can disable whitespace-mode for go-mode with
(setq whitespace-global-modes '(not go-mode))
There is a related question on emacs stack exchange.
I found that this
(add-hook 'go-mode-hook
(lambda ()
(add-hook 'before-save-hook 'gofmt-before-save)
(setq whitespace-style '(face empty trailing lines-tail))
(setq tab-width 4)
(setq indent-tabs-mode 1)))
Worked a bit better for me. Leaves whitespace-mode on but doesn't highlight tabs. Also runs go fmt before save and sets tab width to 4. I'm using prelude.
Add this line
(whitespace-toggle-options '(tabs)))
To your go-mode hook e.g
(use-package go-mode
:preface
(defun go-mode-config ()
(whitespace-toggle-options '(tabs)))
:config
(add-hook 'go-mode-hook (lambda ()
(go-mode-config))))
Taken from prelue go config
Using this emacs lisp setting one ends up with BSD/Allman style braces but introduces tabs with 8 spaces.
(add-hook 'c-mode-hook
(lambda ()
(c-set-style "linux")))
How do I keep the Allman style braces but keep the tab spaces at 4?
I don't use c-set-style, so maybe this advice doesn't fit your environment. I have in my init file:
(custom-set-variables
...
'(c-basic-offset 8)
...)
So, setting c-basic-offset to 4 after c-set-style might do the trick:
(add-hook 'c-mode-hook
(lambda ()
(c-set-style "linux")
(setq c-basic-offset 4)))
Look at C-h f c-set-style. Maybe, playing around with DONT-OVERRIDE might help also.
I would like to configure my Emacs whitespace options independently for C and python files, because the indent highlighting (space before tabs, etc.) does not apply to the Python coding style. I currently set the whitespace variables globally, but would like a separate (minimal) configuration for Python. Here is the relevant part of my .emacs:
(require 'whitespace)
(setq whitespace-line-column 80)
(setq whitespace-style '(face lines-tail indentation trailing space-before-tab))
(add-hook 'c-mode-hook 'whitespace-mode)
This works for C. Preferably I would have '(face lines-tail trailing) for Python, but I don't know how to setq just for a specific mode. What is the correct way to do this? Thanks. Using Emacs 23 in Ubuntu.
I got it to work by putting the settings into the hook along with the evocation of whitespace-mode:
(add-hook 'python-mode-hook
(lambda ()
(progn
(setq whitespace-line-column 79)
(setq whitespace-style '(face lines-tail))
(whitespace-mode))))
Perhaps using File Local Variables could help you?
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))
I'd like to disable line highlighting in term-mode. I tried this:
(add-hook 'term-mode-hook '(lambda () (global-hl-mode 0)))
but it complains about the symbol being void.
I have this further in my .emacs:
(global-hl-line-mode 1)
I agree with Ashutosh that that may be the source of your symbol error, but I'm not sure that that's the right approach anyways. I'm pretty sure that will disable highlighting everywhere, not just in terminal windows, when you load a terminal window.
I think the right thing is this:
(add-hook 'term-mode-hook '(lambda() (set (make-local-variable 'global-hl-line-mode) nil)))
...I'm going off hl-line.el where it says this:
;; You could make variable `global-hl-line-mode' buffer-local and set
;; it to nil to avoid highlighting specific buffers, when the global
;; mode is used.