Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: Set tab indent for just one file on the fly

Tags:

emacs

I work on an open source project where the creator sets his tab-indents to 2 spaces.

I'd like to just enable it on the fly for the one file I work on and not other files of the same type. There must be something like M-x set-tab-indent. It is a JavaScript file ending in .js.

I know I can use:

(setq-default tab-width int) 

inside my .emacs file, but I rather just call an M-x command to set it and forget it during my duration of working on this file. I tried M-x apropos and Google but couldn't find the specific command.

Thanks.

like image 781
Mauvis Ledford Avatar asked Jun 04 '11 23:06

Mauvis Ledford


People also ask

How do I remove an indentation in Emacs?

To delete just the indentation of a line, go to the beginning of the line and use M-\ ( delete-horizontal-space ), which deletes all spaces and tabs around the cursor. If you have a fill prefix, M-^ deletes the fill prefix if it appears after the newline that is deleted.

How do I change the tab space in Emacs?

Just change the value of indent-line-function to the insert-tab function and configure tab insertion as 4 spaces.


2 Answers

You can make the variable js-indent-level local to the buffer using:

M-x make-variable-buffer-local <RET> js-indent-level <RET>

Then you can set that variable in the buffer using:

M-x set-variable <RET> js-indent-level <RET> 2

like image 140
Omri Barel Avatar answered Sep 19 '22 09:09

Omri Barel


The easiest way to do this for a single buffer is to use M-x set-variable.

  1. Type M-x set-variable and press enter
  2. When prompted for the variable to set, set tab-width then press enter
  3. You'll be prompted with the line Set tab-width (buffer-local) to value:. Put the value you want, then hit enter

The buffer should instantly be updated with the new value.

like image 32
Twylo Avatar answered Sep 21 '22 09:09

Twylo