V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
AmitabhaPk
V2EX  ›  Vim

求教根据不同文件类型,自动切换缩紧

  •  
  •   AmitabhaPk · 2021-01-09 20:00:45 +08:00 · 1958 次点击
    这是一个创建于 1194 天前的主题,其中的信息可能已经有所发展或是发生改变。
    各位大佬,求教如何配置 vimrc,根据 filetype 匹配缩紧类型

    比如在 kernel 写代码时,要用 TAB 缩进,但在应用层写 cpp 代码要求 4 个空格缩紧,不会配置,每次切换时 都要手动修改 vimrc 。。。。。
    7 条回复    2021-01-10 10:24:42 +08:00
    odirus
        1
    odirus  
       2021-01-09 20:03:54 +08:00
    yzbythesea
        2
    yzbythesea  
       2021-01-09 20:04:57 +08:00
    autocmd Filetype cpp set softtabstop=4
    zhanglintc
        3
    zhanglintc  
       2021-01-09 20:22:40 +08:00
    这是我的,正常是 4 个空格缩进,如果是 ruby 、html 等,就是 2 个缩进。
    你看着改改就行了。

    " general indent setting
    set tabstop=4 " ts
    set softtabstop=4 " sts
    set shiftwidth=4 " sw
    set expandtab
    set smarttab

    " languages specific indent setting
    autocmd FileType ruby,html,xml,smarty setlocal ai ts=2 sts=2 sw=2

    ai => autoindent
    ts => tabstop
    sts => softtabstop
    sw => shiftwidth
    AmitabhaPk
        4
    AmitabhaPk  
    OP
       2021-01-09 20:23:09 +08:00
    @yzbythesea 我刚才设置成 tabstop=4 expendtab 就好了 多谢!
    AmitabhaPk
        5
    AmitabhaPk  
    OP
       2021-01-09 20:24:37 +08:00
    @zhanglintc 哈哈 收了 现在还不会这么多。。。。
    AmitabhaPk
        6
    AmitabhaPk  
    OP
       2021-01-09 20:28:57 +08:00
    @odirus 这个很好 读一下配置上
    IgniteWhite
        7
    IgniteWhite  
       2021-01-10 10:24:42 +08:00
    我的 https://github.com/g6ai/dotfiles

    " Tab
    filetype indent on
    set expandtab " Always use space instead of tab
    autocmd FileType vim setlocal expandtab tabstop=2 shiftwidth=2 softtabstop=2
    autocmd FileType sh setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4
    autocmd FileType python setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4
    autocmd FileType tex setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4

    nathanaelkane/vim-indent-guides 这个插件也不错
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3763 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:29 · PVG 18:29 · LAX 03:29 · JFK 06:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.