関数を使用してそれを行いました。私はそれをテストしましたが、特定のケースでは、いくつかのバグを修正する必要があるかもしれません。これをvimrcに追加してみてください:
set et
function! Inserttab()
let insert = ""
let line = getline('.')
let pos = getpos('.')[2]
let before = ""
let after = line
if pos != 1
let before = line[ 0: pos - 1]
let after = line[pos : strlen(line) ]
endif
if pos != 1 && substitute(before, "[ \t]", "", "g") != ""
let insert = "\t"
else
let insert = " "
endif
let line = before . insert . after
call setline('.', line)
call cursor(line('.'), strlen(before . insert))
endfunction
inoremap <tab> <esc>:call Inserttab()<CR>a
基本的に、ビジュアルモードでキーを関数 Inserttab() に再マップします。また、ts を 4 以外に変更しても、値がハードコードされているため、2 つではなく 4 つのスペースが出力されることに注意してください。
また、私はvimスクリプトにあまり慣れていませんが、使用されるすべての変数がグローバルになると思いますが、これは悪いことです.
使用できる空白を「見る」ことを忘れていましたset list
。でこれを無効にしset nolist
ます。また、通常モードでga
は、カーソルがある文字に関する情報を表示するために使用できます。
編集
行の先頭にタブを挿入したい場合があることを理解しています。私のスクリプトは、最初にスペースを挿入し、それ以外の場所にタブを挿入します。
タブキーを押すたびにタブが本当に必要な場合は、次のように簡単に使用できます。
set et
function! Inserttab()
let insert = ""
let line = getline('.')
let pos = getpos('.')[2]
let before = ""
let after = line
if pos != 1
let before = line[ 0: pos - 1]
let after = line[pos : strlen(line) ]
endif
let insert = "\t"
let line = before . insert . after
call setline('.', line)
call cursor(line('.'), strlen(before . insert))
endfunction
inoremap <tab> <esc>:call Inserttab()<CR>a
しかし、このバージョンでは、挿入モードから手動でインデントすることはできません。