Why does emacs display tabs differently? - emacs

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.

Related

TAB key not working in scratch buffer of emacs

Hitting TAB key while entering text into scratch buffer does not do anything. I would like TAB key to behave exactly as it would in other normal buffers(move point forward by inserting some X number of spaces or insert a TAB character). Could you please help me achieve that? Thank you.
I have come across below question, but it doesn't have an answer and the link mentioned in its comment is not working. I do not have enough score to add a comment to it.
How to enable tab key in scratch buffer of emacs?
Feel free to close the current question if you could add an answer to above question.
TAB's behavior in most programming-language modes is not "insert a tab" but "make sure the code on this line is indented correctly according to the current rules". The default mode for the scratch buffer is lisp-interaction-mode, and since you have not written any Lisp code, there is no indenting to do, so TAB does nothing.
There are a few ways you could change this. You could change the major mode to, say, text-mode or fundamental-mode, either for a single session (with M-x text-mode) or permanently (by putting (setq initial-major-mode 'text-mode) into your .emacs file).
Or you could leave the mode alone, and rebind the TAB key entirely. One way to do this would be
M-: (global-set-key (kbd "TAB") 'self-insert-command)
I'm sure there are many other alternatives, depending on how exactly you want your scratch buffer to act.
If all you're interested in is to insert the TAB character (i.e. \t), then you could use the quoted-insert function. By default it's bound to C-q. It captures the next input character and inserts it verbatim. So in your case that would be C-q TAB.
I posted that comment with the now dead link, so I'll quote from the Wayback Machine copy:
Emacs isn't inserting anything!!
If you feel like I do, you probably are considering this a fault. You keep pressing the TAB key, but nothing happens.
In programming modes, such as when you're editing C or Perl or Lisp source code, the TAB key is bound to special indentation rules. That is, instead of being bound to indent-relative as in text-mode, the TAB key is pre-bound to cc-indent-line or lisp-indent-line (if editing your .emacs file), and so on. In c-mode, pressing the TAB key will move the cursor to the first indentation level, and then may not move the cursor forward after that, no matter how many times you press it.
If this behavior isn't what you want, you can do one of these things:
Press Ctrl-q <TAB> to insert a TAB character right now
Temporarily reassign the TAB key to self-insert-command while staying in the same editing mode
Switch to a different editing mode for this session; the TAB behavior will change with the editing mode
Change your .emacs file to permanently change the editing mode for the filetype you're using now
I'd recommend reading the entire page, as it explains well how Emacs treats the TAB key and tab characters different from pretty much everything else.

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.

What is the use of a fill-column if most modern text editors can word-wrap?

I have my emacs fill-column set to almost 10 trillion (999999999999). Is that bad, or is there another way to make emacs have a virtually infinite fill-column?
What is the use of a fill-column anyways? Why would one want that sort of line wrapping?
Yes, you can have a gigantic fill-column. Or yes, you can turn off auto-fill-mode (use (auto-fill-mode -1) or set auto-fill-function to nil).
"That sort of line wrapping" is useful if you want text that has lines of limited length. (Duh.)
See also visual-line-mode, which can give you a similar visual effect but without the insertion of hard newlines.

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.

Turn off Emacs Whitespace-mode "Long Line" Visualization

I personally keep all lines under 80 characters, but I also work on projects in teams where other programmers don't care about line length.
I love using whitespace-mode, but the long line visualization is really annoying when I'm working on projects where I shouldn't interfere with the long lines. It seems like it should be easy to turn off the long line visualization---I hit m-x global-whitespace-toggle-options l, and then can hit m-x global-whitespace-toggel-options ? to confirm that the "long-line visualization" is turned off. But long lines are still highlighted. I kill buffers and reload them, and highlighting is still there. I'm definitely using global, not local, whitespace-mode.
Why can't I turn off the long line visualization?
The last time I customized whitespace-mode, I noticed that my changes to the settings didn't have any effect in buffers that already existed; try recreating the buffer, or leaving and reentering whitespace-mode. In case you don't already know, you can use M-x customize-group whitespace to turn off that particular option entirely, rather than doing it manually.
Edit: Specifically you want to customize the whitespace-style variable. This lets you turn on and off individual styles. In this case you should turn off the ones labelled "(Face) Lines" and "(Face) Lines, only overlong part". The former changes the face of the whole line when it is overly long, while the latter only changes the face of the part that extends past the threshold.
(Other options in this group define the faces that whitespace-mode will use to highlight the styles you've turned on, the regexes it uses to identify certain situations, etc, but usually you only care about whitespace-style).
Set whitespace-line-column to a higher value (default is 80), so the highlighting of long lines doesn't kick in:
(setq whitespace-line-column 250)
I'm assuming that you already have whitespace-mode activated somewhere in your init.el or similar. If so, you can adapt duma's comment above, and either
Edit the elisp that sets whitespace-style to remove lines-tail. E.g., Emacs Prelude sets
(setq whitespace-style '(face tabs empty trailing lines-tail))
Simply change that to
(setq whitespace-style '(face tabs empty trailing))
If you don't want to directly edit that elisp, but rather override it later with your own code, do something like
(setq whitespace-style (delete 'lines-tail whitespace-style))
Unfortunately, if running Prelude with auto-loaded buffers (using something like Emacs Desktop), the initial setting will take precedence: for each buffer on which you want to see whitespace-style displayed as directed, you must [1]
kill the buffer
re-open the buffer
[1]: Note to OP: if there's another way to reload a buffer, please edit or comment this answer. I was hoping to find something like M-x reload-buffer but am not seeing anything like that with C-h a buffer.