Reputation: 792
I am working on a jinja file in vim which was poorly formatted for some reasons. I have many variables in curly brackets with a space left in between the curly brackets. Ex:
this is my jinja variable { { foo } }
I would like to remove the extra space between the curly brackets. The desired outcome is
This my jinja variable {{ foo }}
This just works fine if I chain two substitute commands in the command mode.
:%s/{ {/{{/g | %s/} }/}}/g
However, if I wrap the substitute commands in a mapping noremap <leader>cb :%s/{ {/{{/g | %s/} }/}}/g <CR>
, only the first substitution is executed, but not the second. Here is the corresponding output
This my jinja variable {{ foo } }
What am I doing wrong here?
Upvotes: 1
Views: 50
Reputation: 3063
You can do it using one substitution by taking advantage of capture groups:
nnoremap <leader>cb :%s/{ {\(.*\)} }/{{\1}}/<CR>
Not that this will fail if you have the pattern twice on a line like:
{ { hello } } ... { { world } }
because it'll be turned into:
{{ hello } } ... { { world}}
Someone might come along with an actual explanation on why your way doesn't work, so you should probably not accept this answer.
Upvotes: 1
Reputation: 196526
Your mapping doesn't work because of the |
:
noremap <leader>cb :%s/{ {/{{/g | %s/} }/}}/g <CR>
^
You can rewrite it in several ways:
noremap <leader>cb :%s/{ {/{{/g \| %s/} }/}}/g <CR>
noremap <leader>cb :%s/{ {/{{/g <bar> %s/} }/}}/g <CR>
etc.
See :help map-bar
.
By the way, :noremap
covers too many modes, which you might find problematic in the long run. If you want a normal mode mapping, be explicit about it:
nnoremap <leader>cb :%s/{ {/{{/g <bar> %s/} }/}}/g <CR>
Upvotes: 1