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)
Related
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.
I'm attempting to switch from Vim to Emacs, but I'm tearing my hair out trying to configure it to treat tabs how I wish. I require:
Inserted "tabs" to be expanded into two spaces. Emacs stubbornly sticks to eight, no matter what I do.
Tabs (i.e. real \t characters) to be represented on screen by two spaces.
Pressing TAB should insert a tab at the cursor rather than indent the entire line. Currently, I press TAB anywhere and Emacs destroys all whitespace at the start of the line; this is the most infuriating thing so far.
My current ~/.emacs reads
(setq standard-indent 2)
(setq-default indent-tabs-mode nil)
but I have tried no end of suggested configurations from the web, none of which have done what they said they would. (Does the API constantly change? I'm using GNU Emacs 23.1.1, apparently.)
Emacs has extremely flexible support for handling indentation. Generally the mode that you are in dictates how they work - so if you're working on a C file then the way that pressing tab works will be different than if you're working on a Python file.
So it does depend which mode you're working in, which will limit the answers you get. In most cases I would recommend that you don't fight against it - for me the indentation behaviour is one of the best features of emacs. However, you do need to spend the time to customize it for yourself.
To change the way that tabs are displayed you need to set tab-width to 2. If you're editing Java or C style code then it sounds like you want to turn off all the nice indentation features by these to NIL:
c-tab-always-indent
c-syntactic-indentation
indent-tabs-mode
I suggest you set these through the customization interface. If you use "M-x customize-group RET C" then you can see the various settings for C mode.
If you're editting different types of files then the instructions will be different.
Perhaps emacs is in the wrong mode for your file. You could try doing "M-x fundamental-mode" to see if you prefer the behaviour there.
;; * Inserted "tabs" to be expanded into two spaces. Emacs stubbornly
;; sticks to eight, no matter what I do.
;; * Tabs (i.e. real \t characters) to be represented on screen by two
;; spaces.
(setq-default tab-width 2)
;; * Pressing TAB should insert a tab at the cursor rather than indent
;; the entire line. Currently, I press TAB anywhere and Emacs
;; destroys all whitespace at the start of the line; this is the
;; most infuriating thing so far.
(setq-default indent-tabs-mode t)
(mapcar (lambda (hooksym)
(add-hook hooksym
(lambda ()
(kill-local-variable 'indent-tabs-mode)
(kill-local-variable 'tab-width)
(local-set-key (kbd "TAB") 'self-insert-command))))
'(
c-mode-common-hook
;; add other hook functions here, one for each mode you use :-(
))
;; How to know the name of the hook function? Well ... visit a file
;; in that mode, and then type C-h v major-mode RET. You'll see the
;; mode's name in the *Help* buffer (probably on the second line).
;; Then type (e.g.) C-h f python-mode; you'll see blather about the
;; mode, and (hopefully) somewhere in there you'll see (again e.g.)
;; "This mode runs the hook `python-mode-hook', as the final step
;; during initialization."
This should get you most of what you want. You'll probably have to customize some other programming modes you commonly use.
(defun insert-tab ()
"self-insert-command doesn't seem to work for tab"
(interactive)
(insert "\t"))
(setq indent-line-function 'insert-tab) ;# for many modes
(define-key c-mode-base-map [tab] 'insert-tab) ;# for c/c++/java/etc.
(setq-default tab-width 2)
The title of my question is a reference to sane tabs in emacs.
Basically what I want is to globally set tabs and indention to work in
some uniform way. I feel like emacs is so much better than TextMate
or BBEdit but really the way they handle indention is simple and great
for my purposes. In emacs if you use some tab/space scheme that's
different than the scheme enforced by a minor mode you use you're in
trouble.
When I press enter I'd like to be moved to the next line indented to
the right place using tabs. If I can have my cake and eat it too I'd
like to be indented using spaces if the rest of the file is composed
that way.
I've tried these also:
doing tabs in emacs
force emacs to use tabs
Thanks to anyone who can help me achieve this.
-Mike
Perhaps (global-set-key (kbd "RET") 'newline-and-indent) is what you want?
(Or reindent-then-newline-and-indent if that's available, or you could just hit C-j instead of the Enter key.)
For this part of your question:
If I can have my cake and eat it too I'd like to be indented using spaces if the rest of
the file is composed that way.
does this do what you want?
(defun dtrt-indent ()
(setq indent-tabs-mode
(save-excursion
(goto-char (point-min))
(search-forward "\t" nil t))))
(add-hook 'text-mode-hook #'dtrt-indent)
(add-hook 'c-mode-hook #'dtrt-indent)
; etc for all modes you care about
So if there's a tab anywhere in the buffer, indent using tabs; if there is no tab, indent using spaces.
if you do your setup as described:
(setq indent-tabs-mode t)
(setq-default indent-tabs-mode t)
(setq tab-width 4) ;; 8 is way too many
(setq default-tab-width 4) ;; 8 is way too many
(global-set-key (kbd "RET") 'newline-and-indent)
The indent-tabs-mode thing will tell emacs to create your indentation by using TABS and SPACES to make up the desired indentation (defined by the individual mode). This means, if you want to have a TAB inserted instead of TABS/SPACES you need to configure your mode to use tab-width as indentation.
For example if you use c-mode and select cc-mode as indentation style (select with C-c .) which uses 4 as indentation value, newline-and-indent will insert spaces.
To conclude:
Check that your mode uses tab-width as indentation
Check that your mode doesn't overwride indent-tabs-mode (python-mode seems to do this)
Although I personally don't like TABS good luck on your journey :)
The best strategy is to convince your programming mode of choice to
indent things the way you like. This is generally very easy; I am
picky about indentation and my emacs always does the right thing
automatically. (That means that "indent-region" also always does
what I want, which is very convenient.)
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
I want to override the bad default tabbing scheme in emacs so that it will work like most other editors (eclipse, notepad++). I want to set it so that regardless of mode, tab will insert a tab, and pressing enter will keep me at my current tab depth.
I tried this, but it does nothing:
(global-set-key (kbd "TAB") 'tab-to-tab-stop)
(setq default-tab-width 4) ;; 8 is way too many
To make the Enter key take you to the next line and indent it automatically, you can put
(global-set-key (kbd "RET") 'newline-and-indent)
in your .emacs. [Or you can hit C-j instead of Enter.] Once you have that, you'll never need to insert tabs manually, because Emacs automatically indents a new line to extra depth after an opening brace, etc. If you do want to change the indentation, you can hit TAB until it takes you to the right indentation, then start typing from there. [And when you type a closing brace, Emacs is smart enough to take that brace one indentation level backwards.]
You should remove the (global-set-key (kbd "TAB") 'tab-to-tab-stop) for this to work.
Many major modes override the TAB binding, for example cc-mode binds TAB to 'c-indent-to-column.
The 'global-set-key that is suggested does nothing as almost every major mode has overridden the TAB.
One trick that might work for you is to copy the approach that 'pabbrev uses, and define a global minor mode that has the TAB bound. You could do that like so:
(defvar just-tab-keymap (make-sparse-keymap) "Keymap for just-tab-mode")
(define-minor-mode just-tab-mode
"Just want the TAB key to be a TAB"
:global t :lighter " TAB" :init-value 0 :keymap just-tab-keymap
(define-key just-tab-keymap (kbd "TAB") 'indent-for-tab-command))
However, this disables all TAB completion. You'll probably get best results by overriding each of the major-modes one by one (so as to avoid mussing up TAB completion).
This bugged me, too, when I first started using Emacs. I've come to love it, though. If I want to indent appropriately, I hit <tab>; if I want to insert a literal tab, I hit M-i (Meta and 'i' or <Alt>-<i> in some parlances) which is bound to tab-to-tab-stop.
I think trey jackson's answer is probably what you want, except possibly use 'self-insert-command instead of 'indent-for-tab-command. I personally prefer emacs' default behavior, but self-insert-command does what it says instead of trying to do anything fancy like make sure your code is well-formatted.
The few times I actually want to insert a tab (not indent) I press M-i.
You may be interested in this minor mode I created at http://github.com/vohrta/regtab.
It makes it so that when you press the tab key either a tab character (if indent-tabs-mod is not nil) or tab-width spaces will be placed at point. The mode is also capable of handling what you may consider regular behavior on a region of selected text and shift-tabbing to remove tabs at the beginning of the line (or set of lines).
You can enable or disable it at any time by pressing M-x regtab-mode.
C-j does the newline + indent functionality that you want out of pressing Enter.