Using tabs alone for indentation in Emacs - emacs

I love Emacs but don't like how it does indentation: either an uncontrollable mix of tabs-and-spaces or just spaces ((setq indent-tabs-mode nil)).
I want Emacs to do do indentation:
With tabs alone.
Do the indentation to a fixed number of places (not 6 sometimes, 8 other times and 4 in some other places).
Be able to set one level of indentation as being equal to 4 (or 2) spaces.
If I change the value of the tab stop, all newly-opened or reloaded files should use the new value (or can this change be affected only by re-starting Emacs?)
Is all of the above possible? Some settings in .emacs or a package?
Most IDEs (e.g. Eclipse) offer the above.

smart tabs would insert tabs and spaces contextually.
Personally I only use spaces for both indentation and alignment (at least for my own projects). Here is another article on emacswiki I found very useful about the topic

For C/C++/Java, you could try adding to your mode hook an identical tab-width, indent-level and c-basic-offet:
(defun my-c-mode-common-hook ()
(setq c-indent-level 3
c-brace-offset -3)
(setq c-basic-offset 3)
(setq-default tab-width 3)
(setq tab-width 3))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
This makes it so when you're in cc-mode, tabs and indenting are equal so emacs will always choose tabs over spaces. See also Indenting C
The tab-width can be set elsewhere and you can apply it to other modes in conjunction with indent length variables like python's python-indent.
Edit:
Actually, it looks like kindahero's link pretty much does this: http://www.emacswiki.org/SmartTabs

I use tabs for indentation. But when someone else using a different editor, they could see that the indentation is gone. So, you can select the piece of code that you indented using tabs and run "M-x untabify". This replaces the tabs with white space, so the first said issue won't be there for the users using a different editor.

Related

emacs: Want to indent with tabs (tab-size 2 or 4) allign with spaces

I use emacs now for a while and like it in most cases (useful on ssh, syntax highlight, ...).
But when it comes to indentation and justification (alignment) of code, I don't get clear with emacs.
I want in all my code (SQL, C, Java, ECMAScript, HTML, PHP, CSS, ...) that a press of the "TAB" key realy makes a TAB character (which I usually have a size of 2 spaces, but 4 or 6 are good as well). [I agree, that TABs in Code are evil when it comes to alignment, but spaces are evil as well, when it comes to indentation!] You can have a look, how I want to have it in my HTML template:
https://github.com/pheek/HTMLTemplate/blob/master/template.html
On the other hand I always align with spaces, so other programmers have all the equal signs properly aligned in my code. This works fine, except for 2 exceptions:
To enter a TAB-Char I always have to press "CTRL-q TAB", which is annoying.
Different programming languages are configured in emacs to use different TAB sizes. For myself, I always want to behave a TAB to be the size of 2 spaces.
How can I achieve that
a) a press of the TAB-Key always inserts a TAB-Char?
b) TABs are in all emacs-modes (c, java, html, php, css, ecmascript, ...) 2 chars wide?
As #lawlist says, it's different for every major mode. Each one usually has its own indent-line-function, and settings like tab-width are buffer local.
There's a smart-tabs package (https://github.com/jcsalomon/smarttabs) that works well for me, but I only use it for C-like modes (which is not to say it doesn't work for others, I just haven't tried).
(use-package smart-tabs-mode
:commands (smart-tabs-mode)
:init
(add-hook 'c-mode-common-hook #'smart-tabs-mode)
:config
(smart-tabs-advice c-indent-line c-basic-offset)
(smart-tabs-advice c-indent-region c-basic-offset)
)
tab-width should be set to your preferred width globally, and can be changed in major mode hooks to be different in some modes.
(setq-default tab-width 2)
(defun jpk/c-mode-common-hook ()
(setq tab-width 4))
(add-hook 'c-mode-common-hook #'jpk/c-mode-common-hook)
a) Set the value of indent-tabs-mode to non-nil in your emacs configuration file.
(setq-default indent-tabs-mode t) should do the trick.
https://www.gnu.org/software/emacs/manual/html_node/emacs/Indentation.html
b) Similarly, set the value of tab-width to the desired width.
(setq-default tab-width 2) in your case.
https://www.gnu.org/software/emacs/manual/html_node/efaq/Changing-the-length-of-a-Tab.html

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.

How to make emacs perform the same as other editors on tab character?

The TAB key in emacs is bound to indent-for-tab-command, but the indent itself converts all the tabs into spaces, which I didn't like because it's harder to locate character in the code and the code gets bigger.
I tried to use (setq-default indent-tabs-mode t), (setq tab-width 4) or (setq default-tab-width 4), none of the above works. Neither the width of \t character changes, nor the indent uses tabs instead of spaces.
And 'M-x tabify' does not work either.
I searched for a long time but got nearly nothing. Any ideas?
It's your .emacs settings, if you don't have any special settings in your .emacs file, tab should be inserted by default.
;; This will force emacs to insert spaces instead of tabs
(setq-default indent-tabs-mode t)
This other two settings are not related.
Check emacswiki.org for any Emacs related questions.

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.

Removing tab inconsistencies in Emacs

To set tabs in emacs I have this line in my .emacs:
(global-set-key (kbd "TAB") 'tab-to-tab-stop)
I'm looking for some way to make all modes show tabs in emacs as 4 spaces and have emacs save the tabs as tab characters (instead of saving them as spaces).
If I'm using c-mode that .emacs line will make tabs look like 8 spaces and save them as a tab character. But in ada-mode enter will auto-indent (which I'm ok with) and it will appear as 4 spaces in emacs and save as four spaces.
Does anyone know how to universally set tabs to insert one tab (and no spaces) when the tab key is pressed and have it appear on emacs as four spaces?
I've also tried:
(setq tab-width 4)
but I still had the same problem with ada-mode.
You can't really do it for all modes as there are mode-specific indentation variables, but you can set it it for all of the languages you care about. For C, something akin to the following in your .emacs should work for what you describe:
(add-hook 'c-mode-common-hook`
(lambda ()
(setq c-basic-offset 4)
(setq tab-width 4)
(setq standard-indent 4)
(setq c-tab-always-indent t)
)
)
That will set up tab stops at 4 characters and make 4 the default indentation level for all C-style modes. For other languages and their respective modes, you have to look up their indentation variables and set them accordingly in that mode's common hook. Some examples include 'sh-indentation, 'tcl-indent-level, and 'perl-indent-level. The easiest way to figure out what needs to be set is to run:
M-x describe-key [TAB]
That should send you down the rabbit hole.
Cheers!
Sean