0

in my .vimrc i have an autocmd which cleans up my files, but how can i make a mapping out of the command?

autocmd BufWritePre *.test1 silent! :%s/\s\+$//e | silent! :%s/\(\n\n\)\n\+/\1/ | silent! :%s#\($\n\s*\)\+\%$## | :%s/;$
nnoremap ;; silent! :%s/\s\+$//e | silent! :%s/\(\n\n\)\n\+/\1/ | silent! :%s#\($\n\s*\)\+\%$## | :%s/;$<CR>

The mapping fails saying

Error detected while processing ~/.vimrc:
line  966:
E486: Pattern not found: ;$<CR>
1

So the main problem with your command is that by using | you're actually breaking it into several commands, the first of which being nnoremap, but from the second on, a series of silent! commands with substitutions, which are being run at the time your .vimrc is being sourced, rather than becoming part of the command.

Using either \| or <bar> should solve that.

The second issue is that nnoremap expects a command in normal mode, so it takes your silent! and runs the s (substitute) command, which then replaces the character under the cursor with ilent! ..., which is not what you want... You need an extra : before that first silent! so it knows to go into command mode.

And third, your last command %s/;$<CR> is not really well formed, you need two additional /s to complete it. I'm assuming your intention is to strip all ;s at the end of lines, so %s/;$// is what I'm assuming you want here. (I also noticed you skipped a silent! there, maybe by omission? I'm adding it here, just in case.)

Putting it all together:

nnoremap ;; :silent! :%s/\s\+$//e \| silent! :%s/\(\n\n\)\n\+/\1/ \| silent! :%s#\($\n\s*\)\+\%$## \| silent! :%s/;$//<CR>
| improve this answer | |
  • 1
    thx a lot. you got everything right. thx for the explanations too! – divramod Aug 15 '18 at 5:39

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.