Spacemacs set tab width
Asked Answered
V

2

40

I just migrated from VIM to Spacemacs and would like to change the tab width from default (\t?) to only 2 spaces. I found commands like

(setq-default indent-tabs-mode nil)

and

(setq tab-width 4) ; or any other preferred value
(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'cperl-indent-level 'tab-width)

My problem is that I don't know if they are correct, where in the .spacemacs file I should insert them, and what they even mean.

Variable answered 19/4, 2016 at 13:7 Comment(0)
V
55

I found this article: http://blog.binchen.org/posts/easy-indentation-setup-in-emacs-for-web-development.html

I added this part of the code into my .spacemacs file outside of any function (but before (defun dotspacemacs/user-init () ... )):

(defun my-setup-indent (n)
  ;; java/c/c++
  (setq c-basic-offset n)
  ;; web development
  (setq coffee-tab-width n) ; coffeescript
  (setq javascript-indent-level n) ; javascript-mode
  (setq js-indent-level n) ; js-mode
  (setq js2-basic-offset n) ; js2-mode, in latest js2-mode, it's alias of js-indent-level
  (setq web-mode-markup-indent-offset n) ; web-mode, html tag in html file
  (setq web-mode-css-indent-offset n) ; web-mode, css in html file
  (setq web-mode-code-indent-offset n) ; web-mode, js code in html file
  (setq css-indent-offset n) ; css-mode
  )

and added the line

(my-setup-indent 2) ; indent 2 spaces width

into the (defun dotspacemacs/user-init () ... ) like this:

(defun dotspacemacs/user-init ()
  "Initialization function for user code.
It is called immediately after `dotspacemacs/init', before layer configuration
executes.
 This function is mostly useful for variables that need to be set
before packages are loaded. If you are unsure, you should try in setting them in
`dotspacemacs/user-config' first."
  (my-setup-indent 2) ; indent 2 spaces width
  )
Variable answered 19/4, 2016 at 13:16 Comment(0)
S
28

You can also just customize the the standard-indent variable, setting it to 2, by calling the command customize-variable within spacemacs. This will save the customization into your .spacemacs file.

Edit:

to run 'customize-variable' use the hotkey M-x (alt-x on most systems) then type customize-variable in to the prompt.

you can use the search to search for 'standard-indent'

Sandbank answered 22/7, 2016 at 16:40 Comment(5)
Can you explain a little better how to do this? Sorry, I am new to spacemacsGough
@Gough You press SPC SPC then type customize-variable into the helm (or ivy) buffer that just popped up, select that entry and press Enter. In the new buffer you type in standard-indent and press Enter. In the new window you change the variable from its default 4 to 2 and then click on Apply and Save.Glottic
And you exit the Easy Cutomization interface by typing qMuskogean
Why isn't this the accepted answer? Seems less hackyStabilize
@Stabilize I think its cuz the other answer sets the spacing for a ton of modes at once. Using customize group, you'd have to set that indentation value for all the modes one by one, its a bit tediousBettinabettine

© 2022 - 2024 — McMap. All rights reserved.