0

vim-surround not working properly in macro:

I want to change all li's

          <li><a href="/">home</a></li>
          <li><a href="/stuff">Stuff</a></li>
          <li><a href="/contact">Contact</a></li>

to NavLink's: `

              <NavLink><a href="/">home</a></NavLink>
              <NavLink><a href="/stuff">Stuff</a></NavLink>
              <NavLink><a href="/contact">Contact</a></NavLink>

This should be possible doing (cursor is on first li element): [record macro:] _flcst<NavLink>j

But when executing the macro on the next line, it macro stops at the point where "vim surround" expects you to type in the tag name you wish to substitute for.

Questions:

  1. Is this expected behavior -- it's not right? (A bug)
  2. What would be an effecient alternative for changing the li elements to NavLink elements?
2
  • 1
    In this particular case, since none of the other text contains li, you could use a substitute. You could also do something like :[range]global/<li>/normal flcst<NavLink>
    – D. Ben Knoble
    May 9 at 12:59
  • 1
    This also does not happen on my vim, but I have no idea about ideavim
    – D. Ben Knoble
    May 9 at 13:34
1

Using cst<NavLink> in a macro works just fine in Vim with vim-surround.

Note that the "surround" you get in IdeaVim is not the real vim-surround, as the vim plug-ins available in IdeaVim are actually emulated and not running the same Vimscript code as the actual Vim plug-ins do (AFAICT).

I imagine the issue with this particular usage is that the cst command will use something akin to an input() to prompt for the new tab name and I can see how this could confuse the macro recording/replaying system and how keys are fed to Vim. (To be frank, when I first saw your question, I found it quite plausible that this would not work in Vim itself, but testing it showed that it actually does.)

So, is it a bug? Possibly. I guess it depends on how much the IdeaVim authors (who presumably also own the emulated plug-ins) care about close compatibility with Vim. In any case, I'd suggest raising this problem in the issue tracker of IdeaVim, since the authors are probably the ones who can make the call of whether that should (or even can) be fixed in IdeaVim's emulation of vim-surround.

As for a replacement, I'd suggest going with a :s for replacing the lis with NavLinks. Depending on whether li> is unique enough to not match anything other than the <li> and </li>s, you could use something like:

:'<,'>s/li>/NavLink>/g

I'm using a Visual selection as the range (the '<,'> part), assuming you don't want this replacement in the whole buffer... So use a Visual first to delimit the region where you want this executed. Vim automatically adds the '<,'> when you press : from a Visual selection, so you typically don't have to type it. Not sure if IdeaVim does the same, I'd expect it will.

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.