emacs tab and spaces indentation for visual studio projects - emacs

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.

Related

Why does emacs display tabs differently?

So I've heard about the goodness of emacs and have only recently started using it. Forgive me if this is a stupid question, but why does emacs display tabs, differently? It seems as though it doubles the number of spaces, but it doesn't, at least, I guess. Here're some pictures to describe what I'm talking about:
And this is what it looks like in emacs:
As I've previously stated, it seems as though it doubles the number of spaces. When I add this line to .emacs:
(setq c-basic-offset 4)
and reindenting the code using C-x h C-M-\ makes it look normal in emacs, but the secondary indentation are in-line with the first indentation (as in 2 tabs are now 1 tab) when viewed in other text editors, and again, I couldn't understand why. Changing it to
(setq c-basic-offset 8)
makes it save and display normally in other text editors though. At this point I'm really, really confused.
Can someone please explain why? Thanks.
The variable tab-width is the distance between tab spaces in columns, and defaults to 8. If you'd like it to default to 4, you can (setq-default tab-width 4). If you'd like to untabify everything and convert tabs to spaces, you can do M-: (untabify (point-min) (point-max)).
And you might find this thread helpful, especially the point on tab-stop-list when you want to ADD your own tabs.
You can also adopt sanity and not use TAB chars in your code. ;-)
To prevent inserting TAB chars when you hit the TAB key (and RET or C-j, depending on your Emacs version) set the value of option indent-tabs-mode to nil.
To remove pre-existing TAB chars from code you are editing, use command untabify.
See also Tabs Are Evil and Untabify Upon Save.
And note that, in Emacs, whether or not you use TAB chars is unrelated to how much and whether code is indented. For example, option c-basic-offset governs indentation amount regardless of whether TABs are used for some of the indenting.
Note too that after you kick the TAB habit, any TAB chars left in your code that are meaningful to the code are much easier to find. They are not lost in an ocean of insignificant-whitespace TABs.
Finally, note that there are various ways to highlight TAB chars. Command hc-toggle-highlight-tabs in library highlight-chars.el is one way. See Show Whitespace.

Using tabs alone for indentation in 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.

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 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.

Getting Emacs indentation to work correctly in major mode

I am writing a major-mode in emacs for a DSL I've created. I'm inheriting from fundamental-mode, which tabs out way far (6 tab stops, I think).
I'd like to be able to define:
(setq mydsl-tab-width 4)
and have that work.
Not quite understanding the question...
In your major mode, I presume you're making some settings. Perhaps one of those could be:
(setq tab-width mydsl-tab-width) ;# use the tab width specified by your variable
Could you elaborate on how mydsl-tab-width is currently used? Emacs surely doesn't know about it - tab-width is the variable to use/set.