0

I want to write a command to do the following:

  1. Make a visual selection.
  2. Call a command on the visual selection.

By way of example, I'd like to call vat to visually select around the current tag, and then use Limelight to highlight it.

Attempt #1:

vim.api.nvim_create_user_command('HighlightTagBlock',
    function()
        vim.cmd.normal("vat")
        vim.cmd("'<,'>Limelight")
    end,
    { nargs = 0 }
)

Attempt #2:

vim.api.nvim_create_user_command('HighlightTagBlock',
    function()
        vim.cmd.normal("vat")
        vim.cmd.visual("Limelight")
    end,
    { nargs = 0 }
)

Neither of these works as expected. What's the right way to do this?

2 Answers 2

0

I don't know neovim api, but <Plug>(Limelight) defined as visual mapping in Limelight might work:

vim.api.nvim_create_user_command('HighlightTagBlock',
    function()
        vim.cmd.normal("vat")
        vim.cmd.visual("<Plug>(Limelight)")
    end,
    { nargs = 0 }
)
1
  • Best I can tell, it seems like this is equivalent to the vim.cmd.visual("Limelight") version.
    – Sasgorilla
    Mar 13 at 14:55
0

I would do:

vim.api.nvim_create_user_command('HighlightTagBlock',
  [[
    execute 'normal vat'
    '<,'>Limelight
  ]],
  { nargs = 0 }
)

I have protected the normal call with execute since I otherwise the rest of the string is considered as an argument of the normal code.

2
  • 1
    I don’t think parens are needed: :normal vat is what I would write in Vimscript
    – D. Ben Knoble
    15 hours ago
  • Thanks for the comment. 14 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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