Transitioning to vim. Having issues with indentation

妖精的绣舞 提交于 2019-12-02 12:03:23

问题


I am recovering from a surgery, thusly, I am transitioning to VIM. As I starting point, I've opted to use the vim-sensible plugin as the foundation of my configuration. Additionally, I've installed the 'vim-rails' and 'vim-ruby' plugins. These are all loaded via Pathogen.

For whatever reason, the plugins are not adjusting indentation settings in accord with filetype. Tabs seem locked to 9 columns. I am able to set them manually, but obviously this is not ideal.

Here is my .vimrc. As you can see, it is very basic.

execute pathogen#infect()
syntax on
filetype plugin indent on

回答1:


Vim provides the following buffer local options for managing indention: 'softtabstop', 'shiftwidth', 'tabstop', and 'expandtab'. The idea is to set these options for a specific filetype, e.g. ruby either by using an autocommand or using the after ftplugin directory (my preference).

After directory approach

Add the following to your ~/.vim/after/ftplugin/ruby.vim file:

setlocal softtabstop=2
setlocal shiftwidth=2
setlocal tabstop=2

Autocommand approach

Add the following to your ~/.vimrc file:

augroup MyIndentSettings
  autocmd!
  autocmd FileType ruby setlocal softtabstop=2 shiftwidth=2 tabstop=2
augroup END

Learn more

As you are just starting to vim it is best to learn how to query these options so you can track down future bugs. Querying an option is as simple as :verbose set {option}?, e.g. :verbose set expandtab?.

You may also be interested in Tim Pope's vim-sleuth which heuristically sets indent settings.

For more help see:

:h :set
:h 'softtabstop'
:h 'shiftwidth'
:h 'tabstop'
:h 'expandtab'
:h after-directory



回答2:


In my vimrc I have the following:

set autoindent
set expandtabs
set shiftwidth=4
set tabstop=4

Actually I just have

set ai et sw=4 ts=4

You can change the specific settings using FileType or BufEnter:

autocmd BufEnter *.py ai et sw=4 ts=4


来源:https://stackoverflow.com/questions/27804353/transitioning-to-vim-having-issues-with-indentation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!