changing tabstop from 4 to 2 after using expandtab (with minimum manual effort)

StackOverflow https://stackoverflow.com/questions/3518549

  •  29-09-2019
  •  | 
  •  

문제

For some time I have used tabstop=4 in my source files. Now that I write lot of javascript that has deep indentations, a tabstop of 4 seems wasteful, so I want to convert it to 2. The problem is I use "set expandtab" too. So merely setting tabstop=2, won't help.

Any suggestions on how I can convert all my files quickly to tabstop 2?

My current relevant .vimrc settings are

set tabstop=4
set shiftwidth=4
set expandtab
도움이 되었습니까?

해결책

You can do

" convert spaces to tabs first
set noexpandtab
set tabstop=4
set shiftwidth=4
retab!
" now you have tabs instead of spaces, so insert spaces according to
" your new preference
set tabstop=2
set shiftwidth=2
set expandtab
retab!

in a vim window, and it will retab according to your preferences. You can probably create a mapping to make this easier, or record a macro. There might be another way, but the above should work. (" is comment)

다른 팁

You can change your settings, select all, and hit the = key to indent it:

ggVG=

I also recommend using ftplugin to specify settings per file type.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top