neatly indented folds in vim

If, like me, you use tabs for indenting your code, then you may have been annoyed to find that the amazing folds capability in vim indents its collapsed lines by default with spaces.

An example with bad indents:

      for(var b=0,n=nodeList[a].childNodes.length;b<n;++b){
        var node=nodeList[a].childNodes[b];
        switch(node.tagName){
     case 'A': {-------------------------------------------------------------------------------------------------------------------------------
     case 'UL': {------------------------------------------------------------------------------------------------------------------------------
        }
      }
 

Well, I decided to RTFM, and here is how to correct it.

Edit /usr/share/vim/vim63/syntax/javascript.vim and replace the following line:

set foldtext=getline(v:foldstart)

With this one:

set foldtext=substitute(getline(v:foldstart),'\ ','\ \ ','g')

Note that the first escaped character there is a tab – not a space.

With the above code in place, the broken indentation is easier to read:

      for(var b=0,n=nodeList[a].childNodes.length;b<n;++b){
        var node=nodeList[a].childNodes[b];
        switch(node.tagName){
          case 'A': {--------------------------------------------------------------------------------------------------------------------------
          case 'UL': {-------------------------------------------------------------------------------------------------------------------------
        }
      }
 

Comments are closed.

%d bloggers like this: