Is there a motion for capturing text in between /
or \
? I know there is motions for other symbols ---
ci"
- Capture 'text' inside "text"
vi(
- Visual capture int var
inside foo(int var)
di[
- Delete word [word]
into []
The only workaround I can find is by using Vim Surround, and using it to change surrounding \
into "
(cs\"
), and then working from there. However, not only is it that kind of tedious, but the plugin only supports backslashes, not forward.
You could write your own text-object for this pretty easily.
onoremap <silent> i/ :<C-U>normal! T/vt/<CR> " inside /
onoremap <silent> a/ :<C-U>normal! F/vf/<CR> " around /
For it to work with visual mode :
xnoremap <silent> i/ :<C-U>normal! T/vt/<CR> " inside /
xnoremap <silent> a/ :<C-U>normal! F/vf/<CR> " around /
Similarly you could also do for \
Edit: Added info comments.
Here's one way to do it with a mapping:
nnoremap H mal??e+1<Enter>mb//<Enter>y`b`a
This mapping will yank everything between two occurrences of the last search pattern.
Place it in your .vimrc
file to make it permanent.
Usage for your question:
/\/
(note the backslash escape character)
H
Everything between the last /
and the next /
will get yanked.
Explanation:
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`
Example input:
This is a *funky* string
*
H
The word funky will be in the yank buffer.
Example input:
<br>
Capture all this text.
<br>
<br>
H
in normal mode when between <br>
s (or on 2nd <br>
)
Example input:
<p>
Capture this paragraph.
</p>
<.\?p>
(or <\/\{,1}p>
to be more correct)<p>
tag)
A better approach might be to use a register to remember a delimiter, so you can use this mapping quickly and/or repeatedly. In other words, you could store /
or \
or <\?p>
in a register and use that to quickly capture text between your stored delimiter.
there is no built-in text object with slash
. However there are plugins support customized text-object, like:
targets
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With