Kit Lessenger
Kit Lessenger

Reputation: 11

Snippet of current time in nvim

I'm trying to create a snippet for vimwiki. The snippet must change __ to current time.

local line_begin = require("luasnip.extras.expand_conditions").line_begin
return {
    s(
        {trig='__', desc="Insert current time", snippetType = "autosnippet"},
        {t("_"), t(vim.fn.system([[date +"%H:%M" | tr -d '\n']])), t({'_', ''})},
        {condition = line_begin}
    ),
}

Looks like it must work, but it past only fix time - the time of start of first vimwiki buffer. How to improve the snippet?

I tried to find in documentation how to do this, but have not find any solution.

Upvotes: 1

Views: 440

Answers (1)

lcheylus
lcheylus

Reputation: 2423

Vim has a internal strftime function to return a date/time formatted in a way you specify with a format string (see :help strftime).

In your code, replace vim.fn.system([[date +"%H:%M" | tr -d '\n']])) by vim.fn.strftime("%H:%M").

Upvotes: 1

Related Questions