0

I found a command that would sort highlighted lines by their length and wanted to make a mapping for it so that I could avoid typing it every time.

vnoremap ,s :<C-U>'<,'> ! awk '{ print length(), $0 | "sort -n | cut -d\\  -f2-" }'<CR>

However, when I try to use the remapping, only the following is seen:

:'<,'> ! awk '{ print length(), $0

It abruptly stops at $0 and does not complete the rest of the command.

I don't know what I am doing wrong; any help would be appreciated.

3
  • the \" is seen as starting a comment. Try to escape the double quotation Dec 12, 2022 at 13:45
  • 1
    You'll also need to write <bar> or \| instead of |
    – D. Ben Knoble
    Dec 12, 2022 at 14:30
  • 1
    replacing | with <bar> worked. thank you. Dec 12, 2022 at 15:54

1 Answer 1

0

The mapping command, like most of the Vim commands, can be part of a list of commands | separated.

When executed the following line:

nnoremap mm :echo "foo" | echo "bar"

is equivalent to the script:

nnoremap mm :echo "foo"
echo "bar"

If you like the | to be part of the mapping it has to be escaped as \| or as <bar>.

Because mappings simulate keys, the mapping itself use the < escaping:

  • <lt> stands for <
  • <char-65> stands for A
  • <C-s> stands for Ctrl-s
  • <bar> stands for |
  • ...
2
  • Thanks, I have updated the solution accordingly :-) Feb 16 at 15:31
  • I saw you were doing but I didn't dare, still too green :-) I'll do in the future. Feb 16 at 16:26

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.