2

Say I want to print the output of strftime("%D") in the current line, or to print the return of any user-defined function for that matter. What's the standard (one step?) way to do it?

I currently do this: :let @a=strftime("%D"), then "ap (two steps, but can be combined with mapping).

1 Answer 1

4

From command-line mode, use :put in combination with with the expression register "= (inserts after the current line):

:put =strftime('%d')<CR>

Or, in Insert mode, use Ctrlr (inserts before the cursor position, as if typed with no abbreviations or mappings):

<C-r>=strftime('%d')<CR>

Or, in Normal mode, use "= (inserts after the cursor position):

"=strftime('%d')<CR>p

Relevant help:

:h quote=
:h :put
:h i_CTRL-R
:h quote
:h p
6
  • 2
    or in insert mode: <c-r>=strftime('%d') Commented Oct 4, 2020 at 18:28
  • 2
    Or normal mode "=strftime('%d')<cr>p
    – D. Ben Knoble
    Commented Oct 5, 2020 at 0:53
  • @Jake for some reason it requires \ before the two ". I like Christian's solution better cause it inputs the date in the current line after cursor.
    – mahbubweb
    Commented Oct 5, 2020 at 4:43
  • 1
    @MahbubAlam all the ways suggested are equally valid depending on the context. I just mirrored what you said your current process was. Commented Oct 5, 2020 at 4:46
  • 1
    @MahbubAlam they can be used to start comments; it's odd that in this case single-quotes are required, as in most other places you could use double-quotes with no issue
    – D. Ben Knoble
    Commented Oct 5, 2020 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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