all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Setting up Emacs tabs like my Vim config
@ 2013-12-30 18:42 Some Developer
  2013-12-30 20:19 ` William G. Gardella
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Some Developer @ 2013-12-30 18:42 UTC (permalink / raw)
  To: help-gnu-emacs

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

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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Setting up Emacs tabs like my Vim config
  2013-12-30 18:42 Setting up Emacs tabs like my Vim config Some Developer
@ 2013-12-30 20:19 ` William G. Gardella
       [not found] ` <mailman.10722.1388434857.10748.help-gnu-emacs@gnu.org>
  2013-12-30 20:49 ` Bob Proulx
  2 siblings, 0 replies; 4+ messages in thread
From: William G. Gardella @ 2013-12-30 20:19 UTC (permalink / raw)
  To: help-gnu-emacs

Some Developer <someukdeveloper@gmail.com> 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.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Setting up Emacs tabs like my Vim config
       [not found] ` <mailman.10722.1388434857.10748.help-gnu-emacs@gnu.org>
@ 2013-12-30 20:39   ` Damien Wyart
  0 siblings, 0 replies; 4+ messages in thread
From: Damien Wyart @ 2013-12-30 20:39 UTC (permalink / raw)
  To: help-gnu-emacs

* "William G. Gardella" <wgg2@member.fsf.org> in gnu.emacs.help:
> 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---

I do not think this will be equivalent to the OP's vim settings, because
expandtab inserts spaces instead of a tab.

So I would rather go with

(setq tab-width 4
      indent-tabs-mode nil)

but this doesn't get the effect of backspace going back to the previous
tab stop. I do not know a way to get this when using spaces (when using
tabs, this can be approached with
http://www.emacswiki.org/emacs/BackspaceWhitespaceToTabStop)

To get something stronger that the default of DEL removing one space,
hungry delete might be used, but it will often be too strong (removing
all whitespace instead of going back to previous tab stop). Hungry
delete from CC mode can be used, or there is also a dedicated minor
mode :
http://stackoverflow.com/questions/5045820/how-to-bind-hungry-delete-for-clojure-in-emacs
https://github.com/nflath/hungry-delete

delete-horizontal-space is also somehow usable, but with a dedicated key
sequence (C-u M-\).

-- 
DW


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Setting up Emacs tabs like my Vim config
  2013-12-30 18:42 Setting up Emacs tabs like my Vim config Some Developer
  2013-12-30 20:19 ` William G. Gardella
       [not found] ` <mailman.10722.1388434857.10748.help-gnu-emacs@gnu.org>
@ 2013-12-30 20:49 ` Bob Proulx
  2 siblings, 0 replies; 4+ messages in thread
From: Bob Proulx @ 2013-12-30 20:49 UTC (permalink / raw)
  To: Some Developer; +Cc: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-12-30 20:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-30 18:42 Setting up Emacs tabs like my Vim config Some Developer
2013-12-30 20:19 ` William G. Gardella
     [not found] ` <mailman.10722.1388434857.10748.help-gnu-emacs@gnu.org>
2013-12-30 20:39   ` Damien Wyart
2013-12-30 20:49 ` Bob Proulx

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.