マッピングでそれを行う 1 つの方法を次に示します。
nnoremap H mal??e+1<Enter>mb//<Enter>y`b`a
このマッピングは、最後の検索パターンの 2 つの出現の間のすべてをヤンクします。
.vimrc
ファイルに配置して、永続的にします。
質問の使用法:
- スラッシュを検索:
/\/
(バックスラッシュのエスケープ文字に注意してください)
- 2 つのスラッシュの間(または 2 番目のスラッシュ) にカーソルを配置します
- 押す
H
最後/
と次の間のすべて/
がヤンクされます。
説明:
nnoremap H mal??e+1<Enter>mb//<Enter>y`b`a
- nnoremap H map H in normal mode, ignoring other mappings
- ma place a mark (named a) at the current cursor location
- l move the cursor one char to the right
- ??e+1<Enter> move to 1 character after END of prev occurrence of last search pattern
- mb place a mark (named b) at the current cursor location
- //<Enter> go to the beginning of the next occurrence of the last search pattern
- y`b yank to the location of the mark named x (note: ` = "back tick")
- `a go to the mark named `a`
入力例:
This is a *funky* string
- 検索する
*
- カーソルを 2 つのアスタリスクの間(または 2 番目のアスタリスクの上) に配置します。
- 押す
H
funkyという単語がヤンク バッファに格納されます。
単語を区切り文字として使用できます。
入力例:
<br>
Capture all this text.
<br>
- 検索する
<br>
H
通常モードで<br>
s の間(または 2 番目)に押す<br>
正規表現も使用できます。
入力例:
<p>
Capture this paragraph.
</p>
- を検索します
<.\?p>
(または <\/\{,1}p>
、より正確には)
- 段落内(または終了タグ)で、通常モードで H を押します。
<p>
...
より良いアプローチは、レジスターを使用して区切り文字を記憶することです。これにより、このマッピングを素早くおよび/または繰り返し使用できます。つまり、/
or\
または<\?p>
をレジスターに保存し、それを使用して、保存された区切り文字の間のテキストをすばやくキャプチャすることができます。