13

When I am in a CSS file with a blank line, like this:

enter image description here

and I want to simply make a new line and move down to it, by hitting Enter , vim is auto inserting a tab character, e.g.

enter image description here

Running :set fo? inside the CSS file => formatoptions=croql.
If I disable all formatoptions with :set fo= - it still happens.
however if format options are disabled using :set paste vim does stop inserting the tab - but I dont want to have to work in paste mode all the time!

settings:

  • noautoindent
  • nosmartindent
  • nocindent

If I turn off filetype specific indenting with

filetype indent off

It still happens, what am I missing - do I need to restart vim or something for the settings to take effect?

How can I stop vim autoinserting these tab characters?

1
  • Try if setting :setlocal expandtab helps Commented Oct 30, 2015 at 12:13

4 Answers 4

13

I found a couple of ways to solve the problem.

1. reload the buffer after running :filetype indent off

Vim's docs (:help :filetype-indent-off) provided an explanation:

You can disable :filetype indent on with:

:filetype indent off

This actually loads the file "indoff.vim" in 'runtimepath'. This disables auto-indenting for files you will open. It will keep working in already opened files. Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.

  • "It will keep working in already opened files." - explained that I simply had to kill and reload the buffer after I ran the command :filetype indent off

2. Reset indentexpr

per docs

Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.

it turned out the issue was caused by indentexpr which had the value of

indentexpr=GetCSSIndent()
Last set from /usr/share/vim/vim74/indent/css.vim

I was able to reset it with: :set indentexpr=""

which also fixed the problem.

2

This issue has already been answered, but I though I could also add my solution:

As suggested by filbranden, it's better to not modified vim's system files. Instead, he suggested to use skip_defaults_vim=1 or create a .vimrc file in my home directory to disable the default features. If I want any of the default features, I can add them to my .vimrc file.

In my case (vim 8.1), disabling the defaults does the trick. I like vim highlight feature, but that feature is defined elsewhere so disabling the defaults do not disable highlighting.

I added the following line at the beginning of /etc/vim/vimrc:

let skip_defaults_vim=1

----------- Old answer (not recommended) -----------

If you simply want to disable auto-indent system wise, for every file type (basically, disable the auto-indent feature completely), you can do the following:

  1. Backup the indent.vim file:
    sudo mv /usr/share/vim/vim81/indent.vim /usr/share/vim/vim81/indent.vim.orig
  2. Create a new empty indent.vim file:
    sudo touch /usr/share/vim/vim81/indent.vim

IMPORTANT NOTE: Modifying system files is not recommended. It can have unexpected side effect. Also, the modification is likely to be overwritten on the next vim update. Avoid this solution if possible.

5
  • Welcome to Vi and Vim! This will break as soon as you upgrade the Vim package in your system, which will overwrite this file back to its original contents... The indent.vim file is being loaded by the file type detection code, and you can disable indent support easily by adding filetype indent off to your vimrc file instead, that's a much better approach.
    – filbranden
    Commented Sep 18, 2020 at 2:35
  • Thanks filbranden. I wish that approach would work, that would be much cleaner. Unfortunately, at least on Ubuntu, adding filetype indent off at the end (or even at the beginning of the file) of /etc/vim/vimrc has no effect.
    – Gael
    Commented Sep 18, 2020 at 3:21
  • That's because filetype plugin indent on will be executed by $VIMRUNTIME/defaults.vim which is run if the user has no .vimrc in their home directory, and it runs after the system-wide vimrc. You should really use a user vimrc file and not the system one... If you want to disable the defaults.vim file, you can use let skip_defaults_vim = 1 in your system vimrc file (but note that defaults.vim sets up a lot of other useful stuff, such as syntax highlighting.) See :help defaults.vim for details.
    – filbranden
    Commented Sep 18, 2020 at 3:49
  • I have restored the /usr/share/vim/vim81/indent.vim file and verified that the auto-indent was back. Then I touch ~/.vimrc to create an empty .vimrc file. The auto-indent is gone! Thanks. NOTE: I tried this earlier (a few version of vim back, when I was running Debian). After creating an empty .vimrc in my home directory, I would lose a lot of features, such as highlighting which I like. I will edit my answer.
    – Gael
    Commented Sep 18, 2020 at 4:59
  • For years this automatic indention features harassing me. Now I can stop this 'feature'. Thank you. This is best answer. Any other anwsers can't help much.
    – hurelhuyag
    Commented Jan 21, 2021 at 9:44
1

Here is one option, first find your file type by typing :set filetype? within your Vim opened file. Suppose it spits out css, then in your .vimrc file, add the line

autocmd FileType css setlocal indentexpr=
6
  • Consider using ftplugins (~/.vim/after/ftplugin/css.vim) rather than autocommands.
    – D. Ben Knoble
    Commented Oct 18, 2021 at 14:12
  • @D.BenKnoble Thanks for the comment, could you clarify why it is preferable to use plugins? I tend to solve everything within .vimrc file because that way I have only one file to keep track of.
    – zyy
    Commented Oct 20, 2021 at 6:06
  • It’s reinventing the wheel, and may be slower. Vim already has a Filetype autocommand that will trigger all the ftplugin scripts. Adding more autocommands gives it more to do when it already has a mechanism for that. Besides, your vimrc can live in your .vim directory, so then you only have one directory to keep track of. I also find that its more organized and that it makes complex configurations easier, since the autocommand syntax can get a bit clumsy.
    – D. Ben Knoble
    Commented Oct 20, 2021 at 11:00
  • @D.BenKnoble Thanks for the explanation, however when you get a new laptop, you would have to remember to copy all the ftplugin scripts. Is there a way to automate?
    – zyy
    Commented Oct 21, 2021 at 3:34
  • 1
    Copy the single directory ~/.vim; that’s where all the configuration is. In fact you could version control this directory, or all of your dotfiles. I do this, along with a makefile (sort of) to create symlinks and install things.
    – D. Ben Knoble
    Commented Oct 21, 2021 at 11:24
-1

Yes this solved for me also this unkind effect:

I opened, and edited my ~/.vimrc

:filetype indent off

If you do not like the higlight scheme, you just search the selected colorscheme here:

/usr/share/vim/vim74/colors

i use generaly "colorscheme desert", in the 77-th row you find:

  hi Search cterm=NONE ctermfg=grey ctermbg=lightblue
  hi Search cterm=bold ctermfg=black ctermbg=green

that was for me better than the lightbue

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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