Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 780
rob Avatar asked Jun 24 '11 17:06

rob


People also ask

How do I set the tab length in Emacs?

With tab-width equal to the default value of 8, Emacs would insert 1 tab plus 2 spaces. Use t rather than nil to tell Emacs to use tab characters where appropriate. If you only want this in a particular mode, add (setq indent-tabs-mode nil) to that mode's hook.

How do you add a tab character in Emacs?

To manually insert a tab in Emacs, use ctrl-Q TAB. control-Q causes the next key to be inserted rather than interpreted as a possible command.

How many spaces is a tab in Emacs?

This is because standard tabs are set to eight spaces. Tabs are special characters.


1 Answers

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)
like image 154
Gilles 'SO- stop being evil' Avatar answered Sep 27 '22 22:09

Gilles 'SO- stop being evil'