0

I often use abbreviations when using vim editor. Now I want to use Linux commands when defining vim abbreviations inside /etc/vimrc.

I tried various things but none worked.

My use case:

Inside vim: When I type #! and enter "tab" key, Then the output should be:

   #! /usr/bin/env bash
   #Author: Dhimant
   #Date: Realtime date at which command is fired
   #Description:

I completed up to first two lines by using "cr" tag

ab #!   #!/usr/bin/env bash<cr>#author: Dhimant Thanki

But when using "date command", it is not working

ab #!   #!/usr/bin/env bash<cr>#author: Dhimant Thanki <cr>#Date: `date` <cr>#Task: (NOT WORKING)

So need help in completing this task. Any help is appreciated.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • This is generally what snippet managers do. I’ve written a template/skeleton program and am working to integrate it to vim which accomplishes slightly different goals. https://github.com/benknoble/tmplr – D. Ben Knoble Aug 03 '19 at 20:49
  • You should use snippets to achieve what you want. – jdhao Aug 06 '19 at 06:51

3 Answers3

1

You can use <C-r>= to insert a Vim expression:

iabbr xxx # Date: <C-r>=strftime('%c')<CR><CR># Second line

Note the two <CR>s: one to end the expression, and one to insert a newline.

I used strftime() here instead of the date shell command, as that's easier and will work on all platforms, but you can also use <C-r>=system('date')[:-2]<CR> if you prefer (the [:-2] is to remove trailing newline).

See: :help abbreviations, :help c_<C-R>.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
1

You should use snippet engine to achieve what you want. First, you need to install ultisnips. See here on how to set up ultisnips to recognize your own snippets.

Under your custom snippet directory, create a file named all.snippets and add the following to this file:

snippet #! "bash file header"
#Author: Dhimant
#Date: `!v strftime('%c')`
#Description: just for a test
endsnippet

In the above snippet, !v is used to execute a Vim command and in this case, strftime() to get the current time.

Then open a new file and type #! and press Tab. Boom, your custom header is created. enter image description here

jdhao
  • 24,001
  • 18
  • 134
  • 273
0

The <cr> you entered will be interpreted as literally the letters <cr>

In order to go down to a new line you can use <ESC> to go back to command mode then use o.

O - Begin a new line above the cursor and insert text, repeat [count] times.

o - Begin a new line below the cursor and insert text, repeat [count] times.

For example:

ab #! #!/usr/bin/env bash<ESC>o#author: ErectCrested<ESC>o#Some more text<ESC>o

Use the last <ESC>o if you want to start insert on a new line after the abbreviation, otherwise remove it.

Community
  • 1
  • 1
ErectCrested
  • 56
  • 1
  • 10
  • Hi ErectCrested, Actually I have used and it is working properly. But my question was to use Linux commands inside vim abbreviations. But no worries, I got the answer. Thank you. – Dhimant Thanki Aug 03 '19 at 14:24