tags:

views:

1577

answers:

4

The source tree that I work on has files indented with different tab values, (not to mention spaces) and the best I can do is to set emacs to use the style found in the region of code I am modifying. Instead of doing M-x set-variable tab-width to 4 or 8, a key binding to toggle the tab-width among these two value would help immensely.

Thanks.

A: 

Throw this in your .emacs or .emacs.d/init.el file:

(defun toggle-spaces ()
  "Toggle tab-width between 4 and 8"
  (interactive)
  (if (eq tab-width 4)
      (setq tab-width 8)
    (setq tab-width 4)))

;; This will set Ctrl-g to toggle but you can set it to anything
;; you want.
(global-set-key "\C-g" 'toggle-spaces)
Sean Bright
C-g is hard-coded to mean "quit" in so many places that I wouldrecommend against rebinding that key anywhere. It just won't work right.
jrockway
Ah yes. Good point.
Sean Bright
+7  A: 
;; Obviously substitute your preferred key for <f8>
(global-set-key (kbd "<f8>") 'tf-toggle-tab-width-setting) ; ' "fix" highlighting

(defun tf-toggle-tab-width-setting ()
    "Toggle setting tab widths between 4 and 8"
    (interactive)
    (setq tab-width (if (= tab-width 8) 4 8))
    (redraw-display))

Edited to add redraw-display as per comment suggested

Trey Jackson
Thanks. I added a redraw-display after setting the variable so that the change is reflected immediately.
Thanks! Instead of hitting f8 each time I open a buffer, can I add something to my .emacs to run this automatically?
Ian Cohen
@IanCohen I'm not quite sure what you want. The request was for toggling the width. If you want it to be set to a particular value for all buffers, add `(setq-default tab-width 4)` (or whatever value you want).
Trey Jackson
I ended up using M-x untabify after toggling tab-width 4. Adding (setq-default tab-width 4) to my .emacs didn't change the spacing of files which had tab-width 8. Using the toggle function above changed the spacing, but I had to run it each time I opened a buffer. Thanks for following up.
Ian Cohen
+2  A: 

Not quite answering the question (the answers given are good enough), but you might want to consider setting a per-file local variable. For example, assuming that "//" means comment in your language, you would put the following in the first line of the file:

// -*- tab-width: 4 -*-

And emacs will set the variable for you whenever you visit the file. See http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html for more information on file-based variables.

Of course this might not be a choice if the file is shared among a group - unless you can convince your colleges that this first line comment is completely harmless and extremely useful!

polyglot
Thanks. We have editor specific markup in some files, but it's generally frowned upon as everyone uses a different editor :-/
And I guess it'd be a good idea to use setq-default so that a local buffer variable takes precedence. I hadn't thought about it when I put in the question. Thanks again.
A: 

Thanks for all the answers. I added a redraw-display call so that the change is reflected immediately. (Tried posting this as a comment, but can't.)

(global-set-key (kbd "<f8>") 'tf-toggle-tab-width-setting)
(defun tf-toggle-tab-width-setting ()    "toggle setting tab widths between 4 and 8"
    (interactive)
    (setq tab-width (if (= tab-width 8) 4 8))
    (message "set tab-width to %d." tab-width)
    (redraw-display)
)

And along the same lines. :(

(global-set-key (kbd "<f7>") 'tf-toggle-indent-mode-setting)
(defun tf-toggle-indent-mode-setting ()
    "toggle indenting modes"
    (interactive)
    (setq indent-tabs-mode (if (eq indent-tabs-mode t) nil t))
    (message "Indenting using %s." (if (eq indent-tabs-mode t) "tabs" "spaces"))
)