2

I have used this script to be able to highlight results from test reports: https://vim.fandom.com/wiki/Highlight_multiple_words

When using it in Vim, it works fine when I type :Highlight 1 FAILED (For some reason the numpad-keys don't work).

Unfortunately, this is not session persistent, so what I was planning to do is create a chain of these commands and add them to my notepad where I store frequently used commands. In this case, I could use it every time I open a test-report.

So for example, I would use: :Highlight 1 FAILED | :Highlight 2 PASSED. Unfortunately this didn't work. I also tried escaping the pipe and removing the second colon. Strangely enough, when trying echo "a" | echo "b" it does work?

What am I doing wrong here? How do I chain these commands properly?

4
  • Note that even though the context is the same as this question, It is NOT a duplicate. Commented Apr 4, 2022 at 12:26
  • 3
    See :help command-bar Commented Apr 4, 2022 at 14:48
  • @D.BenKnoble I must be missing something obvious? What am I supposed to see there? I see some explanation about how to use "bar" (which I guess is a strange way of describing the pipe symbol?), but isn't that precisely what I did with the echo's in my example? Commented Apr 4, 2022 at 17:24
  • Maybe it’s :help :command-bar; should be the explanation on adding the -bar attribute to user-commands to allow the chaining to work Commented Apr 4, 2022 at 17:53

1 Answer 1

4

What exactly happens when using the | after a custom command (that is defined using :com (see in the help at :h :command-bar.

In short, if you know that your argument cannot contain the | character, you should add the -bar argument to your command definition, so that a user can always chain several commands using the :A | :B syntax. (This means the command :A was defined as followed :command -bar A ....)

In your specific case this doesn't seem to be the case. The usual work-around to that is to wrap the command into an :exe statement, which are then allowed to be chained by using the bar |. So you can use:

:exe ":Highlight 1 FAILED" | :exe ":Highlight 2 PASSED" 
3
  • Thanks! The sample you gave worked indeed, so upvoted already! I'm trying to understand the rest of your answer though. You say something about not being allowed to use | in a command, but in the A-B example you do use one? What's the difference there? Also: where should I add this -bar argument? Simply behind the command? Like :command A | :command B -bar ? I've never seen this kind of syntax in Vim before. I've been exclusively using Vim for 2 years now, and I still feel like there's so much I don't understand yet :) Commented Apr 5, 2022 at 7:25
  • you need to add the -bar to the definition of the command. E.g. you need to define command A like this: :command -bar A :echo foobar and then you can use :A | echo "bar". Commented Apr 5, 2022 at 8:54
  • Okay, thanks for the explanation. I it didn't work in this case (but you already pointed that out in your answer), but it's good to know for the future. Commented Apr 5, 2022 at 8:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.