Search code examples
vimeditorconfig

EditorConfig/vim: tab character lost if alone on line


I'm using the vim plugin to EditorConfig with these settings:

let g:EditorConfig_exclude_patterns = ['fugitive://.*']
let g:EditorConfig_core_mode = "external_command"
let g:EditorConfig_preserve_formatoptions = 1

I'm on Ubuntu and installed EditorConfig with:

sudo apt-get install editorconfig

I'm editing a source code file in an open source project that has lines like this (">" indicates tab character):

// code
>
// code
>   
// code

I find that when I save the file, EditorConfig is stripping out the tab characters that are alone on a line.

// code

// code

// code

The .editorconfig for the project looks like this:

root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# npm is using 2 spaces when modifying package.json
[package.json]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false

I've looked through the EditorConfig documentation but I can't find a way to keep the stray tab characters.

I understand that these stray tab characters are meaninless and the code is actually cleaner without them, but I don't want to make extra edits to files that I am trying to patch.

Has anyone else had this issue?


Solution

  • Drop the

    trim_trailing_whitespace = true
    

    from your config. "Trailing" does not necessarily mean that there's actual characters before the whitespace in a line; it also affects all-whitespace lines, too.

    Applying for certain projects / repositories only

    If you need different settings for different directories, you'd use :autocmd inside Vim to vary the option values, or use a full-blown local vimrc plugin.

    I don't know much about EditorConfig, but its home page reads:

    When opening a file, EditorConfig plugins look for a file named .editorconfig in the directory of the opened file and in every parent directory.

    So, if you need to configure this differently for one repository, just put such file into the repository's root dir, and reconfigure the trim_trailing_whitespace option there.