help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Setting up Emacs tabs like my Vim config


From: William G. Gardella
Subject: Re: Setting up Emacs tabs like my Vim config
Date: Mon, 30 Dec 2013 20:19:56 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Some Developer <address@hidden> writes:

> I'm playing around with Emacs for the first time at the moment and so
> far things seem to be working well. The only problem I have with Emacs
> is how it deals with tabs and indentation in general.
>
> I've read the manual on it but it seems like Emacs makes this
> unnecessarily hard. Here is my current Vim config:
>
> " Set proper tab / spacing settings set expandtab set smarttab set
> shiftwidth=4 set tabstop=4 set softtabstop=4

Indentation is complex in Emacs, as it uses a mix of tabs and spaces by
default and it is common for different major modes to supply their own
functions and variables for indentation.  Therefore, you'd need to enter
each mode's local enivronment to really change the effect of the TAB key
where you expect.

A rough equivalent to your code could be something like this: 

--8<---------------cut here---------------start------------->8---
(defun tab-is-tab-is-tab ()
  "Configure tab-related settings and ensure that TAB is
`tab-to-tab-stop' in the local map."

  (setq tab-width 4
        tab-stop-list (loop for i to 120 by 4 collect i)
        indent-tabs-mode t)
  (local-set-key (kbd "TAB") 'tab-to-tab-stop))
--8<---------------cut here---------------end--------------->8---

This is intended to plug in to the local variable settings and keymap of
a buffer in a particular major mode.  We can add it to a bunch of modes
at once by hooking to the modes they inherit from; e.g. programming
modes generally inherit from `prog-mode' and natural language
composition modes from `text-mode'.

--8<---------------cut here---------------start------------->8---
(add-hook 'prog-mode-hook 'tab-is-tab-is-tab)
(add-hook 'text-mode-hook 'tab-is-tab-is-tab)
--8<---------------cut here---------------end--------------->8---

This will also leave the binding of TAB available in special modes like
shell-mode, irc clients, and comint-derived modes (usually REPLs or
interfaces to interactive processes), which often use TAB for shell-like
completion.

P.S. You can use M-x tabify to convert existing code from spaces to
tabs, although a bulk processing tool like "astyle" is often a better
choice if you have to re-indent a lot of code at once, or if the goal is
to make indentation uniform across several people's contributions.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]