sponsor Vim development Vim logo Vim Book Ad

Smart Tabs : Use tabs for indent, spaces for alignment

 script karma  Rating 377/133, Downloaded by 9709  Comments, bugs, improvements  Vim wiki

created by
Michael Geddes
script type
There are many different arguments about tabs and stuff.  My current personal preference is a choice of tabbing that is independent of anybody's viewing settings.  

For the beginning of the line, this means we can use <tabs> which will expand whatever the reader wants it to.  Trying to line up tabs at the end of the line is a little trickier, and making a few assumptions, my preference is to use spaces there.

This script allows you to use your normal tab settings for the beginning of the line, and have tabs expanded as spaces anywhere else.  This effectively distinguishes 'indent' from 'alignment'.  

<tab>  Uses editor tab settings to insert a tab at the beginning of the line (before the first non-space character), and inserts spaces otherwise.
<BS>  Uses editor tab settings to delete tabs or 'expanded' tabs ala smarttab

:RetabIndent[!] [tabstop]
    This is similar to the :retab command, with the exception that it  affects all and only whitespace at the start of the line, changing it to suit your current (or new) tabstop and expandtab setting.  With the bang (!) at the end, the command also strips trailing  whitespace.

CAUTION: After 1.1, this is no longer a filetype plugin by default ( set g:ctab_filetype_maps=1 to override)

Using spaces for alignment of code under unmatched brackets is now supported.
install details
Pre 1.1:  Use as an ftplugin, especially for c, cpp, idl files.  By this, I mean place the script in (say) the macros directory, and then source it from a ftplugin for c, cpp or idl (~/.vim/ftplugin/c.vim).
>= 1.1 :  Use as a general plugin.  (Doesn't include <m-;> mappings)

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
ctab.vim 2.6 2011-02-08 7.0 Michael Geddes Fix issue with alignment not disappearing
ctab.vim 2.5 2010-10-30 7.0 Michael Geddes Fixes an issue with aligning when pressing <CR>
ctab.vim 2.4 2010-08-23 7.0 Michael Geddes Bugfix
ctab.vim 2.3 2010-03-03 6.0 Michael Geddes - Fix (Gene Smith) for error with non C files
- Add option for filetype maps
- Allow for lisp indentation
ctab.vim 2.1 2009-06-04 6.0 Michael Geddes Small fix to equal operation
ctab.vim 2.0 2009-06-03 6.0 Michael Geddes Added support (after many requests) for using tabs to expand alignment for mismatched brackets etc.
ctab.vim 1.4.1 2005-10-11 6.0 Michael Geddes Changed to unix fileformat (thanks Peter)
ctab.vim 1.4 2003-12-15 6.0 Michael Geddes Fixed Backspace tab being off by 1
ctab.vim 1.3 2003-12-09 6.0 Michael Geddes 1.1: - Added support for backspacing over spaced tabs \\'smarttab\\' style
     - Clean up the look of it by blanking the :call
     - No longer a \\'filetype\\' plugin by default.
1.2: - Interactions with \\'smarttab\\' were causing problems. Now fall back to
       vim\\'s \\'smarttab\\' setting when inserting \\'indent\\' tabs.
     - Fixed compat with digraphs (which were getting swallowed)
     - Made <BS> mapping work with the \\'filetype\\' plugin mode.
     - Make CTabAlignTo() public.
1.3: - Fix removing trailing spaces with RetabIndent! which was causing
       initial indents to disappear.

ctab.vim 1.0 2003-12-07 6.0 Michael Geddes Added RetabIndent command - similar to :retab, but doesn't cause internal tabs to be modified.
ctab.vim 0.1 2002-02-25 6.0 Michael Geddes Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.