How to set Emacs tabs to spaces in every new file? - emacs

I would like to have an .emacs setting so that tabs are always formed by consecutive spaces. Preferably in each possible mode. In other editors it never seemed a problem, but in .emacs I'm a bit stuck with the tabs I'm afraid.

add this in your .emacs:
(setq-default indent-tabs-mode nil)
or you can define a before-save-hook that eliminate hard tabs

Also of use, M-x untabify, which will convert all the tabs into spaces in the current region. You can use this to get rid of the existing tabs in files you've edited before you had the indent-tabs-mode set properly.
C-x h (M-x mark-whole-buffer)
M-x untabify

Related

Emacs : Open buffer in vertical split by default

I am very very new to emacs. I want something like this. Every time I open a new buffer, it should split current winodow vertically. How should I change .emacs file. Please provide some pointers.
You know that you can do this manually with C-x 3 right? So we can use this fact to learn how to add the command to do this to .emacs.
We just need to find out what the function is. So let's do C-h k C-x 3 to find the help for C-x 3. That shows:
C-x 3 runs the command split-window-right, which is an interactive
compiled Lisp function in `window.el'.
So, open .emacs (C-x C-f ~/.emacs), go to the end of the file and add:
(split-window-right)
Then save the file, restart emacs and it should work. I just tested it.
I don't remember the exact route I followed to get this, but I have the following configuration to suggest to Emacs that it should split the frame vertically rather than horizontally when Emacs has the choice (eg when bringing up help).
This seems to work fine on my widescreen monitors.
(setq split-height-threshold nil)
(setq split-width-threshold 160)
Add This to .emacs to split windows vertically as default opening a new buffer in other windows
(setq
split-width-threshold 0
split-height-threshold nil)
From this post on reddit you can set this explicitly for ediff.
(custom-set-variables
'(ediff-window-setup-function 'ediff-setup-windows-plain)
'(ediff-diff-options "-w")
'(ediff-split-window-function 'split-window-horizontally))
This has the advantage that it doesn't impact other splits.

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

Making Emacs tabs behave exactly like vim's

I'm learning currently Emacs and I'm trying to set up my initialization file.
Currently it looks like this (found it somewhere in the web):
(setq indent-tabs-mode t)
(setq-default indent-tabs-mode t)
(global-set-key (kbd "TAB") 'self-insert-command)
(setq default-tab-width 4)
(setq tab-width 4)
(setq c-basic-indent 4)
But it does not behave like Vim's style of tabs.
I just want it to behave like Vim when using tabs.
That means not substituting tabs with spaces (I think Emacs does this by default).
So that everyone can edit files in their preferred tab width. I generally use 4 for the tab width. And that when I press Backspace it will go the same number backwards that means if I've set tab to 4 and I press Tab it shall go back by 4 chars after I've pressed Backspace.
It should also always use 4 spaces for tab. Because sometimes in emacs it does not do that.
Vim's tab handling can be configured, so it's not a good description of what you want to do, but the rest of your description has enough information, for the most part.
The easiest way to cope with tabs is never to use them. So don't be surprised if setting up tabs in the way you like them takes a bit of work.
You've set up the Tab key to insert a tab character. That's not the custom in Emacs: usually the Tab key is used to indent the current line. What you've done is enough for the default, but language-specific modes may still make Tab indent. I presume from your inclusion of c-basic-indent that you're working on C code; so you need to tell C mode that you don't want Tab to indent. This should do it:
(eval-after-load "cc-mode"
'(define-key c-mode-map (kbd "TAB") 'self-insert-command))
Another thing you've run into is that by default, the Backspace key tries to move back by one column rather than one character. The following should make it delete one character:
(global-set-key (kbd "DEL") 'backward-delete-char)
(setq c-backspace-function 'backward-delete-char)

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.