16

I am trying to make vim auto indent with tabs, and align with whitespaces, but actually, vim indent with tabs and fill the alignment with tabs + white spaces.

Vim does it :

enter image description here

(each block is a tab)

{
>--long a = 1,
>-->--..b = 2,
>-->--..c = 3;
}

May do it :

{
>--long a = 1,
>--.....b = 2,
>--.....c = 3;
}

So, I think there is a lot of questions about the subject of tabs and spaces, but none of them provide some Just Works solution:

Actually I am using:

set ai
set cindent
set cinoptions=(0,:0,u0,U0,g0)

I tried the Smart Tab plugin, which seems to work well, but when I try gg=G I get some weird errors :

Error detected while processing function CtabRedoIndent..<SNR>12_CheckAlign:
line   11:
E108: No such variable: "b:ctab_lastalign"
Press ENTER or type command to continue
Error detected while processing function CtabRedoIndent:
line   10:
E170: Missing :endwhile

This discussion seems to be prety old, so now, 2015, there is a official solution to deal with it ?

  • 3
  • I don't have a mapping for this, just an idea...but what if you set Vim to insert tabs (of whatever width you like) when you press the tab key, but actually expand a tab to spaces if you type Shift-Tab? Wouldn't that make this sort of alignment simple? – Wildcard Jan 31 '16 at 7:58
  • (P.S.: There's a nice discussion on "Tabs vs. Spaces" in the "Religious Wars" forum of the xkcd comic. Enjoy ;) – Wildcard Jan 31 '16 at 8:05
3

Doing this truly automatically is difficult. Vim doesn't know what is "indentation" and what is "alignment"; and some people may disagree on what exactly constitutes indentation and alignment.

That being said, the Smart Tabs plugin is designed to do what you want; I found that it works reasonably well for C files, but has a number of caveats; it breaks Python files, for example, and does some unexpected (for me, anyway) things for C files at times.
Looking at the source, I can't really figure out how it works in 5 minutes, and combined with the fact that it breaks some stuff, should tell you to proceed carefully...

2

The Smart Tabs plug-in that you mentioned is about as good as it gets. I initially ran into the same error that you did and gave up but eventually spent a few minutes to fix it. Try applying the following patch:

--- ctab.vim.orig   2016-08-11 17:05:20.767275274 +0800
+++ ctab.vim    2016-08-11 17:15:20.039286525 +0800
@@ -221,7 +221,7 @@
       if a:line == line('.')
         let b:ctab_lastalign=a:line
       else
-        unlet b:ctab_lastalign
+        unlet! b:ctab_lastalign
       endif
       set ts=50
       set sw=50

I later found that someone has suggested a similar fix in the Smart Tabs wiki section. Alas, there hasn't been a new release.

0

Indentation in vim is performed simply using the following script

Script to perform Indentation of a single file

#!/bin/bash 
#Author: Ankit shah
#Enter file name to indent as argument
file=$1
vim $file -c "normal gg=G" -c "wq"
#Use expand command to turn tabs into spaces. Code uniform across different vim environment
expand -t 4 $file $file 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.