Tab key == 4 spaces and auto-indent after curly braces in Vim

后端 未结 11 1310
自闭症患者
自闭症患者 2020-11-27 09:01

How do I make vi-Vim never use tabs (converting spaces to tabs, bad!), makes the tab key == 4 spaces, and automatically indent code after curly brace blocks like Emacs does?

相关标签:
11条回答
  • 2020-11-27 09:17

    On many Linux systems, like Ubuntu, the .vimrc file doesn't exist by default, so it is recommended that you create it first.

    Don't use the .viminfo file that exist in the home directory. It is used for a different purpose.

    Step 1: Go to your home directory

    cd ~

    Step 2: Create the file

    vim .vimrc

    Step 3: Add the configuration stated above

    filetype plugin indent on
    set tabstop=4
    set shiftwidth=4
    set expandtab
    

    Step 3: Save file, by pressing Shift + ZZ.

    0 讨论(0)
  • 2020-11-27 09:24

    From the VIM wiki:

    :set tabstop=4
    :set shiftwidth=4
    :set expandtab
    
    0 讨论(0)
  • 2020-11-27 09:24

    The auto-indent is based on the current syntax mode. I know that if you are editing Foo.java, then entering a { and hitting Enter indents the following line.

    As for tabs, there are two settings. Within Vim, type a colon and then "set tabstop=4" which will set the tabs to display as four spaces. Hit colon again and type "set expandtab" which will insert spaces for tabs.

    You can put these settings in a .vimrc (or _vimrc on Windows) in your home directory, so you only have to type them once.

    0 讨论(0)
  • 2020-11-27 09:29

    The best way to get filetype-specific indentation is to use filetype plugin indent on in your vimrc. Then you can specify things like set sw=4 sts=4 et in .vim/ftplugin/c.vim, for example, without having to make those global for all files being edited and other non-C type syntaxes will get indented correctly, too (even lisps).

    0 讨论(0)
  • 2020-11-27 09:30

    The recommended way is to use filetype based indentation and only use smartindent and cindent if that doesn't suffice.

    Add the following to your .vimrc

    set expandtab
    set shiftwidth=2
    set softtabstop=2
    filetype plugin indent on
    

    Hope it helps as being a different answer.

    0 讨论(0)
提交回复
热议问题