在记事本中按下F5键插入日期时间

2024-06-09 11:13:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我做的随便吧。py首选项中的文件-浏览包-用户(文件夹)

import sublime, sublime_plugin, time
class InsertDatetimeCommand(sublime_plugin.TextCommand):
def run(self, edit):
    sel = self.view.sel();
    for s in sel:
        self.view.replace(edit, s, time.strftime( '%H:%M %d/%m/%Y' ))

那么

在preferences-keybindings-User中,添加了以下行:

^{pr2}$

按F5键,我可以插入日期时间,但当我这样做的时候,这个日期时间是被选中的,当我按enter键转到新行时,它被删除了。你知道在按F5键后,我应该在上面的代码中更改哪个部分不选择所有日期时间字符串吗?在


Tags: 文件用户pyimportself文件夹viewtime
1条回答
网友
1楼 · 发布于 2024-06-09 11:13:24

我建议使用view.insert在选择的第二个结尾添加字符串:

class InsertDatetimeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sel = self.view.sel()
        for s in sel:
            self.view.insert(edit, s.b, time.strftime( '%H:%M %d/%m/%Y' ))

replace的问题是您要用Selection替换区域。因此,在运行命令之后,时间字符串被选中,因此当您点击Enter时,它将被替换。您可以执行类似sel.clear()或修改每个区域来修复它。在

更新:
正如OdatNurd所说,在这种情况下,使用s.a将被混淆。使用s.b将更加一致。在

相关问题 更多 >