Indentation and tabs in emacs - emacs

In our company, we have people who use Windows,OS-X and Ubuntu for writing code (mainly javascript) and I use mainly Emacs as my text editor.
However, I am quite new to Emacs, and I find the whole tab vs spaces customisation a bit confusing.
This is what the settings files for my friend's Visual Studio Code looks like
// Place your settings in this file to overwrite the default settings
{
"editor.tabSize": 4,
"editor.insertSpaces": false,
"files.trimTrailingWhitespace": true
}
How do I replicate the same settings on Emacs?
This is what I have so far as part of my ~/.emacs/init.el file, but it still doesn't work as expected.
;;Tab and spaces
(setq-default indent-tabs-mode nil)
(setq default-tab-width 4)
Any help to solve this and/or proper links to learn more will be appreciated..

There is an error in your conf (maybe paste pb)
;;Tab and spaces
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4) ;; not (setq default-tab-width 4)
This configuration is good but sometime another configuration (major-mode or minor-mode configuration) could overwrite your defaults.
For example
python use python-indent-offset or python-indent (obsolete since 24.3).
C++ use c-indent-level
nxml use nxml-child-indent and nxml-attribute-indent.
...
So the solution depends on the languages. You need a proper configuration by language.
I hope this helps

Related

Customizing emacs in .emacs file

