如何在 vim 中保存带有 UTC 时间的文件名?

发布于 2024-11-02 18:34:20 字数 453 浏览 5 评论 0原文

我正在尝试保存包含日期和时间的文件名。但是,我希望日期和时间采用 UTC 格式。这就是我正在做的:

在 vimrc 中:

cmap <F3> <C-R>=strftime("%Y%m%d%H%M")<CR>

我在保存文件时键入此内容:

:w i<F3>.txt

并且我得到一个名为:的文件,

i[localtime].txt

但我想要:

i[UTCtime].txt

有没有办法真正做到这一点,或者我永远坚持我的当地时间? Vim 是我探索各个时区的唯一方法,请帮助我。 :)

哦,顺便说一下,我将同时使用 Linux 和 Windows。

I am trying to save a file name that includes the date and time. However, I would like the date and time to be in UTC. This is what I am doing:

In vimrc:

cmap <F3> <C-R>=strftime("%Y%m%d%H%M")<CR>

I type this when I save the file:

:w i<F3>.txt

and I get a file that is named:

i[localtime].txt

but I want:

i[UTCtime].txt

Is there a way to actually do this, or am I stuck with my local time forever? Vim is my only way to explore various time zones, please help me. :)

Oh and by the way, I will be using both Linux and Windows for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最好是你 2024-11-09 18:34:20

好吧,我不确定这是否能在 Linux 上正常工作,但我相信是这样的(我在 Mac 上)。

这个想法是,您可以使用date实用程序更灵活地检索时间。它接受一个选项-u,该选项输出 UTC 时间。您所要做的就是将其包装在 system() 调用中。

cmap <F3> <C-R>=system('date -u "+%Y%m%d%H%M"')<CR>

在那里你就有了格式化的 UTC 时间。

如果插入的时间末尾出现空字符(显示为 ^@),那么您可能需要附加一个 [:-2] 将其从返回字符串,位于上面映射中的 之前。它来自系统输出的翻译后的换行符。检查 :h NL-used-for-Nul

关于窗户,我不知道。在映射之前,您可能想在 Linux 中尝试一下该实用程序,但我几乎可以肯定不会有任何差异。

Well, I'm not sure whether this will work correctly on Linux or not, but I believe so (I'm on a Mac).

The idea, is that you can use the date utility to retrieve the time with more flexibility. It accepts an option -u which outputs the time in UTC. All you have to to is wrap that in a system() call.

cmap <F3> <C-R>=system('date -u "+%Y%m%d%H%M"')<CR>

And there you have your formatted UTC time.

If a null character appears at the end of the time inserted (it appears as ^@) then you may want to append a [:-2] to strip it from the returning string, right before the <CR> in the mapping above. That comes from the translated newline the system outputs. Check :h NL-used-for-Nul.

About windows, well I have no clue. You may want to try out the utility in Linux before mapping things, but I'm almost sure there will be no differences.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文