Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I love Emacs, but my customization powers are weak, and I would appreciate any help on this.

My default settings are to never use literal tab stops (indent-tabs-mode nil). However, for one particular project I do need to use tab stops, and I need to display tabs with spacing 4 (not the default 8), and I want the tab-stop-list to be 4, 8, 12, ....

Now the question: How can I define (in my ~/.emacs.d) a single function that I can M-x invoke from within any mode I might already be in so that these particular settings are overridden to those requirements? Alternatively, how can I make these settings a minor mode that I can activate optionally inside a given major mode (say some fictitious PHP-mode (which I haven't found yet))?

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted
(setq-default 
          tab-width 4                                     ; Set tab stops
          tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44
                            48 52 56 60 64 68 72 76 80 84)
          )

put this in your .emacs.d/init.el and use M-i to go to next tab position

UPDATE:

(defun my-tab-width ()
  "set tab-width as local variable"
  (interactive)
  (progn
    (set (make-local-variable 'tab-width) 4)
     (set (make-local-variable 'tab-stop-list)
     '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84))))

call this function iteratively with M-x or put a hook to your desired mode to load automatically

(add-hook 'php-mode-hook 'my-tab-width)
share|improve this answer
    
No no, I don't want this to happen by default - rather, I only want to enable this behaviour in very particular situations, so I'd like a command that I call in those situations and that only changes the local settings for that one buffer. –  Kerrek SB Sep 4 '11 at 12:08
    
updated the answere –  kindahero Sep 4 '11 at 12:27
    
Great, this is already working well in fundamental mode. But now, say, I finally do find a PHP mode which already defines everything, how can I make the command override the mode's settings? –  Kerrek SB Sep 4 '11 at 12:38
    
doesn't it make local variables if you call the function in the buffer?? –  kindahero Sep 4 '11 at 12:48
    
I just tested it in C++ mode, where it doesn't override the mode's indentation settings :-( –  Kerrek SB Sep 4 '11 at 12:50
show 3 more comments

You probably don't need to create a mode. You only want to set variables (indent-tabs-mode, tab-width and tab-stop-list) for one or multiple files.

That has also been asked in this Stack Overflow question.

My suggestion: Create a ".dir-locals.el" file in that project's directory with these contents:

((nil . ((tab-width . 4)
         (indent-tabs-mode . t)
         (tab-stop-list . (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84)))))

That will set those three variables for all files in that folder.

But you need to make sure that your mode/file hooks don't also set these variables. Setting them directly that way will always take priority no matter what you configure.

And remember that these variables only affect pure text indentation. Programming modes usually have their own variables for indentation (e.g. c-basic-offset).

share|improve this answer
    
Can I somehow turn that into a command? I don't want to have to copy a configuration file into random parts of a large source tree... but thanks for the idea! (I already managed to set my working buffer to have the right behaviour, I just want to be able to activate this behaviour with a single command somehow.) –  Kerrek SB Sep 4 '11 at 13:00
    
You don't need to put it into random parts of the source tree. You can put it at the project root and/or specify a folder instead of nil. See gnu.org/software/libtool/manual/emacs/Directory-Variables.html for details. Why use a single command when zero commands will work? Why write code when the code already exists? –  nschum Sep 4 '11 at 13:15
    
That's true, but it does create this non-local dependency... the point is that I don't always have the exact same source tree, I might be checking it out in random places on random machines - but I always have my ~/.emacs :-) –  Kerrek SB Sep 4 '11 at 13:20
    
I did try it, though, but I get lots of "Wrong type argument: number-or-marker-p, quote" errors when I press tab... –  Kerrek SB Sep 4 '11 at 13:24
    
Fixed by removing the apostrophe. I would still prefer to copy a file when checking out rather than activating a mode every time I open a file. But anyhow, this answer might not be for you then. :) Maybe it will help future visitors. –  nschum Sep 4 '11 at 14:54
show 6 more comments

I'm no eLISP expert, but you could add something like

(add-hook 'your-minor-mode '(set-variable 'tab-width 4))

to your .emacs

share|improve this answer
    
How would I activate the mode? If I just put this in, I don't seem to get the new minor mode. –  Kerrek SB Sep 4 '11 at 11:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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