質問

vi - Vim は決してタブを使用せず(スペースをタブに変換する、悪い!)、タブキーを== 4スペースにし、中括弧の後にコードを自動的にインデントする Emacs のようなブレースブロック?

また、これらの設定を保存して再度入力する必要がないようにする方法

これに関連する他の質問を見たことがありますが、私が望むものから常に少し外れているようです。

役に立ちましたか?

解決

以下のいくつかの回答で指摘されているように、現在好ましい方法はsmartindentを使用せず、代わりに以下を使用することです(。vimrc ):

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab

<ストライク> .vimrc:ファイル:

set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

ヘルプファイルは慣れるまでに少し時間がかかりますが、読むほどにVimが良くなります:

:help smartindent

さらに良いことに、移植性のためにこれらの設定をソースに埋め込むことができます:

:help auto-setting

現在の設定を表示するには:

:set all

graywh がコメントで指摘しているように、smartindentはcindentに置き換えられました。まだ主にCライクな構文の言語用:

:help C-indenting

他のヒント

関連、タブとスペースの両方を使用するファイルを開いた場合、持っていると仮定して

set expandtab ts=4 sw=4 ai

すべてのタブをファイル全体のスペースに置き換えることができます

:%retab

ファイルタイプ固有のインデントを取得する最良の方法は、vimrcで filetype plugin indent on を使用することです。次に、たとえば.vim / ftplugin / c.vimで set sw = 4 sts = 4 et のようなものを指定できます。編集中のすべてのファイルやその他の非C型構文も正しくインデントされます(lisps)

ほとんどのファイルに4スペースタブ、Makefileに実際の8ワイドタブ文字、C / C ++などのさまざまなファイルに自動インデントを設定するには、これを〜/ .vimrc ファイルに入れます。

" Only do this part when compiled with support for autocommands.
if has("autocmd")
    " Use filetype detection and file-based automatic indenting.
    filetype plugin indent on

    " Use actual tab chars in Makefiles.
    autocmd FileType make set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
endif

" For everything else, use a tab width of 4 space chars.
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.

Ubuntuなどの多くのLinuxシステムでは、デフォルトで .vimrc ファイルは存在しないため、最初に作成することをお勧めします。

ホームディレクトリにある .viminfo ファイルを使用しないでください。別の目的で使用されます。

ステップ1:ホームディレクトリに移動します

cd〜

ステップ2:ファイルを作成する

vim .vimrc

ステップ3:上記の構成を追加します

filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab

ステップ3: Shift + ZZ を押してファイルを保存します。

推奨される方法は、ファイルタイプベースのインデントを使用し、それが十分でない場合にのみsmartindentとcindentを使用することです。

次を.vimrcに追加します

set expandtab
set shiftwidth=2
set softtabstop=2
filetype plugin indent on

別の答えとして役立つことを願っています。

〜/ .vimrcを編集します

$ vim ~/.vimrc

次の行を追加:

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab

VIM wikiから

:set tabstop=4
:set shiftwidth=4
:set expandtab

自動インデントは、現在の構文モードに基づいています。 Foo.javaを編集している場合、 {を入力して Enter を押すと、次の行がインデントされます。

タブについては、2つの設定があります。 Vim内でコロンを入力して、「set tabstop = 4」を設定します。タブを4つのスペースとして表示するように設定します。もう一度コロンを押し、「set expandtab」と入力します;タブにスペースを挿入します。

これらの設定は、ホームディレクトリの.vimrc(またはWindowsでは_vimrc)に配置できるため、一度入力するだけで済みます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top