0

I know how to make an abbreviation to a command in vim. For example to print the date, I have this in .vimrc:

iabbrev mydate <C-R>=strftime("%a %d/%m/%Y")

Now I'm running a VM with FreeBSD as a guest. Base system has good old vi/nvi.

I can print the date with:

:r !date "+%a %d/%M/%Y"

But how can I make an abbreviation to the above command?

I've tried:

:ab mydate :r !date "+%a %d/%M/%Y"

but this will not work.

Any ideas?

EDIT 1

Correction. In vim I actually have:

iabbrev mydate <C-R>=strftime("%a %d/%m/%Y")

not

:ab date_ ^[:r!date -I^MkJA

The rest of the question stands.

EDIT 2

D. Ben Knoble,

I made a mess with the original question.

To clarify:

I'm using vi/nvi on FreeBSD. (Version 2.1.3 (2015-04-08) The CSRG, University of California, Berkeley.)

"iabbrev" and "nnoremap" are Vim commands, not vi/nvi commands.

4
  • You need to escape the % Nov 13 '19 at 21:30
  • 1
    In the future, please use edit to update the question.
    – D. Ben Knoble
    Nov 14 '19 at 1:49
  • See my edits regarding historical compat.
    – D. Ben Knoble
    Nov 14 '19 at 14:17
  • Also consider installing Vim on your FreeBSD VM. It's definitely available from the Ports repository and should be pretty easy to install.
    – filbranden
    Nov 28 '19 at 15:11
2

I finally found a solution:

I put this in ~/.nexrc:

ab mydate ^[:r !date "+\%a \%d/\%m/\%Y"^MkJA

Where:

^[ = CTRL+v+ESC

and

^M = CTRL+v+ENTER

Many thanks to Antony_ on Freenode channel ##vi for the solution.

1
  • A precision for those who are not used to the notation used above: ^[ is what appears on the screen and CTLR+v ESC is what you should actually type.
    – Damien L
    Jul 28 at 13:53
1

You can create a mapping for it

nnoremap keys :read !date...<CR>

Or you can use <C-o>:

iabbrev keys <C-o>:read !date...<CR>

For historical (vi) compatibility, I believe one can do map! and abbrev—these are listed in Vim under :help vi-features as supported vi features (though I cannot find them in the POSIX spec, they are mentioned). Note that abbreviations are used in Insert, Replace, and Command mode, while map!s are used in Insert and Command mode. Choose keys carefully. You'll probably also need Ctrl-vEnter to insert the literal, instead of <CR>.

Peter points to in the comments <C-r>=get(systemlist('date'),0—not sure how this interacts with pure vi, however.

1
  • 1
    Or use <c-r> with expression register, e.g. iabbrev keys <c-r>=get(systemlist('date ...'),0)<cr> Nov 15 '19 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.