Unnecessary Context:
I want to write a function that closes open HTML tags, because closetag.vim seems complicated, and I like to understand all the stuff I run in Vim.
My plan for this is to match the most recent previous <\w+
segment, grab whatever is \w+
, and then output </\1>
at the cursor location when I ran this function.
Question:
How do I set a variable in my function equal to the most recent previous instance of \w\+
that comes after a <
? I can do the search with ?<\(\w\+\)
, but nothing I've found will honor the capture group usage.
I have found matches
and getmatches
in :help
(and searchpos
and getline
on SO), but it certainly feels like there should be an easier way than using two builtins together. I know about the search register, but that only contains the literal regex above, not what it has matched to.
It looks like a similar question was asked 5 months ago but the only answer is about matching tags on the same line, and I would like to do more. I found this question that says how to visually highlight the whole match, but can't handle capture groups, though it looks like the best method if I give up on the capture group limitation.
TL;DR: Is there a simple way to get the contents of a capture group search result into a variable?
:help
it looks like that's only valid for substitutions, and I don't plan on changing the text I'm trying to match. If I'm incorrect, I'd love to see an answer that shows what you mean!:s^^\=submatch(0).'</'.submatch(1).'>'^g
is adequate. For cases where you do not want to do an immediate substitution, the problem is a bit more complicated.