How to avoid indentation error after changing tab settings in Vim?

I used to have 8-space tabs in Vim. Then I changed to 4 spaces, but now, when I add a line to some code that I wrote before going to 4 spaces, it gives me the indentation mismatch error, even if everything works out well. Is there any way to avoid this problem?

+3
source share
4 answers

Have you changed only the tabstop option?

I use 4 spaces (fill in the spaces when I click the tab to insert the actual hit tab ctrl-v tab). The following are the settings associated with the .vimrc tab:

" tabs
set tabstop=4
set shiftwidth=4
set expandtab

, , .

, -, , (8,4,3,5) .

( vim 7.1 help tabstop):

    Note: Setting 'tabstop' to any other value than 8 can make your file
    appear wrong in many places (e.g., when printing it).


    There are four main ways to use tabs in Vim:
    1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4
       (or 3 or whatever you prefer) and use 'noexpandtab'.  Then Vim
       will use a mix of tabs and spaces, but typing <Tab> and <BS> will
       behave like a tab appears every 4 (or 3) characters.
    2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use
       'expandtab'.  This way you will always insert spaces.  The
       formatting will never be messed up when 'tabstop' is changed.
    3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a
       |modeline| to set these values when editing the file again.  Only
       works when using Vim to edit the file.
    4. Always set 'tabstop' and 'shiftwidth' to the same value, and
       'noexpandtab'.  This should then work (for initial indents only)
       for any tabstop setting that people use.  It might be nice to have
       tabs after the first non-blank inserted as spaces if you do this
       though.  Otherwise aligned comments will be wrong when 'tabstop' is
       changed.
+9

:%retab...?

+5

python , , :

:set tabstop=8
:set shiftwidth=4
:set expandtab

, - 8- " ", . , , . : .

4 ,

:set tabstop=4
:retab
:set tabstop=8

, 4 .

, , , - , , , , .

+3

: , .

set listchars=tab:>-,trail:-,nbsp:+ "This is terminal friendly but you can make fancy
set list

, python, . , .

" ". theres python -tt, , , , http://www.python.org/dev/peps/pep-0008/. , .

You can include this in your device testing. Pep seems to recommend 4 spaces for newer code. You might want to consider changing if you intend to participate in open source projects.

must also be superfluous to remove the space in eol

nnoremap <leader>wd :%s/\s\+$//<cr>
+2
source

Source: https://habr.com/ru/post/1708617/


All Articles