Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't find this: How do I use 4 SPACES instead of a TAB in EMACS?

Tags:

php

emacs

elisp

I am making the jump to EMACS, and I can't find what I need to do in my .emacs file to get php-mode AND all other modes to insert 4 spaces instead of a TAB. Help?

UPDATE:

When I hit tab I still get 8 spaces in a plain file with the given answers. In php-mode I still get 2 spaces. Hitting tab in php mode does nothing, tab in regular EMACS adds 8 spaces.

UPDATE2:

This is what I have in my .emacs:

(require 'color-theme)
(color-theme-calm-forest)

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq c-basic-offset 4)

Still in regular files 8 spaces, and in PHP files the tabbing doesn't work, or jumps around randomly now. My php-mode is from the Ubuntu 9.10 apt-get install php-mode


UDATE3:

OK Here is what I want...

  1. When I hit the TAB Key, and when I always hit the TAB key, I want 4 SPACES inserted.
  2. I want the TAB key to jump to the relative position of the previous line (auto tab up to the last line, again entering in SPACES)

These rules need to apply to all files but if necessary need to first and foremost apply to (text) and PHP files.

like image 837
Urda Avatar asked Mar 05 '10 16:03

Urda


People also ask

How do you change the tab indent to 4 spaces?

The shiftwidth parameter controls your indentation size; if you want four space indents, use :set shiftwidth=4 , or the abbreviation :set sw=4 . If only this is done, then indentation will be created using a mixture of spaces and tabs, because noexpandtab is the default.

How do I change the tab space in Emacs?

Thankfully, Emacs has facilities in place that make it possible to easily convert between tabs and spaces. The commands tabify and untabify do just that; they convert the region to tabs or whitespaces.

Is 4 spaces a tab?

In most code editors, tabs are not the same as 2 spaces or 4 spaces by default. A tab is stored differently than spaces in the code. Tabs can be seen as a big “jump” in the text, while spaces are always 1 space each.

Is tab 4 spaces or 8?

Indentation: tabs vs spaces Java: 4 spaces, tabs must be set at 8 spaces.


3 Answers

Change the variable indent-tabs-mode to nil. You can do it interactively (for just one buffer) by M-x set-variable. To make it permanent (and for all buffers), put

 (setq-default indent-tabs-mode nil)

in your init file.

To make a tab do just 4 spaces in most modes, also add

 (setq-default tab-width 4)

For C based modes (like PHP) you will have to do:

(setq c-basic-offset 4)
like image 113
Andrew Stein Avatar answered Oct 04 '22 08:10

Andrew Stein


You're missing a (setq c-basic-indent 4). So you should have:

;; 4 spaces rather than tabs
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq c-basic-offset 4)
(setq c-basic-indent 4)
like image 29
theadriangreen Avatar answered Oct 04 '22 07:10

theadriangreen


The default behavior of TAB in Emacs (in programming modes) is to automatically indent to the right place, where this place is decided based on indentation rules described by the major mode (it's one of the most important job of a major-mode, actually, much more demanding in general than color-highlighting). Of course if the line is already indented to the right place, hitting TAB won't do anything.

So you can force TAB to behave in a dumb way, like you're asking, but then you'd miss out on most of the fun. E.g. you could do something like

(global-set-key "\t"
  (lambda ()
    (interactive)
    (let ((prevline-indent (save-excursion (forward-line -1) (current-indentation))))
      (if (< (current-column) prevline-indent))
          (indent-to prevline-indent)
        (insert "    ")))))

But instead, I recommend you use TAB the way Emacs intended and explain to Emacs how you like your code to be indented. Probably by setting something like:

(setq-default indent-tabs-mode nil) ;; Prefer SPC over TAB when indenting.
(setq c-basic-offset 4)             ;; I like indenting by 4 spaces.

From what you say, the TAB indentation in your php-mode doesn't work right, so there might simply be a bug in your php-mode. Tell us exactly in which case it is not working right, showing the text that gets mis-indented, and explaining in which way the indentation does not match your expectations.

like image 43
Stefan Avatar answered Oct 04 '22 07:10

Stefan