When working with Jinja templates and YAML for Ansible configuration, I often copy+paste a variable name (say, nginx_root
) and then need to wrap it in double curly braces: {{ nginx_root }}
. This gets cumbersome to do manually, so I want to be able to type a Vim key binding to wrap the current word under the cursor in double curly braces.
Using the vim-surround plugin, I can add the desired curly braces to the word under the cursor via ysiw}lysiw{
, but that is many more key strokes than I would prefer. I have tried the following Vim key binding, but invoking it in normal mode has no visible effect at all:
" Wrap text under cursor with double curly braces (e.g., for Jinja variables)
nnoremap <C-J> ysiw}lysiw{
Ergo, my questions are:
Using stock Vim functionality, the vim-surround plugin, and/or any other combination of tooling, how can one bind a key that will wrap the current word under the cursor with space-padded double curly braces? e.g.,
nginx_root
→{{ nginx_root }}
Is there a way to also achieve this in insert mode, with the cursor just to the right of the word? (
nginx_root*
, where*
is the cursor position)How would one bind a key (preferably available in both normal and insert modes) to insert
{{ * }}
at the current cursor position, where*
is the position of the cursor after insertion? (This would facilitate entering new Jinja variables, versus operating on copy+pasted variables as noted above.)