3

For example:

QString QSSMLTools::heather(QString s)
{
    if (!m_Enable) {return s;}

    s.insert(0,"<voice name='Heather'>");
    s.append("</voice>");
    return s;
}
QString QSSMLTools::isabella(QString s)
{
    if (!m_Enable) {return s;}

    s.insert(0,"<voice name='Isabella'>");
    s.append("</voice>");
    return s;
}

Placing my cursor over v on s.insert(0,"<๐˜ƒoice name='Heather'>"); and pressing * will give me ๐˜ƒ๐—ผ๐—ถ๐—ฐ๐—ฒ, highlighting 4 selections:

s.insert(0,"<๐˜ƒ๐—ผ๐—ถ๐—ฐ๐—ฒ name='Heather'>");
s.append("</๐˜ƒ๐—ผ๐—ถ๐—ฐ๐—ฒ>");
s.insert(0,"<๐˜ƒ๐—ผ๐—ถ๐—ฐ๐—ฒ name='Isabella'>");
s.append("</๐˜ƒ๐—ผ๐—ถ๐—ฐ๐—ฒ>");

Pressing ** should give me voice name highlighting two sections.

Pressing *** should give me voice name=' highlighting two sections.

Pressing **** should give me voice name='Heather highlighting one section.

How can this behaviour be achieved?

4

You can do this with two mappings, a normal mode mapping, and a visual mode mapping. Try this:

nnoremap * viw
xnoremap * wiw

The first one is pretty straightforward. It starts visual mode (v), and selects (i)nside of the current (w)ord. Then, the second time you hit it, the other mapping applies since we will no longer be in normal mode.

So on the second press, it moves forward one word, (now the selection is voice n) then again, selects (i)nside of the current (w)ord. Now the selection is voice name. Hitting it the third and fourth times repeats the same concept, and can even extend across multiple lines.

2

If I understand correctly, you want to highlight every occurence of the selected words. You can easily expand DJMcMayhem answer to search for the selected words in the buffer like so:

:nnoremap * viwy/<C-R>"<CR>Ngn
:vnoremap * wiwy/<C-R>"<CR>Ngn

After selecting the correct number of words, it searches the selection with y/<C-R>"<CR>, then reselect it with Ngn.

2
  • 2
    If you want to preserve the previous content of the unnamed register, you can save it and restore it afterward as Luc Hermitte did in this answer. – Jair López Oct 4 '16 at 11:59
  • @JairLópez Yes, of course, or you can simply use another register if you can spare one. I should have mentionned it. – TonioElGringo Oct 4 '16 at 12:01

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.