vim - How can I make a key bind that only applies in a certain situation? -
note: bar symbol (|
) represents editor caret throughout question
i've made ultisnips snippet this:
snippet "(\w+)" "html tag" r <`!p snip.rv = match.group(1)`>$0</`!p snip.rv = match.group(1)`> endsnippet
this lets me expand word html tag, example typing "body" , pressing tab expands <body>|</body>
.
the caret placed between tags. when press return, end with:
<body> | </body>
this done keybind this:
:ino <buffer> <cr> <cr><esc>o
but don't want permanently rebind return key. want specific keybind active when caret placed between opening , closing html tag.
how can done simply?
another example when have caret placed between 2 curly brackets, so:
function() {|}
and press enter, result be:
function() { | }
again can done above key mapping, in case want active when caret placed between 2 curly brackets.
you use map-expression (see :h map-expression
) decide whether <cr>
mapped when hit it:
for example following insert mode map:
inoremap <expr> <cr> strpart(getline('.'), col('.')-2, 1) =~ '[>{]' ? '<cr><esc>o' : '<cr>'
checks charachter before cursor , if >
or {
returns <cr><esc>o
in other situations <cr>
in place of conditional ternary expression define full functional function respond in situatuion pairs; there great plugins intend such tasks gracefully:
demilitmate , auto-pairs 2 famous one.
give them try.
Comments
Post a Comment