1

I've written a "script" to highlight all lines that begin with n (for a todo list):

syn region dash start=/^n/ end=/$/ oneline | highlight link dash String

And mapped it to ; in my init.vim:

map ; :syn region dash start=/^n/ end=/$/ oneline <bar> highlight link dash String

When I press ; the first command shows up in the bar at the bottom. They only run after I press enter. Why does Vim require me to hit enter, and how can I get rid of that step?

I'm very new to vim scripting, hopefully it's not something too obvious.

4

See :help map-return:

When you have a mapping that contains an Ex command, you need to put a line terminator after it to have it executed. The use of <CR> is recommended for this.

You should also use <silent> to prevent Vim from printing the long command-line before executing it. This should also help prevent a "Press ENTER or type command to continue" message.

Also, try to make your mappings non-recursive by default, unless you really need to make them recursive.

And usually it's good to restrict the mode in which they're available. In this case, you only want this mapping to work in Normal mode, since it doesn't really work (as is) in Visual, Select or Operator-pending modes.

So, in your case, what you need is:

nnoremap <silent> ; :syn region dash start=/^n/ end=/$/ oneline <bar> highlight link dash String<CR>
  • Somehow I did not know :help exists, thanks! When I run what you've written, I get a message to "Press ENTER or type command to continue". Appending another <CR> makes the issue go away. Is appending a good idea, or is there a better way to deal with this? – CtrlAltF2 Aug 24 '20 at 22:09
  • 1
    @CtrlAltF2 Updated with more details. I believe with <silent> you will no longer see the prompt, since I think it's being caused by a long command-line being printed by Vim before execution. (But I might be wrong about that part.) – filbranden Aug 24 '20 at 22:16
  • 2
    How do I navigate to topics in Vim's documentation? might be useful for you if you're unfamiliar with :help @CtrlAltF2 There's also :help help-summary – Martin Tournoij Aug 25 '20 at 1:49

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.