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: Bob Proulx
Subject: Re: Setting up Emacs tabs like my Vim config
Date: Mon, 30 Dec 2013 13:49:54 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

Some Developer wrote:
> I'm playing around with Emacs for the first time at the moment

Welcome!

> 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.

Tabs are a troubled topic everywhere.  Sigh.  You will probably get
many answers to this question.

> I've read the manual on it but it seems like Emacs makes this
> unnecessarily hard. Here is my current Vim config:

Here is the upstream manual entry on it.

  
http://www.gnu.org/software/emacs/manual/html_node/emacs/Indentation.html#Indentation

And of course inside emacs you can easily read the documentation
matching your installed version with C-h i m emacs to jump right to
the emacs manual.  Then I usually simply hit 's' to search and type in
a search string.  Here the topic is "indentation".

The EmacsWiki page on this is:

  http://www.emacswiki.org/emacs/IndentationBasics

As you can see in the discussion on the wiki this topic is neither
simple nor settled even after decades.

> " Set proper tab / spacing settings
> set expandtab
> set smarttab
> set shiftwidth=4
> set tabstop=4
> set softtabstop=4

Can I say that I really hate it when people change the standard width
of tabs to something nonstandard that isn't eight?  It makes
interoperating with others difficult because everyone will see a
different result.  Sigh.  It is for this reason that most projects
have gone to a policy of forbidding using tabs at all and requiring
all indention to be spaces.  Grr...  (The tabstop=4 above is the
problem.  Should have stopped at softtabstop=4.  However I do see
expandtab so that there isn't propagation of tab characters to the
file.  The shiftwidth=4 doesn't affect tab stops and is okay.)

This is different from what column the cursor moves to when you press
the TAB key.  The TAB key moves to the next tab position.  The editor
then determines if it should use an ASCII TAB character or a
combination of TABs and spaces to arrive at that column.  Changing the
offset is always okay.  I only object to changing the hard tab width.
I personally like tabs but people changing the standard width of them
has killed being able to use them as they were intended.

> Does anyone know what an equivalent configuration would be in Emacs?
> I'm using 24.3.1 if that makes any difference.

If you really, really, really must change the tab-width to 4 then the
following.  This is the same as your above vim configuration.

  (setq-default indent-tabs-mode nil) ; no tab characters in files
  (setq-default tab-width 4)

Also, indentation is usually language specific.  It is good to keep
the original style of the file you are editing.  Often this is 2 for
one file and 4 for another file and so forth.  Instead of globally
changing it I think it is better to do customizations in the desired
modes only.

This is often set in a mode hook for a specific mode.  And specific
modes often have specific indention capabilities.  For example C mode
has c-basic-offset and sh mode has sh-basic-offset and so forth.  I
set the following for sh mode for example.

  (setq sh-basic-offset 2)

Or for text-mode I like the StackOverflow answer.  But I set it in a
mode hook to be buffer local to that mode.

  ;; From 
http://stackoverflow.com/questions/69934/set-4-space-indent-in-emacs-in-text-mode
  (add-hook 'text-mode-hook
          (lambda ()
            (abbrev-mode 1)
            (auto-fill-mode 1)
            (setq tab-stop-list (number-sequence 4 200 4))))

Personally when I want a custom indentation for a programming language
I always set it in a mode specific way.  Such as using c-set-style for
C files or sh-basic-offset for shell scripts or cperl-indent-level for
perl and so forth.

Other good related hints are that you can use emacs 'untabify' on a
buffer and remove all tab characters.  Also the shell 'expand -t4'
command.  To change tab sizes in a file.  'expand -t4 | unexpand' will
reset a file's hard tab characters from a non-standard 4 back to a
standard size 8.

Bob



reply via email to

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