I am trying to define a map to define a key to sort a file based on a pattern, eg

nmap <leader>so :sort i /ap\s\+<leader>/<cr>

When I execute ,so the sort is limited to the first 6 chars of the pattern, ie the picture. I guess there's a limit on the pattern size but can anyone tell me why?

,so not using full pattern

I could call the external sort but would prefer not to.

1 Answer 1


There is no limit to a pattern size and your mapping does exactly what you asked it to do. Your problem is that you didn't ask it to do what you want.

So your command is the following:

nmap <leader>so :sort i /ap\s\+<leader>/<cr>

What we can see in your screenshot is that when you pressed <leader>so vim executed the mapping until it met <leader> where it put a , instead. This is pretty normal you probably have let mapleader = "," somewhere in your vimrc. After that, the mapping continued with / and you can't see <cr> since it is the keypress which validates the command.

So your problem is not a limit in the pattern it is that the command you want to map doesn't work properly. To solve that you should

  • Take your pattern /ap\s\+<leader>/ and change it until it matches what you want (simply start a search with / and modify the pattern until you are satisfied
  • Once your pattern works use the command :sort i /newpattern/ from Vim command line to be sure it works properly.
  • Finally, when you command works you can create your mapping. (Note that I would advise using nnoremap instead of nmap)
  • Use <lt> instead of < in <leader>. e.g. nnoremap <leader>so :sort i /ap\s\+<lt>leader>/<cr>. See :h keycodes Dec 12, 2017 at 15:27
  • Hi @statox. Thanks. The / works but Peter Rincker hit the nail on the head. I'll accept your answer though as it is a good through process
    – Steve
    Dec 13, 2017 at 4:51
  • @Steve Thanks, Peter always has the solution! :) If he makes another answer don't hesitate to accept his.
    – statox
    Dec 13, 2017 at 8:05

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.