尝试编写 Vim 函数/命令?

发布于 2024-12-11 13:57:37 字数 547 浏览 0 评论 0原文

我正在使用 Janus(尽管这对于理解这个问题并不是关键)。基本上他们所做的就是将 :e 重写为 :Edit 以与 NERDTree 一起使用,但 NERDTree 不支持强制打开文件(例如 e!)。所以我写了一些重写的​​东西:编辑!到 :e!,但我喜欢只使用 :Edit! 的好处,所以我试图欺骗它,就好像它已经内置一样。这就是我想到的:

ca Edit! e! <bar> Edit

唯一的问题是,你可以'对于尚未保存至少一次的文件,不要执行此操作。但我知道这样做

ca Edit! bd! <bar> Edit

会起作用:它将关闭未保存的缓冲区并继续前进,就像什么都没发生一样。我所需要做的就是编写逻辑,以便始终在正确的场景中使用正确的命令。最好逻辑不会在每个页面上运行:只有当我实际运行命令时,它才应该尝试找出答案。有什么想法吗?

编辑:我尝试询问 #vim 频道(给了我很多帮助)...他们说使用 Expand("%") 并检查它是否存在可能对我的情况有帮助?不知道,只是尽可能提供详细信息。

I'm using Janus (isn't pivotal to understand the question though). Basically what they do, is rewrite :e to be :Edit to work with NERDTree, but NERDTree doesn't support force opening a file (e.g. e!). So I had written something that rewrites :Edit! to :e!, but I like the benefits of using just :Edit!, so I'm trying to spoof if as if it was already built in. This is what I came up with:

ca Edit! e! <bar> Edit

The only problem, is that you can't do that for files that haven't been saved at least once. But I know doing

ca Edit! bd! <bar> Edit

will work: it will close the unsaved buffer and keep moving like nothing happened. All I need to do is write logic that will use the correct command in the right scenario all the time. Preferably the logic doesn't run on every page: only when I actually run the command should it attempt to figure it out. Any ideas?

Edit: I tried asking the #vim channel (gave me tons of help)... they said using expand("%") and checking to see if it exists might help my case? Don't know, just providing as much detail as I can.

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

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

发布评论

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

评论(1

风轻花落早 2024-12-18 13:57:37

试试这个。定义一个这样的函数:

function! CommandAbbrForEdit()
    if &modified
        return 'bd! | Edit'
    endif
    return 'e! | Edit'
endfunction

然后定义你的缩写,如下所示:

cabbrev <expr> Edit! CommandAbbrForEdit()

Try this. Define a function like this:

function! CommandAbbrForEdit()
    if &modified
        return 'bd! | Edit'
    endif
    return 'e! | Edit'
endfunction

Then define your abbreviation like this:

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