vim: how can i make a mapping from a autocmd

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$
nnoremap ;; silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$<CR>
The mapping fails saying
Error detected while processing ~/.vimrc:
line 966:
E486: Pattern not found: ;$<CR>
vim vimrc
add a comment |Â
up vote
0
down vote
favorite
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/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$
nnoremap ;; silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$<CR>
The mapping fails saying
Error detected while processing ~/.vimrc:
line 966:
E486: Pattern not found: ;$<CR>
vim vimrc
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$
nnoremap ;; silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$<CR>
The mapping fails saying
Error detected while processing ~/.vimrc:
line 966:
E486: Pattern not found: ;$<CR>
vim vimrc
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/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$
nnoremap ;; silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$<CR>
The mapping fails saying
Error detected while processing ~/.vimrc:
line 966:
E486: Pattern not found: ;$<CR>
vim vimrc
vim vimrc
asked Aug 15 at 5:03
divramod
1225
1225
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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/(nn)n+/1/ | silent! :%s#($ns*)+%$## | silent! :%s/;$//<CR>
1
thx a lot. you got everything right. thx for the explanations too!
â divramod
Aug 15 at 5:39
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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/(nn)n+/1/ | silent! :%s#($ns*)+%$## | silent! :%s/;$//<CR>
1
thx a lot. you got everything right. thx for the explanations too!
â divramod
Aug 15 at 5:39
add a comment |Â
up vote
1
down vote
accepted
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/(nn)n+/1/ | silent! :%s#($ns*)+%$## | silent! :%s/;$//<CR>
1
thx a lot. you got everything right. thx for the explanations too!
â divramod
Aug 15 at 5:39
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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/(nn)n+/1/ | silent! :%s#($ns*)+%$## | silent! :%s/;$//<CR>
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/(nn)n+/1/ | silent! :%s#($ns*)+%$## | silent! :%s/;$//<CR>
answered Aug 15 at 5:27
Filipe Brandenburger
3,734622
3,734622
1
thx a lot. you got everything right. thx for the explanations too!
â divramod
Aug 15 at 5:39
add a comment |Â
1
thx a lot. you got everything right. thx for the explanations too!
â divramod
Aug 15 at 5:39
1
1
thx a lot. you got everything right. thx for the explanations too!
â divramod
Aug 15 at 5:39
thx a lot. you got everything right. thx for the explanations too!
â divramod
Aug 15 at 5:39
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462668%2fvim-how-can-i-make-a-mapping-from-a-autocmd%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password