diff --git a/doc/usr_30.jax b/doc/usr_30.jax index 2abf63d0a..99e557075 100644 --- a/doc/usr_30.jax +++ b/doc/usr_30.jax @@ -1,4 +1,4 @@ -*usr_30.txt* For Vim バージョン 9.1. Last change: 2024 Apr 29 +*usr_30.txt* For Vim バージョン 9.1. Last change: 2025 May 30 VIM USER MANUAL - by Bram Moolenaar @@ -398,102 +398,199 @@ Vim が使っている名前を確認できます: > ============================================================================== *30.5* タブとスペース -'tabstop' の初期設定は 8 です。変更することはできますが、それはトラブルの元に -なります。他のプログラムはあなたが使用しているタブ幅を知ることができません。そ -のようなプログラムでは、恐らくタブの幅は 8 であると仮定されているので、テキス -トが崩れて表示されてしまいます。また、ほとんどのプリンタはタブ幅を 8 で固定し -ています。したがって、'tabstop' を変更するのはお勧めしません。(他のタブ幅設定 -で書かれたテキストを修正したい場合は |25.3| を参照してください。) -プログラムをインデントする場合、8 桁単位でインデントするとすぐに画面が埋まって -しまいます。しかしスペースが一つだけではインデントが目立ちません。多くのプログ -ラマは (折衷して) 4 桁を好んで使います。 - の幅は 8 桁で、インデントを 4 桁にしたいということは、 文字を使って -インデントすることはできないということです。そのようなインデントには二つの方法 -があります: - -1. とスペースを混在させる。 は 1 つでスペース 8 個分の幅になるので - ファイルサイズの節約になります。スペース 8 個より の方が入力も削除も - 簡単です。 - -2. スペースだけを使う。違うタブ幅を使う他のプログラムでもトラブルが起きませ - ん。 - -Vim はどちらの方法もサポートしています。 - - -☆タブとスペースを混在させる - -タブとスペースを混在させる場合は特別な設定は必要ありません。初期設定でうまくい -きます。 -'softtabstop' オプションを設定すると編集が少し楽になります。このオプションが設 -定されると、タブ幅が 'softtabstop' の値に設定されたかのように、 キーが動 -作します。しかし実際にはタブとスペースが混在して使われます。 -次のコマンドを実行すると、 キーでカーソルが 4 桁ごとに進むようになります: -> - :set softtabstop=4 +☆タブ導入の背景の簡単な歴史 + +(Vim の祖先である) `vi `は Bill Joy によって開発されました。当時、彼はメモリと +I/O 操作能力が限られた PDP-11 を使用していました。当時は、次のようなトリックを +使ってソースコードのサイズを最適化するのが一般的でした。 + ASCII テーブルは、もともとテレプリンターを遠隔制御するために設計されました。 +制御文字 9 (水平タブ、キャレット表記: ^I) をテレプリンターに送信すると、キャ +リッジは次のタブストップに移動します。タブストップが 8 桁 (一般的な標準) で区 +切られていると仮定すると、1 つの制御文字で最大 8 つのスペース文字と同じ視覚効 +果を生み出すことができます。例えば、次の 2 行は同じように表示されます > + + 1234^I9 + 1234 9 + + キーを使用する方が、 キーを複数回入力するよりも高速でした。 +キーの場合も同様です。 + + +☆タブとインデントの問題 + +Vimでは、2 つの (仮想的な) 水平タブストップ間の列数は 'tabstop' で制御され、デ +フォルトでは 8 に設定されています。この値は変更できますが、後ですぐに問題に遭 +遇します。他のプログラムは、あなたが設定したタブストップ値を認識できないからで +す。おそらくデフォルト値の 8 が使用されるため、テキストの見た目が突然大きく変 +わってしまいます。また、ほとんどのプリンタは固定のタブストップ値 8 を使用しま +す。したがって、'tabstop' はそのままにしておくのが最善です。異なるタブストップ +設定で作成されたファイルを編集する場合は、|25.3| を参照して修正してください。 + プログラム内の行をインデントする場合、8 桁の倍数を使用するとすぐにウィンド +ウの右端にぶつかってしまいます。スペース 1 つでは見た目の違いが十分に出ません。 +多くの人は、妥協案として4桁のスペースを使うことを好みます。 + 行の先頭のタブ文字は視覚的に 8 つのスペースとして表され、4 つのスペースのイ +ンデントを使用したいので、タブ文字を使用してインデントを作成することはできませ +ん。 + これを改善するために、`vi` には 'shiftwidth' オプションがありました。これを +4 に設定すると、挿入モードで新しい行に を押すと行が 4 スペース分インデン +トされます。これは、 キーと 'tabstop' を 8 に設定しただけでは実現できない +結果です。 + スペースを最適化するために、`vi` は暗黙的にスペースの塊を削除し、タブ文字に置 +き換えます。以下は、 を数回押したときに何が起こるかを示しています。 + "." はスペース文字を表し、"------->" はタブ文字を表します。 + + 入力 結果 ~ + .... + -------> + ------->.... + + 同様に、挿入モードで を押すとインデントが減ります。したがって、 +`set tabstop=8 shiftwidth=2` とすると、以下のようになります + + 入力 結果 ~ + ..----->.. + -------> + + `vi` で設定できる 3 つ目のオプションは 'autoindent' です。これは前の行のイン +デントレベルをコピーします + + 入力 結果 ~ + hello .------>hello + hello .------>hello + -------> + +ただし、新しい行は、使用される文字数を最適化することによって生成されます。 + + +☆スペースだけ + +しかし、タブストップを 8 桁で区切るという方法は普遍的ではありませんでした。IBM +では 10 桁が標準であり、今日では Go 開発者の中には `tabstop=4` でコードを記述 +する人もいます。テキストが毎回異なる 'tabstop' 値で表示されると、特にファイル +を共有して別のマシンで開いた場合、テキストの位置がずれる危険性があります。 + その間にコンピュータの性能は飛躍的に向上し、タブ文字の使用によって節約された +数オクテットはもはや実質的な効果を持たなくなっていました。スペースのみを使用す +ることで、どこでも同じ結果のテキストを保証できるようになりました。しかし、`vi` +では機能を犠牲にすることなくスペースのみを使用することは不可能でし +た。'autoindent' は、可能な場合はタブ文字を体系的に入力しようとしていたことを +思い出してください。 + Vim 4.0 では、'expandtab' オプションが導入され、スペースのみの操作がタブのみ +(またはタブとスペースの混合) の操作と同じくらい便利になりました。このオプショ +ンを有効にすると、Vim は通常挿入する水平タブ文字を同数のスペースに置き換え、視 +覚効果を同じにします。 は、これまでと同様に一度に 1 文字のみを削除します。 + + 入力 結果 ~ + ........ + ....... + + +☆タブをスペースに変換 (およびその逆) + +'expandtab' を設定しても、既存のタブ文字にはすぐには影響しません。ファイル内の +水平タブ文字をすべて削除するには、Vim 5.3 で |:retab| コマンドが導入されまし +た。以下のコマンドを使用してください: > -行頭で を押すと、スペースが 4 個挿入されます。次に を押すと、最初 -のスペース 4 個が削除されて 文字が挿入されます (つまりカーソルは 8 列 -目)。このように、スペースは可能な限り 文字で置き換えられます。 -バックスペースはその反対の動作になります。 を押すと、'softtabstop' の幅だ -け削除されます。可能な限り が使われ、余りはスペースになります。 -次の図は、 をいくつか入力してから を使ったときの様子を示しています。 -"." はスペースで "------->" は です。 + :set expandtab + :retab - 入力 結果 ~ - .... - -------> - ------->.... - -------> - .... +これは少し危険です。文字列内のタブも変更されてしまう可能性があるからです。タブ +が存在するかどうかを確認するには、次のようにします: > -同様の動作は 'smarttab' オプションを使っても実現できます。このオプションを設定 -した場合、インデントの入力のために を押すと 'shiftwidth' の幅が使われ、 -何かの文字より後ろでは実際の 文字が使われます。ただし、 の動作は -'softtabstop' のときと同様にはなりません。 + /"[^"\t]*\t[^"]*" +文字列内でタブ文字そのものを使用することは推奨されません。問題を回避するには、 +タブ文字を "\t" に置き換えてください。 -☆スペースだけを使う + 逆の場合も同様に機能します: > -Tab 文字を一切使いたくない場合は 'expandtab' オプションを設定してください: > + :set noexpandtab + :retab! - :set expandtab -このオプションをオンにすると、 キーでスペースが入力されるようになります。 -つまり、 文字と同じ量のスペースが挿入され、 文字は使われません。 -バックスペースキーはスペースを一つずつ削除するので、 を押した後でそれを削 -除するには を 8 回押さなければなりません。インデントの削除は CTRL-D を使 -うと簡単です。 +☆ソフトタブストップ +スペースだけを使用する場合、またはスペースと水平タブを組み合わせて使用する場 +合、タブ文字だけを使用する場合のように、 キーと キーが対称的に動作し +ないという不快な感覚を覚えます。 + Vim 5.4 では 'softtabstop' オプションが導入されました。テキスト内の水平タブ +文字を表示するために使われる (ハード) タブストップに加え、Vim はカーソル専用の +ソフトタブストップを追加します。'softtabstop' が正の値に設定されている場合、 + キーを押すとカーソルが次のソフトタブストップに移動します。Vim はタブ文字 +とスペースの適切な組み合わせを挿入し、視覚的な効果を実現します。同様に +キーを押すと、カーソルは最も近いソフトタブストップに到達しようとします。以下の +例では、`:set softtabstop=4` を使用しています。 -☆タブをスペースに変換する (あるいはその逆) + 入力 結果 ~ + .... + a ------->a + a ------->a... + a ------->a -'expandtab' を設定しても、それ以前に入力されたタブ文字は影響を受けません。つま -り、文章中のタブ文字はタブ文字のままです。タブ文字をスペースに変換したい場合 -は、":retab" コマンドを使ってください。次のように使います: > + 全体的な一貫性を維持するために、`:set softtabstop=-1` を設定し +て、'shiftwidth' の値を 2 つのソフトタブストップ間の桁数として使用するようにす +ることができます。 - :set expandtab - :%retab + 'shiftwidth' と 'softtabstop' に異なる値を設定したい場合でも、'shiftwidth' +でインデントするには を使用できます。あるいは Vim 5.6 で導入された +'smarttab' オプションを使うと、状況に応じて適切な動作をする統一された +キーを実現できます。 -すべてのインデントがスペースに変換されます。ただし、何かの文字より後ろにある -(インデント以外の) タブ文字はそのままです。それらのタブ文字も変換したい場合は -コマンドに ! を付けます: > - :%retab! +☆可変タブストップ -これは少し注意が必要です。なぜなら、文字列の中のタブ文字も変換されてしまうから -です。文字列の中でタブ文字が使われているかどうかは、次の検索パターンで確認でき -ます: > +前述の通り、ASCII テーブルはテレプリンターを遠隔制御するために設計されました。 +特定のテレプリンターでは、物理的なタブストップの間隔を可変に設定できます。結局 +のところ、制御文字 ^I は次のタブストップがどこであっても次のタブストップへ移動 +するという指示を出すだけだったのです。 + Vim 7.3では、同じ機能をエミュレートする 'vartabstop' が導入されました。例え +ば、Vim を `+vartabs` でコンパイルし、`:set vartabstop=2,4` とすると、以下のよ +うになります - /"[^"\t]*\t[^"]*" + 実際の文字 結果 ~ + ^I -> + ^I^I ->---> + ^I^I^I ->--->---> -文字列の中でタブ文字を使うのはお勧めしません。トラブルを避けるためにも "\t" を -使ってください。 + 同様に、可変間隔のソフトタブストップを設定するための 'varsofttabstop' も導入 +されました。`:set varsofttabstop=2,4` とすると、以下のようになります。 -同様に、スペースをタブ文字に変換できます: > + 入力 結果 ~ + .. + ...... + ------->.... + + +☆構成例 +デフォルトでは、Vim はタブのみを使用するように設定されています: > + + :set tabstop=8 + :set shiftwidth=8 :set noexpandtab - :%retab! + :set softtabstop=0 + :set nosmarttab +< + C コードを Python のように記述したい場合 (スペースのみ、インデントは 4 スペー +ス)、以下のように記述します: > + + :set shiftwidth=4 + :set softtabstop=-1 + :set expandtab +< + 同じ動作をしたいが、配置をより適切に制御したい場合 (例えば、パラメータまたは +コメントを 2 の倍数のスペースで整列させる) は、以下のようにします: > + + :set shiftwidth=4 + :set softtabstop=2 + :set expandtab + :set smarttab +< + 代わりに、Bram Moolenaar のような C コード (タブとスペースの組み合わせを使用) +を記述したい場合は、以下のようにします > + + :set shiftwidth=4 + :set softtabstop=-1 +< ============================================================================== *30.6* コメントの整形 diff --git a/en/usr_30.txt b/en/usr_30.txt index c3bbcba69..86fe649bd 100644 --- a/en/usr_30.txt +++ b/en/usr_30.txt @@ -1,4 +1,4 @@ -*usr_30.txt* For Vim version 9.1. Last change: 2024 Apr 29 +*usr_30.txt* For Vim version 9.1. Last change: 2025 May 30 VIM USER MANUAL - by Bram Moolenaar @@ -409,108 +409,202 @@ the cursor is on "printf": ============================================================================== *30.5* Tabs and spaces -'tabstop' is set to eight by default. Although you can change it, you quickly -run into trouble later. Other programs won't know what tabstop value you -used. They probably use the default value of eight, and your text suddenly -looks very different. Also, most printers use a fixed tabstop value of eight. -Thus it's best to keep 'tabstop' alone. (If you edit a file which was written -with a different tabstop setting, see |25.3| for how to fix that.) - For indenting lines in a program, using a multiple of eight spaces makes -you quickly run into the right border of the window. Using a single space -doesn't provide enough visual difference. Many people prefer to use four -spaces, a good compromise. - Since a is eight spaces and you want to use an indent of four spaces, -you can't use a character to make your indent. There are two ways to -handle this: +A QUICK HISTORY OF THE RATIONALE BEHIND TABS -1. Use a mix of and space characters. Since a takes the place of - eight spaces, you have fewer characters in your file. Inserting a - is quicker than eight spaces. Backspacing works faster as well. +`vi` (the ancestor of Vim) was created by Bill Joy. At the time, he was using +a PDP-11 with limited memory and I/O operation capabilities. Back then, it +was common to optimize the size of source code with the following trick. + The ASCII table was first designed to remotely control teleprinters. When +control character 9 (the Horizontal Tab, caret notation: ^I) was sent to a +teleprinter, it would move the carriage to the next tab stop. Assuming tab +stops were separated by 8 columns (a typical standard), this means that a +single control character could produce the same visual effect as up to 8 space +characters. For example, the following two lines will display identically > -2. Use spaces only. This avoids the trouble with programs that use a - different tabstop value. + 1234^I9 + 1234 9 -Fortunately, Vim supports both methods quite well. +Using the key was also faster than typing several times; the +same was true for . -SPACES AND TABS +THE ISSUE WITH TABS AND INDENTATION -If you are using a combination of tabs and spaces, you just edit normally. -The Vim defaults do a fine job of handling things. - You can make life a little easier by setting the 'softtabstop' option. -This option tells Vim to make the key look and feel as if tabs were set -at the value of 'softtabstop', but actually use a combination of tabs and -spaces. - After you execute the following command, every time you press the key -the cursor moves to the next 4-column boundary: > +In Vim, the number of columns between two (virtual) horizontal tab stops +is controlled by 'tabstop' and is set to eight by default. Although you can +change it, you quickly run into trouble later. Other programs won't know what +tabstop value you used. They probably use the default value of eight, and +your text suddenly looks very different. Also, most printers use a fixed +tabstop value of eight. Thus it's best to keep 'tabstop' alone; if you edit a +file which was written with a different tabstop setting, see |25.3| for how +to fix that. + For indenting lines in a program, using a multiple of eight columns makes +you quickly run into the right border of the window. Using a single space +doesn't provide enough visual difference. Many people prefer to use four +spaces, a good compromise. + Since a tab character at the beginning of a line is visually represented +as eight spaces and you want to use an indent of four spaces, you can't use a +tab character to make your indent. + To remedy this, `vi` had the 'shiftwidth' option. When set to 4, on a new +line, pressing in Insert mode would indent the line by 4 spaces, +a result impossible to get with the key and 'tabstop' set to 8. + To optimize space, `vi` would also silently remove packs of spaces and replace +them with tab characters. The following shows what happens pressing +a few times. + A "." stands for a space character and "------->" for a tab character. - :set softtabstop=4 + type result ~ + .... + -------> + ------->.... -When you start in the first column and press , you get 4 spaces inserted -in your text. The second time, Vim takes out the 4 spaces and puts in a -(thus taking you to column 8). Thus Vim uses as many s as possible, and -then fills up with spaces. - When backspacing it works the other way around. A will always delete -the amount specified with 'softtabstop'. Then s are used as many as -possible and spaces to fill the gap. - The following shows what happens pressing a few times, and then using -. A "." stands for a space and "------->" for a . + Similarly pressing in Insert mode would decrease the indent. Hence +with `set tabstop=8 shiftwidth=2` one has - type result ~ - .... - -------> - ------->.... - -------> - .... + type result ~ + ..----->.. + -------> -An alternative is to use the 'smarttab' option. When it's set, Vim uses -'shiftwidth' for a typed in the indent of a line, and a real when -typed after the first non-blank character. However, doesn't work like -with 'softtabstop'. + A third option that one could set in `vi` was 'autoindent'. It copies the +indent level of the previous lines, + type result ~ + hello .------>hello + hello .------>hello + -------> -JUST SPACES +but the new line is produced by optimizing the number of characters used. -If you want absolutely no tabs in your file, you can set the 'expandtab' -option: > - :set expandtab +JUST SPACES -When this option is set, the key inserts a series of spaces. Thus you -get the same amount of white space as if a character was inserted, but -there isn't a real character in your file. - The backspace key will delete each space by itself. Thus after typing one - you have to press the key up to eight times to undo it. If you are -in the indent, pressing CTRL-D will be a lot quicker. +But separating tab stops with 8 columns was not universal: IBM had a standard +at 10 columns, and today some Go developers write code with `tabstop=4`. Every +time text is displayed with a different 'tabstop' value, it risks misaligning +the text, especially once the file is shared and opened on another machine. + In the meantime, computers got much better and the few octets saved by using +tabs were no longer making any real difference. It became possible to use +only spaces and thus guarantee the same resulting text everywhere. But using +only spaces was impossible in `vi` without sacrificing features. Remember that +'autoindent' would systematically try to input a tab character when it could. + Vim 4.0 made working with only spaces as convenient as working only with +tabs (or a mix of tabs and spaces), by introducing the 'expandtab' option. +When set, Vim will replace any horizontal tab character it would normally +insert with an equivalent number of spaces, to end up with the same visual +effect. would continue to remove only one character at a time. + + type result ~ + ........ + ....... CHANGING TABS IN SPACES (AND BACK) -Setting 'expandtab' does not affect any existing tabs. In other words, any -tabs in the document remain tabs. If you want to convert tabs to spaces, use -the ":retab" command. Use these commands: > +Setting 'expandtab' does not immediately affect existing tab characters. In +order to purge a file from all its horizontal tab characters, Vim 5.3 +introduced the |:retab| command. Use these commands: > :set expandtab - :%retab - -Now Vim will have changed all indents to use spaces instead of tabs. However, -all tabs that come after a non-blank character are kept. If you want these to -be converted as well, add a !: > - - :%retab! + :retab This is a little bit dangerous, because it can also change tabs inside a string. To check if these exist, you could use this: > /"[^"\t]*\t[^"]*" -It's recommended not to use hard tabs inside a string. Replace them with -"\t" to avoid trouble. +It's recommended not to use actual tab characters inside a string. Replace +them with "\t" to avoid trouble. -The other way around works just as well: > + The other way around works just as well: > :set noexpandtab - :%retab! + :retab! + + +SOFT TAB STOPS + +When using only spaces, or a mix of spaces and horizontal tabs, one gets the +unpleasant feeling that the two keys and do not act in mirror, as +they do when using only tab characters. + Vim 5.4 introduced the 'softtabstop' option. On top of the (hard) tab stops +used to display the horizontal tab characters in the text, Vim adds extra +soft tab stops dedicated only to the cursor. When 'softtabstop' is set to a +positive value, and the key will push the cursor to the next soft tab +stop. Vim will insert the correct combination of tab characters and spaces to +make the effect visually. Likewise pressing will have the cursor try to +reach the nearest soft tab stop. The following example uses +`:set softtabstop=4` + + type result ~ + .... + a ------->a + a ------->a... + a ------->a + + To maintain global coherence, one can `:set softtabstop=-1` so that +the value of 'shiftwidth' is use for the number of columns between two soft +tab stops. + + If you prefer to have different values for 'shiftwidth' and 'softtabstop', +you can still do so and use to indent with 'shiftwidth'. Or you can +use the 'smarttab' option introduced in Vim 5.6, allowing for a unified + key that knows what to do in the different situations. + + +VARIABLE TAB STOPS + +As we said before, the ASCII table was designed to remotely control +teleprinters. A given teleprinter could be configured to have their physical +tab stops have variable spacing. After all, the ^I control character was +only stipulating: go to the next tab stop wherever it is. + Vim 7.3 introduced 'vartabstop' to emulate the same functionality. For +example if Vim was compiled with `+vartabs` and `:set vartabstop=2,4` one gets + + actual character result ~ + ^I -> + ^I^I ->---> + ^I^I^I ->--->---> + + Similarly, 'varsofttabstop' was also introduced, to have variably spaced +soft tab stops. With `:set varsofttabstop=2,4` one gets + + type result ~ + .. + ...... + ------->.... + + +EXAMPLES OF CONFIGURATION + +By default, Vim is configured to use only tabs: > + + :set tabstop=8 + :set shiftwidth=8 + :set noexpandtab + :set softtabstop=0 + :set nosmarttab +< + If you want to write C code as if it were Python (only spaces, with indents +of 4 spaces), here is what you can use: > + + :set shiftwidth=4 + :set softtabstop=-1 + :set expandtab +< + If you want the same behavior but with better control over alignment +(e.g. lining up parameters or comments in multiples of 2 spaces), use: > + + :set shiftwidth=4 + :set softtabstop=2 + :set expandtab + :set smarttab +< + If instead, you would like to write C code like Bram Moolenaar would have +(using a mix of tabs and spaces), you can use > + + :set shiftwidth=4 + :set softtabstop=-1 +< ============================================================================== *30.6* Formatting comments