0

I'm trying to use junegunn/vim-easy-align to vertically align some text.

EasyAlign has an interactive-mode which means you can invoke EasyAlign, it prompts for a key, and then aligns around whatever key you enter.

eg: given the following text:

foo = bar
hello = world
a = b
something = this

You can visually select the 4 lines, and then enter interactive mode using :EasyAlign, then hit =, and it will result in:

foo       = bar
hello     = world
a         = b
something = this

Manually this works great, but when I attempt to add a keybinding it fails.

I would like to bind :EasyAlign to <leader>xa in visual mode.

This is the command I've used:

vim.api.nvim_set_keymap('v', "<leader>xa", "<cmd>EasyAlign<CR>", { noremap=true, silent = true })

When I hit <leader>xa I can see the EasyAlign interactive prompt in the message area, but when I then hit = nothing happens.

How can I bind <leader>xa to open the EasyAlign interactive prompt, and then have it align around whatever key I enter?

1
  • @D.BenKnoble that worked!! Thank you! If you would like to write this in an answer I'd gladly accept. Where can I read up on what the difference is here? Mar 10, 2022 at 13:26

2 Answers 2

2

Try : instead of <cmd>; it’s possible switching mode is intended here. Read about the difference at :help <cmd>.

0

I just ran into this myself while doing my vimscript-to-lua conversion. For whatever reason, you can't use noremap=true. Here's how I have it in my setup:

vim.api.nvim_set_keymap('v', '<Enter>', '<Plug>(EasyAlign)', {})
2
  • You have to let <Plug> mappings go through recursive processing, in general, so that’s one of the few times you want (for example) nmap instead of nnoremap
    – D. Ben Knoble
    Mar 11, 2022 at 13:42
  • I've never really understood the purpose of <Plug>. I guess I'll have to read up on it. Why would junegunn recommend using <Plug>, as indicated here: :h easy-align-1-using-plug-mappings or github.com/junegunn/vim-easy-align/blob/master/doc/…
    – Phil R
    Mar 11, 2022 at 16:34

Your Answer

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

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