如何将 NeoVim 设置为我的默认 text/code 编辑器?

How do I make NeoVim my default text/code editor?

有没有办法让 NeoVim 成为默认的 text/code 编辑器(没有任何不良副作用)?
相信我,我查看了很多 Whosebug question/answers 并尝试了一些方法,但对我没有任何效果。

注意:我在 macOS Big Sur(版本 11.2.1) .我想要的是当我点击文件在 NeoVim 中打开时。


--> 例如,在~/.zshrc中(并添加到~/.bash_profile也是为了以防万一)我有:

注意: zsh 是我的默认 shell

alias nvim=$HOME/nvim-osx64/bin/nvim
export EDITOR="nvim"
export VISUAL="nvim" 

当我在终端中执行 set 时,它显示:

EDITOR=nvim
VISUAL=nvim

是的,我退出并启动了终端(我正在使用 iTerm2)。我什至重新启动。

--> 我会把 my $PATH 放在这里以防万一它有什么需要做的。当我做 echo $PATH 它显示:


--> 而且,以防万一有人建议:
我不能 Select a File > Open With... 和 select NeoVim 作为默认的文本编辑器,因为那个选项不显示而且我不能 Choose Other 因为我不能select NeoVim 就是这样。


如果有人需要更多信息,请说出来,我将使用该信息编辑问题。谢谢!

在终端中设置变量不会影响 GUI 文件关联。为此,您必须更改 OS 的文件关联。

虽然它看起来是一个小项目并且不受支持,但我在使用 duti 方面有很好的体验。它是 Apple 文件扩展名 API 的包装器。配置确实花了我一分钟的时间来弄清楚。我会 post 如果我能找到它。

一段时间后我找到了我自己的问题的答案,这里是如何将 Mac 中的 NeoVim 设置为默认文本编辑器。现在,您将能够单击文件并在 NeoVim 中打开它们:


有人推荐我看下面的链接:

https://gist.github.com/Huluk/5117702

https://superuser.com/questions/139352/mac-os-x-how-to-open-vim-in-terminal-when-double-click-on-a-file


这对我不起作用,但它可以作为查找相关主题的参考(automator + neovim)。

过了一段时间,我发现了这个博客:

https://blog.schembri.me/post/neovim-everywhere-on-macos/


去看看博客,但是这里是你如何做的:

  1. 启动 Automator(Finder -> 应用程序 -> Automator
  2. 新建文档 -> 选择文档类型:Application
  3. 在“操作”中搜索 Run AppleScript 并将其拖动到显示类似“将操作拖至此处...”的位置。
  4. 删除AppleScript的默认示例
  5. 将博客中的代码(上面写着 NeoVim.app)复制并粘贴到之前具有默认代码的位置
  6. 保存新的 Automator 应用程序(另存为应用程序格式)。 Save it in the Applications folder
  7. 每次单击要打开的文件类型时,请右键单击它们(例如 .php 文件)。 Select Get Info 或执行 cmd + i,它将打开有关该文件的信息。滚动到显示 Open With 和 select Other 的位置。然后只需转到 Aplicattions folder 和 select 你的新 NeoVim“应用程序”。
  8. 如果需要,对其他文件类型执行相同的操作。
  9. 您现在可以双击您的 PHP 文件(或其他文件,如果您这样做的话)并在 NeoVim 中打开它们。享受吧!

注意:您确实需要执行 Right-ClickGet Info 并查找 Open With 以更改所有具有该扩展名的文件。如果您跳过 Get Info 并直接右键单击 + 打开方式,它将仅适用于该特定文件...


这是博客中的代码:

on run {input, parameters}
  set cmd to "nvim"
  if input is not {} then
    set filePath to POSIX path of input
    set cmd to "nvim \"" & filePath & "\""
  end if
    
  tell application "iTerm"
    create window with default profile
    tell the current window
      tell the current session to write text cmd
    end tell
  end tell
end run

这会打开一个新的 window,即使您已经打开了一个。


我将其更改为在选项卡中打开:

on run {input, parameters}
    set cmd to "nvim"
    if input is not {} then
        set filePath to POSIX path of input
        set cmd to "nvim \"" & filePath & "\""
    end if
    
    tell application "iTerm"
        tell the current window
            create tab with default profile
            tell the current session to write text cmd
        end tell
    end tell
end run

注意:我正在使用 iTerm2。如果您使用的是另一个终端仿真器,请将 iTerm 的位置更改为您的终端名称...

对于在 MacOS 上使用 Kitty 的任何人,我发现了一种使用远程控制功能来完成此操作的非常简单的方法。

首先,您需要在 kitty.conf 中设置以下内容:

allow_remote_control yes
listen_on unix:/tmp/mykitty

像@DGF 的回答一样使用 Automator,我用“运行 Shell 脚本”操作创建了一个应用程序,这是脚本:

if [ -z "$(pgrep kitty)" ]
then
    open /Applications/kitty.app
    sleep 3 # allow ample time to startup and start listening
fi

/usr/local/bin/kitty @ --to=unix:/tmp/mykitty-$(pgrep kitty) launch --type=os-window nvim "$@"

将其作为应用程序保存在某处,然后 select 从“打开方式”中将其保存!

注意:老实说,处理启动 kitty(如果它还没有 运行)的逻辑有点不稳定。但是当 kitty 已经 运行 时,它似乎工作得很好,当然对我来说大部分时间都是这样。此外,如果 kitty 是 运行 但没有 windows,它根本不起作用。 :\