Tab Vs Space preferences in Vim

前端 未结 7 1073
眼角桃花
眼角桃花 2020-12-07 07:31

Vim is very accommodating when it comes to tab Vs. space preferences. As I understand it, the tabstop setting indicates the width of a tab character. The

7条回答
  •  生来不讨喜
    2020-12-07 07:53

    Creating a stab option in Vim itself would not be easy, but I've whipped up this command/function that you can drop in your .vimrc (or a plugin file if you're super-organized). Use :Stab and you will be prompted for an indent level and whether or not to use expandtab. If you hit enter without giving it a new indent level, it will just print the current settings.

    " put all this in your .vimrc or a plugin file
    command! -nargs=* Stab call Stab()
    function! Stab()
      let l:tabstop = 1 * input('set shiftwidth=')
    
      if l:tabstop > 0
        " do we want expandtab as well?
        let l:expandtab = confirm('set expandtab?', "&Yes\n&No\n&Cancel")
        if l:expandtab == 3
          " abort?
          return
        endif
    
        let &l:sts = l:tabstop
        let &l:ts = l:tabstop
        let &l:sw = l:tabstop
    
        if l:expandtab == 1
          setlocal expandtab
        else
          setlocal noexpandtab
        endif
      endif
    
      " show the selected options
      try
        echohl ModeMsg
        echon 'set tabstop='
        echohl Question
        echon &l:ts
        echohl ModeMsg
        echon ' shiftwidth='
        echohl Question
        echon &l:sw
        echohl ModeMsg
        echon ' sts='
        echohl Question
        echon &l:sts . ' ' . (&l:et ? '  ' : 'no')
        echohl ModeMsg
        echon 'expandtab'
      finally
        echohl None
      endtry
    endfunction
    

提交回复
热议问题