0

I personally use 2 spaces for indentation but everyone has a preference and I'd like to respect that. When I open someone else's files I would prefer to use their indentation scheme. In my .emacs file here is everything I use to set my indentation:

(setq css-indent-offset 2)
(setq js-indent-level 2)
(setq javascript-indent-level 2)

(setq-default python-indent 2) ;emacs 23                                                                                
(setq-default python-indent-offset 2); emacs 24                                                                         
(add-hook 'python-mode-hook
    (lambda () (setq tab-width 2)))
(setq html-indent-offset 2)
(setq-default indent-tabs-mode nil)
(setq default-tab-width 2)
(add-hook 'html-mode-hook
      (lambda()
      (setq sgml-basic-offset 2)))

I could modify .emacs every time I want to use a different format, but I'd rather visually identify the indentation mode and manually change the variables (for the current emacs process only) to match the indentation of the file.

Now I know very little about emacs/elisp, but my assumption was M-x setq intent-tabs-mode t would make my tab key generate tabs instead of spaces. No such luck and googling has gotten me no where. Any thoughts?

  • That is the default emacs behavior of TAB. Your assumption is misplaced. See this page. – Emacs User Jul 11 '16 at 19:20
1

You can define a command that you can use for the switching. Here's a very basic version:

(defun my-set-indents (n)
  "Set the normal indentation amount.  With no argument, sets to 2.
With just one C-u sets to 4.  With any other argument, sets to that amount."
  (interactive "P")
  (setq n (if n (prefix-numeric-value n) 2))

  (setq css-indent-offset n)
  (setq js-indent-level n)
  (setq javascript-indent-level n)

  (setq-default python-indent n) ;emacs 23                                                                                
  (setq-default python-indent-offset n); emacs 24                                                                         
  (add-hook 'python-mode-hook
    (lambda () (setq tab-width n)))
  (setq html-indent-offset n)
  (setq-default indent-tabs-mode nil)
  (setq default-tab-width n)
  (add-hook 'html-mode-hook
    (lambda()
      (setq sgml-basic-offset n)))
  )

Of course, if you use it more than once, that add-hook could be problematical. I think you'll want to take out the add-hook wrapper and just have the setq directly in this code. You can then invoke it with an arg using M-x my-set-indents, or you could put it on a key (sequence).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.