[key combinations, shortcuts]

13 views
Skip to first unread message

Maxim Abalenkov

unread,
Aug 26, 2021, 6:17:25 AM8/26/21
to vim...@googlegroups.com
Dear all,

How are you? I hope all is well with you. I have a rather basic question. Recently I find myself typing a lot of text in markdown. Frequently I need to input highlighted Perl or bash source code into my markdown. To do that I use the

  {% highlight perl %}
  {% endhighlight %}

macro. I’m certain it would be possible to create a "key shortcut" in Vim, such that when I press a certain key combination, Vim would automatically insert the above macro. Would you please direct me: how is this functionality called in Vim and where can I read more about it and see some basic examples? Thank you and have a good day ahead!

Best wishes,
Maxim

Maxim Abalenkov \\ maxim.a...@gmail.com
+44 7 486 486 505 \\ http://mabalenk.gitlab.io

Stefan Klein

unread,
Aug 26, 2021, 6:42:37 AM8/26/21
to vim...@googlegroups.com
Hi,


Am Do., 26. Aug. 2021 um 12:17 Uhr schrieb Maxim Abalenkov <maxim.a...@gmail.com>:
I’m certain it would be possible to create a "key shortcut" in Vim, such that when I press a certain key combination, Vim would automatically insert the above macro. Would you please direct me: how is this functionality called in Vim and where can I read more about it and see some basic examples? Thank you and have a good day ahead!

i think you're searching for abbreviate, `:help abbreviate` and/or https://vim.fandom.com/wiki/Using_abbreviations should get you started.

regards,
Stefan

Maxim Abalenkov

unread,
Aug 26, 2021, 6:47:12 AM8/26/21
to vim...@googlegroups.com
Dear all,

I found out that this functionality is called “mapping”. I followed this tutorial (https://vim.fandom.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_1)) to create a basic map in the insert mode:

:map ,h {% highlight perl %}{% endhighlight %}

What I would like to do, is to place a newline in between the curly braces symbols “}{“ and have my cursor there, such that I can start typing the source code directly. Finally, I’m sure the “,h” key combination is not the best. Would you be able to recommend me some best practices on choosing a good key combination? Thank you and have a good day ahead!

Best wishes,
Maxim

Maxim Abalenkov \\ maxim.a...@gmail.com
+44 7 486 486 505 \\ http://mabalenk.gitlab.io

Maxim Abalenkov

unread,
Aug 26, 2021, 9:09:07 AM8/26/21
to vim...@googlegroups.com
Dear all,

After further investigation I came up with the following solution:

:imap <C-h> {% highlight perl %}<Enter><Enter>{% endhighlight %}<Up>

Please correct me, if there are any obvious mistakes. Thank you!

Best wishes,
Maxim

Maxim Abalenkov \\ maxim.a...@gmail.com
+44 7 486 486 505 \\ http://mabalenk.gitlab.io

Paul

unread,
Aug 29, 2021, 1:09:54 PM8/29/21
to vim...@googlegroups.com
On Thu, Aug 26, 2021 at 04:08:49PM +0300, Maxim Abalenkov wrote:
>After further investigation I came up with the following solution:
>
>:imap <C-h> {% highlight perl %}<Enter><Enter>{% endhighlight %}<Up>
>
>Please correct me, if there are any obvious mistakes. Thank you!

Just one: you should use inoremap instead of imap. inoremap will prevent the expansion of further maps taking place on the right-hand side. For example, if you had mapped "e" to "X" (":inoremap e X"), you'd have ended up with

{% highlight pXrl %}

{% Xndhighlight %}

Also, bear in mind that in terminal vim, <c-h> is the same as control-backspace, so either one of those keystrokes would trigger your mapping.
Reply all
Reply to author
Forward
0 new messages