I want emacs to start with specific settings by default. I found that I need to edit the .emacs file in my home directory and use LISP language. However I do get some errors. I need to have:
Windows split by vertical line (I work in C++ with headers and source files)
Column number mode
Cua-mode enabled (to work with normal copy, cut & paste shortcuts)
That's what I have in my .emacs file:
(column-number-mode)
(load "cua-mode")
(CUA-mode t)
(split-window-right)
I'ver tried coding two middle settings in one - (cua-mode). It didn't work out well.
The column-number-mode works, cua does not load and my window is split horizontally (top and bottom window). Where is my error? Thanks for feedback.
From the comments to the question:
if you're using Emacs 24.1 or later,
(column-number-mode)
(load "cua-mode")
(cua-mode t)
(split-window-right)
but if you're using an earlier version,
(column-number-mode)
(load "cua-mode")
(cua-mode t)
(split-window-horizontally)
By the way, the split-window-horizontally also works in later versions of Emacs (I'm using Emacs 25.2.1).

emacs tab and spaces indentation for visual studio projects

At my work i have switched to using emacs from Visual Studio. Since codebase is large and my other team mates use Visual Studio (VS), i can not remove the tabs.
For myself i did this :
(setq-default indent-tabs-mode nil)
;; tab-width
(setq-default tab-width 1)
So everything looks good to me, but when i am putting code for review, it looks unintended for them or in other editors.
changing tab-width to 4 made things better but i have to remember to do C-q <TAB> again and again.
I start facing the problem :
The code has lines of average of length 130. Some go as far as 200. And i work on multiple buffers like 4 at a time. This makes code hard to read.
I then let the emacs default take charge and remove both indent-tabs-mode nil and tab-width line and i found that it was much better. It was automatically inserting tab and everything for me.
However, i had little bad experience at some places which are looking good in VS.
Also i had put these configurations from default-basics github repo. Contrastingly, there is another article on spaces are evil. Internet is full of one or another and i am confused.
What is the general guidelines that i can follow ?
SO i see Tab as 1 space it would be great. But in file they should go as "they should be".
Looking at the documentation of
tab-width
Documentation:
Distance between tab stops (for display of tab characters), in columns.
This should be an integer greater than zero.
and
indent-tabs-mode
Documentation:
Indentation can insert tabs if this is non-nil.
I removed the (setq-default indent-tabs-mode nil)
and set the (set tab-width 1)
so now emacs handles the indentation and i see the tabs as 1 column width.

Emacs auto-indent tabs instead of spaces

I'm about to set EMACS put tabs instead of spaces when auto-indent, but cannot find any clue in the manual.
I tried this:
(setq standard-indent 8)
(setq-default tab-width 8), but somewhy there is still 2 characters instead of 8, and they are spaces, not tabs.
Major modes can override indentation settings. Whatever major mode you are using apparently overrides the indentation offset as well as indent-tabs-mode.
To re-enable tabs again, define the following function, and add it the hook of the affected major mode:
(defun my-enable-tabs ()
(setq indent-tabs-mode t))
Take care, though, because a major mode normally has a good reason to apply specific indentation setting.
Use this in your init file (~/.emacs), to turn off indent-tabs-mode by default, everywhere.
Yes, as #lunaryorn notes, other code (e.g. a mode) can override this default setting. But it is still your friend, so you start out right, everywhere.

Different tab indent settings in different modes

I'm currently using whitespace-cleanup in my save hook. Using indent-tabs-mode, I'm able to save files without any tabs.
All is well, I don't want tabs in my files. But.
Makefiles do need tabs. That is my problem. How do I change my settings for makefile-mode?
I tried to setq either indent-tabs-mode (the doc says it becomes buffer-local) or whitespace-style, it does not work.
OK, my bad. Files were loaded before changing the mode.
The following code works fine, provided it is loaded in the .emacs before opening any file (I have my own project manager which re-opens last files).
(defun my-tabs-makefile-hook ()
(setq indent-tabs-mode t))
(add-hook 'makefile-mode-hook 'my-tabs-makefile-hook)
I've had the same problem, and it seems that this is a bug in Emacs (as of 24.2). Try this, using the following .emacs:
(setq-default indent-tabs-mode nil)
(add-hook 'after-save-hook 'whitespace-cleanup)
If you open a file, save it, and then open a Makefile, you'll have the problem you described. But if you open a Makefile first, save it, and then open another type of file, you'll have the opposite problem: 8 spaces will be replaced by tabs.
The problem is that indent-tabs-mode is buffer-local, but in whitespace.el it is set to a regular variable called whitespace-indent-tabs-mode. Hence, the first value that's seen is the one that counts.
Here's another workaround that solves some other problems too. Add this to your .emacs:
(defadvice whitespace-cleanup (around whitespace-cleanup-indent-tab
activate)
"Fix whitespace-cleanup indent-tabs-mode bug"
(let ((whitespace-indent-tabs-mode indent-tabs-mode)
(whitespace-tab-width tab-width))
ad-do-it))
The best solution I have found for whitespace is ethan-wspace. It cleans up any whitespace that you made dirty yourself but leaves other whitespace intact.
It works for tabs in makefiles and avoids the messy diff problem where there are lots of diff lines that are just whitespace changes

How can I set Emacs tab settings by file type?

I need to be able to set the tab settings for the following file types:
.rb: 2 soft spaces
.css, .html.erb: 4 space tabs
I have tried the following, but none of it seems to alter my default tab settings for each of the file types.
;; js-mode-hook has also been tried
(add-hook 'javascript-mode-hook
'(lambda()
(setq tab-width 4)))
(add-hook 'css-mode-hook
'(lambda()
(setq tab-width 4)))
(add-hook 'html-mode-hook
'(lambda()
(setq tab-width 8)))
I am pretty new to emacs so my knowledge of configuration is pretty low.
In emacs each mode has it's own indentation style. The main command to indent (bound to TAB) is indent-for-tab-command.
This command calls mode specific indentation function found in the variable indent-line-function. So each mode has it's own way of doing it.
For Ruby (for my emacs 2 is a default):
(setq ruby-indent-level 2)
For CSS (again, default is 4 for me):
(setq css-indent-offset 4)
Unfortunately SGML mode (on which HTML mode is based) has a very simple indentation mechanism and apparently the level is not configurable.
See the source code of sgml-calculate-indent function.
I personally find it weird. I am not writing HTML, but you can try to modify the sgml-calculate-indent function yourself :). Learn some lisp.
I am using js2 mode, and it indents perfectly by default. For js you have to search for js-indent-level or something similar.
Cheers.
Theres a number of aspects to how Emacs does indentation. Setting the tab-width only specifics how big a tab is if a literal tab is inserted. If you don't wish to use literal tabs for indentation, then you should first disable their insertion (from the manual
):
Emacs normally uses both tabs and
spaces to indent lines. If you prefer,
all indentation can be made from
spaces only. To request this, set
indent-tabs-mode to nil. This is a
per-buffer variable, so altering the
variable affects only the current
buffer, but there is a default value
which you can change as well.which you can change as well.
However, to specify the indentation levels, you'll also need to set the c-basic-offset value variable as well:
(add-hook 'html-mode-hook
'(lambda()
(setq c-basic-offset 4)
(setq indent-tabs-mode nil))
In your case, you may only need the c-basic-offset but try a few combinations and see what works best.
js-mode uses js-indent-level so put (setq js-indent-level 4) into your ~/.emacs (shouldn't have to be in a hook, even, but if you're wondering, it's js-mode-hook, not javascript-mode-hook).
If setting tab-width doesn't change your indentation level for a certain mode, it's often simplest to just open the source for that mode. I found this variable by doing C-h f js-mode, clicking the link "js.el", then searching for "indent", second hit from the top.
However, if you collaborate a lot with other people, it's often better to put a cookie at the top of the file. I typically do // -*- tab-width: 8 -*- in the file, and then I have stuff like this in my ~/.emacs:
(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'cperl-indent-level 'tab-width)
(defvaralias 'perl-indent-level 'tab-width)
(defvaralias 'js-indent-level 'tab-width)
so that I have less variables to deal with (and don't have to get warnings about the file-local variable being unsafe or whatever if the mode-writer forgot to declare it as safe)
If you are using ELPA's css-mode.el with emacs 23.1.1, you can parametrize the global setting for tab width for CSS files for the tab width by doing the following:
1) Type M-x customize-variable ,
2) Then type css-indent-level,
3) Then after you change the variable to your liking, you do "Save for future sessions".
For HTML and erb: if you are using web-mode (the mode provided by Spacemacs) it can be as simple as:
(setq-default
web-mode-code-indent-offset 2
web-mode-markup-indent-offset 2)
where markup-indent-offset refers to the actual tags and code-indent-offset refers to embedded Ruby in ERB, etc.