Program Tip

탭을 4 개의 공백으로 재정의

programtip 2020. 9. 27. 13:47
반응형

탭을 4 개의 공백으로 재정의


내 현재 설정은 8 칸을 가정합니다. 어떻게 재정의 할 수 있습니까?


당신이 의미하는 바에 달려 있습니다. 파일의 실제 탭 문자 4 개의 공백 으로 나타나기를 원합니까, 아니면 "tab"이 실제로 탭 키를 눌러 생성 된 들여 쓰기를 의미합니까? 파일에 문자 그대로 4 개의 공백 문자가 포함됩니다. 각 "탭"을 입력 하시겠습니까?

답변에 따라 다음 설정 중 하나가 적합합니다.

  • 4 칸 너비로 나타나는 탭 문자의 경우 :

    set tabstop=4
    

    소스 코드에서 실제 탭 문자를 사용하는 경우 다음 설정도 원할 것입니다 (실제로는 기본값이지만 방어 적으로 설정하는 것이 좋습니다).

    set softtabstop=0 noexpandtab
    

    마지막으로, 단일 탭에 해당하는 들여 쓰기를 원하는 경우 다음을 사용해야합니다.

    set shiftwidth=4
    
  • 4 개의 공백 문자로 구성 되지만 탭 키로 입력 된 들여 쓰기의 경우 :

    set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab
    

위의 설정을 영구적으로 만들려면이 줄을 vimrc에 추가하십시오 .

조정이 필요하거나 이러한 옵션이 모두 의미하는 바를 이해하려는 경우 다음은 각 옵션의 의미를 분석 한 것입니다.

tabstop

"공백"으로 측정 된 하드 탭 스톱의 너비-실제 탭 문자의 (최대) 너비입니다.

shiftwidth

"들여 쓰기"의 크기. 또한 공백으로 측정되므로 코드베이스가 탭 문자로 들여 쓰기되는 경우 탭 문자 shiftwidth수를 곱하기 를 원합니다 tabstop. 이것은 =, ><명령 같은 것들에서도 사용됩니다 .

softtabstop

이 값을 0이 아닌 값으로 tabstop설정하면 탭 키 (삽입 모드 에서)가이 너비에서 탭 정지 시뮬레이션 하기 위해 공백 (및 가능한 탭) 조합을 삽입하게 됩니다.

expandtab

이를 활성화하면 탭 키 (삽입 모드에서)가 탭 문자 대신 공백을 삽입하게됩니다. 이것은 또한 retab명령 의 동작에 영향을줍니다 .

smarttab

이 기능을 활성화하면 커서가 줄의 시작 부분에있을 때 (즉, 앞의 문자 만 공백) 다음 탭 중지의 다음 들여 쓰기로 이동하기 위해 탭 키 (삽입 모드에서)가 공백이나 탭을 삽입합니다.

이들의에 대한 자세한 내용은 참조하십시오 (예 : VIM에서 ):help 'optionname':help 'tabstop'


현재 사용자에 대해 영구적으로 정의하려면 .vimrc파일을 작성 (또는 편집) 하십시오.

$ vim ~/.vimrc

그런 다음 아래 구성을 파일에 붙여 넣습니다. vim이 다시 시작되면 탭 설정이 적용됩니다.

set tabstop=4       " The width of a TAB is set to 4.
                    " Still it is a \t. It is just that
                    " Vim will interpret it to be having
                    " a width of 4.

set shiftwidth=4    " Indents will have a width of 4

set softtabstop=4   " Sets the number of columns for a TAB

set expandtab       " Expand TABs to spaces

또는 vim modeline의 약자 :

vim :set ts=4 sw=4 sts=4 et :

이것을 복사하여 .vimrc 파일에 붙여 넣었습니다.

" size of a hard tabstop
set tabstop=4

" always uses spaces instead of tab characters
set expandtab

" size of an "indent"
set shiftwidth=4

처음 2 개의 설정은 Tab 키를 누르면 4 개의 공백이 있음을 의미합니다. 세 번째 설정은 내가 할 때 V>(즉, 시각적 및 들여 쓰기) 4 개의 공백도 얻는다는 것을 의미합니다.

받아 들여지는 답변만큼 포괄적이지는 않지만 복사하여 붙여 넣기를 원하는 사람들에게 도움이 될 수 있습니다.


공백 또는 탭 사용 여부를 정의하는 설정이 거의 없습니다.

~/.vimrc파일에 정의 할 수있는 편리한 함수는 다음과 같습니다.

function! UseTabs()
  set tabstop=4     " Size of a hard tabstop (ts).
  set shiftwidth=4  " Size of an indentation (sw).
  set noexpandtab   " Always uses tabs instead of space characters (noet).
  set autoindent    " Copy indent from current line when starting a new line (ai).
endfunction

function! UseSpaces()
  set tabstop=2     " Size of a hard tabstop (ts).
  set shiftwidth=2  " Size of an indentation (sw).
  set expandtab     " Always uses spaces instead of tab characters (et).
  set softtabstop=0 " Number of spaces a <Tab> counts for. When 0, featuer is off (sts).
  set autoindent    " Copy indent from current line when starting a new line.
  set smarttab      " Inserts blanks on a <Tab> key (as per sw, ts and sts).
endfunction

용법:

:call UseTabs()
:call UseSpaces()

파일 확장자별로 사용하려면 다음 구문을 사용할 수 있습니다 (에 추가됨 .vimrc).

au! BufWrite,FileWritePre *.module,*.install call UseSpaces()

참고 항목 : 탭을 공백으로 변환 .


Here is another snippet from Wikia which can be used to toggle between tabs and spaces:

" virtual tabstops using spaces
set shiftwidth=4
set softtabstop=4
set expandtab
" allow toggling between local and default mode
function TabToggle()
  if &expandtab
    set shiftwidth=8
    set softtabstop=0
    set noexpandtab
  else
    set shiftwidth=4
    set softtabstop=4
    set expandtab
  endif
endfunction
nmap <F9> mz:execute TabToggle()<CR>'z

It enables using 4 spaces for every tab and a mapping to F9 to toggle the settings.


Put your desired settings in the ~/.vimrc file -- See below for some guidelines and best practices.

There are four main ways to use tabs in Vim:

  1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4 (or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim will use a mix of tabs and spaces, but typing and will behave like a tab appears every 4 (or 3) characters.

    Note: Setting 'tabstop' to any other value than 8 can make your file appear wrong in many places (e.g., when printing it).

  2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use 'expandtab'. This way you will always insert spaces. The formatting will never be messed up when 'tabstop' is changed.

  3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a |modeline| to set these values when editing the file again. Only works when using Vim to edit the file.

  4. Always set 'tabstop' and 'shiftwidth' to the same value, and 'noexpandtab'. This should then work (for initial indents only) for any tabstop setting that people use. It might be nice to have tabs after the first non-blank inserted as spaces if you do this though. Otherwise aligned comments will be wrong when 'tabstop' ischanged.

Source:


One more thing, use
:retab
to convert existing tab to spaces http://vim.wikia.com/wiki/Converting_tabs_to_spaces


Add line
set ts=4
in
~/.vimrc file for per user
or
/etc/vimrc file for system wide


:set sw=4

See Mastering the VI editor


My basic ~/.vimrc with comment:

set number " show line number                                                                                           
set tabstop=2 " set display width of tab; 1 tab = x space with                                                           
set expandtab " transform tab to x space (x is tabstop)                                                               
set autoindent " auto indent; new line with number of space at the beginning same as previous                                                                      
set shiftwidth=2 " number of space append to lines when type >> 

참고URL : https://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces

반응형