1

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.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
user17880
  • 43
  • 1
  • 7

4 Answers4

12
;; 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
  • 73,529
  • 11
  • 197
  • 229
  • Thanks. I added a redraw-display after setting the variable so that the change is reflected immediately. – user17880 Feb 10 '09 at 17:32
  • Thanks! Instead of hitting f8 each time I open a buffer, can I add something to my .emacs to run this automatically? – Ian Cohen Feb 21 '10 at 02:13
  • @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 Feb 21 '10 at 04:35
  • 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 Feb 21 '10 at 18:00
  • Tip: You can toggle between 8, 4 or 2 with this line instead: `(setq tab-width (if (= tab-width 8) 4 (if (= tab-width 4) 2 8)))` – Alexander Mar 22 '12 at 12:39
5

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
  • 9,945
  • 10
  • 49
  • 63
  • Thanks. We have editor specific markup in some files, but it's generally frowned upon as everyone uses a different editor :-/ – user17880 Feb 10 '09 at 15:47
  • 1
    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. – user17880 Feb 10 '09 at 15:56
1

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"))
)
user17880
  • 43
  • 1
  • 7
0

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
  • 118,630
  • 17
  • 138
  • 146
  • 1
    C-g is hard-coded to mean "quit" in so many places that I would recommend against rebinding that key anywhere. It just won't work right. – jrockway Feb 10 '09 at 04:49