
次のSQLがあります:
update am.PERMISSIONS set PRM_ORDER = 35 PRM_VISIBLE = b'1' where PRM_ID = 3;
update am.PERMISSIONS set PRM_ORDER = [35] PRM_VISIBLE = b'1' where PRM_ID = 7;
update am.PERMISSIONS set PRM_ORDER = [40] PRM_VISIBLE = b'1' where PRM_ID = 10;
update am.PERMISSIONS set PRM_ORDER = [45] PRM_VISIBLE = b'1' where PRM_ID = 11;
...
角括弧を使用して、各数値を 5 ずつ増やすビジュアル ブロックを選択します。どうすればよいでしょうか?
答え1
括弧内のテキストを視覚的に強調表示します。
Ctr+ V2jl
各数字を 5 ずつ増やします。
:norm 5
Ctr+ V Ctr+A 説明:
:norm
コマンド全体を通常モードで実行します。Ctr+はV必須です。そうしないと、カーソルが行の先頭に戻ります。
Ctr+ はA数字を 1 ずつ増やし、これを 5 回繰り返します。コロンを押すと、視覚範囲が自動的に挿入されます。
編集: Stephane が正しく指摘したように、前のコードは任意の行で見つかった最初の数字を増分します。より良い解決策は次のとおりです。
%s/\[\zs\d\+\ze\]/\=(submatch(0)+5)
括弧内のすべての整数に 5 を加算します。 および\zs
は、\ze
一致から括弧を除外するために使用され、submatch
一致した数値を返します。
答え2
数字を増やすためにビジュアルモードを終了する必要はありません。g
5 g Ctrl-a
5 ......... 5 times
g ......... globally
Ctrl-a .... increase numbers
実は私はこのトリックをヴィムゴルフチャレンジ。
答え3
これら2つのコマンドは同じであり、すべての数値を増分します。内で 視覚的な選択(長方形でも!)
:'<,'>s/\%V\d\+\%V/\=submatch(0)+1/g
:s/\%V\d\+\%V/\=submatch(0)+1/g
細かく刻んだ(刻まれた::s
/
\%V
\d\+
\%V
/
\=submatch(0)+1
/
g
は\%V
、現在の(または最後の)選択範囲内の任意の場所に一致するゼロ幅のマッチャーです。
vim ヘルプより:
\%V Match inside the Visual area. When Visual mode has already been
stopped match in the area that gv would reselect.
This is a /zero-width match. To make sure the whole pattern is
inside the Visual area put it at the start and just before the end of
the pattern, e.g.:
/\%Vfoo.*ba\%Vr
This also works if only "foo bar" was Visually selected. This:
/\%Vfoo.*bar\%V
would match "foo bar" if the Visual selection continues after the "r".
Only works for the current buffer.
ctrl-a
残念ながら、これは負の数を理解しないので、それほど賢明ではありません。