質問
タブをgVimのスペースに変換したいと思います。 _vimrc
に次の行を追加しました:
set tabstop=2
2つのスペースで停止するように機能しますが、1つのタブキーが挿入されているように見えます(後でhキーを使用してスペースをカウントしようとしました)。
gVimでタブをスペースに変換するために何をすべきかわかりませんか?
解決
IIRC、次のようなもの:
set tabstop=2 shiftwidth=2 expandtab
トリックを行う必要があります。すでにタブがある場合は、素敵なグローバルREを追加して、それらをダブルスペースに置き換えます。
他のヒント
他の回答に従ってexpandtabを取得したら、新しい設定に従って既存のファイルを変換する非常に便利な方法は次のとおりです。
:retab
現在のバッファで動作します。
試用
set expandtab
ソフトタブ用。
既存のタブを修正するには:
:%s/\t/ /g
すでにタブストップを2つのスペースに設定しているため、2つのスペースを使用しました。
これは私のために働いた:
最初にこれを行うとタブが表示されます:
:set list
その後、タブを置換できるようにするには、次の操作を実行します。
:set expandtab
then
:retab
すべてのタブがスペースに置き換えられました その後、次のように通常の表示に戻ることができます。
:set nolist
gg=G
は、ファイル全体を再インデントし、同僚からファイルに取得したすべてのタブではないにしても、ほとんどのタブを削除します。
.vimrcに次の行を追加します
set expandtab
set tabstop=4
set shiftwidth=4
map <F2> :retab <CR> :wq! <CR>
vimでファイルを開き、F2を押します タブは4つのスペースに変換され、ファイルは自動的に保存されます。
\t
を8スペースに維持する場合は、設定を検討してください:
set softtabstop=2 tabstop=8 shiftwidth=2
<TAB>
を押すごとに2つのスペースが与えられますが、コード内の実際の<=>は8文字として表示されます。
ファイル内のタブの最初の検索:/ ^ I :set expandtab :retab
動作します。
expand
は、タブをスペースに変換するUNIXユーティリティです。 vimで何もset
したくない場合は、vimからシェルコマンドを使用できます。
:!% expand -t8
これでうまくいきました:
:set tabstop=2 shiftwidth=2 expandtab | retab
この記事には、タブとスペースを処理し、それらの間で変換するための優れたvimrcスクリプトがあります。
これらのコマンドが提供されています:
Space2Tab インデントのみでスペースをタブに変換します。
Tab2Space タブをスペースに変換します。インデントのみです。
RetabIndent Space2Tab( 'expandtab'が設定されている場合)、またはTab2Space(そうでない場合)を実行します。
各コマンドは、タブ列のスペースの数を指定する引数を受け入れます。デフォルトでは、「tabstop」設定が使用されます。
出典: http://vim.wikia.com/wiki/Super_retab#Script
" Return indent (all whitespace at start of a line), converted from
" tabs to spaces if what = 1, or from spaces to tabs otherwise.
" When converting to tabs, result has no redundant spaces.
function! Indenting(indent, what, cols)
let spccol = repeat(' ', a:cols)
let result = substitute(a:indent, spccol, '\t', 'g')
let result = substitute(result, ' \+\ze\t', '', 'g')
if a:what == 1
let result = substitute(result, '\t', spccol, 'g')
endif
return result
endfunction
" Convert whitespace used for indenting (before first non-whitespace).
" what = 0 (convert spaces to tabs), or 1 (convert tabs to spaces).
" cols = string with number of columns per tab, or empty to use 'tabstop'.
" The cursor position is restored, but the cursor will be in a different
" column when the number of characters in the indent of the line is changed.
function! IndentConvert(line1, line2, what, cols)
let savepos = getpos('.')
let cols = empty(a:cols) ? &tabstop : a:cols
execute a:line1 . ',' . a:line2 . 's/^\s\+/\=Indenting(submatch(0), a:what, cols)/e'
call histdel('search', -1)
call setpos('.', savepos)
endfunction
command! -nargs=? -range=% Space2Tab call IndentConvert(<line1>,<line2>,0,<q-args>)
command! -nargs=? -range=% Tab2Space call IndentConvert(<line1>,<line2>,1,<q-args>)
command! -nargs=? -range=% RetabIndent call IndentConvert(<line1>,<line2>,&et,<q-args>)
これは、最初に解決策を探しに行ったときの回答よりも少し助けになりました。