次のような正規表現があります。
:%s/pattern/ pattern/gc
置換テキストでわかるように、最初は 8 つの空白文字が必要です。毎回 8 つの空白を入力したくありません。これを行うよりエレガントな方法はありますか?
使用できますprintf('%*s', 8, '')
:
asterisk '*'
フィールド幅または精度、あるいはその両方は、数字列の代わりに で示される場合があります。この場合、Number 引数はフィールド幅を提供します。
:%s/pattern/\=printf('%*spattern', 8, '')/gc
If you're after conserving keystrokes, you can initialize a named register with the replacement text:
:let @a = repeat(' ', 8)
Then, when building your substitution command, you insert the register contents via CTRL-R + {register-name}:
:%s/pattern/<C-R>a&/gc
For further simplification, I have referred to the search pattern in the replacement string via &
.
コマンドの前に数字を入力することで、いくつかの vi コマンドを繰り返すことができます。