如何将NeoVim设置为默认的文本/代码编辑器?

7
有没有办法将NeoVim设置为默认的文本/代码编辑器(没有任何不良影响)?相信我,我看了很多StackOverflow的问答并尝试了一些方法,但都没有成功。
注意:我使用的是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 时,它显示:

<code>EDITOR=nvim
VISUAL=nvim
</code>

是的,我退出并打开了终端(我使用 iTerm2)。 我甚至重新启动了。

--> 为了防止这与$PATH有关,我在此处放置路径。 当我执行echo $PATH时,显示如下:

enter image description here


--> 另外,如果有人建议:
由于该选项未显示,我无法通过选择文件 > 打开方式... 并选择NeoVim作为默认文本编辑器,并且我无法通过选择其他人选... 来选择NeoVim。


如果需要更多信息,请告诉我,我会编辑问题并提供相关信息。 谢谢!


请参见 https://dev59.com/MGox5IYBdhLWcg3wLhii#65359498。 - xdhmoore
请将默认文本编辑器更改为所有基于文本的文件,无论扩展名如何 - xdhmoore
5个回答

5

过了一会儿,我找到了自己问题的答案,以下是如何将NeoVim设置为Mac上的默认文本编辑器。现在,你可以单击文件并在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. 新建文档 -> 选择文档类型:应用程序
  3. 在操作中搜索运行AppleScript,并将其拖到类似“拖放操作”的位置
  4. 删除默认的AppleScript示例
  5. 复制并粘贴博客中的代码(在其中写着NeoVim.app)到之前具有默认代码的位置
  6. 保存新的Automator应用程序(保存为应用程序格式)。将其保存在应用程序文件夹中
  7. 右键单击您希望每次单击它们时打开的文件类型(例如.php文件)。选择获取信息或执行cmd + i,它将打开有关该文件的信息。滚动到打开方式所在的位置,然后选择其他。然后只需转到应用程序文件夹并选择您的新的NeoVim“应用程序”。
  8. 如果您愿意,可以对其他文件类型执行相同的操作。
  9. 现在,您可以双击您的PHP文件(或者如果您做了相同的事情,则是其他文件),并在NeoVim中打开它们。享受吧!

注意:您需要右键单击,选择“获取信息”,并查找“打开方式”以更改所有该扩展名的文件。如果您跳过“获取信息”,只是右键单击+选择“打开方式”,它只会针对该特定文件起作用...


这是来自博客的代码:
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

这将会打开一个新的窗口,即使你已经打开了一个窗口。
我把它改成了在选项卡中打开:

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更改为您终端的名称...


4

在终端中设置变量不会影响GUI文件关联。要更改操作系统的文件关联,您需要改变它的文件关联。

尽管它看起来是一个小型项目且缺乏支持,但我使用duti时有过良好的经验。它是苹果文件扩展API的包装器。配置让我花了一分钟去弄清楚。如果我找到了,我会发帖子。


1
对于任何想要在NeoVim中使用Alacritty的人,这里有一个完整的Applescript,不使用任何shell命令。它可以用Alacritty和NeoVim打开任何你想要的文件。
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

    my alacritty_win()
    tell application "System Events"
        keystroke cmd
        key code 36
    end tell
end run

on alacritty_win()
    set _running to (application "Alacritty" is running)
    tell application "Alacritty" to activate
    tell application "System Events"
        repeat while (name of first application process whose frontmost is true) is not "Alacritty"
            delay 0.05
        end repeat
        set _alacritty to first application process whose frontmost is true
        -- If Alacritty was running, create a new window to run command
        if _running then
            tell _alacritty to set _target to (count windows) + 1
            keystroke "n" using {command down}
        else
            set _target to 1
        end if
        -- Wait for wanted window count
        tell _alacritty
            repeat while (count windows) < _target
                delay 0.05
            end repeat
        end tell
    end tell
end alacritty_win


请注意:您需要在Mac设置中给予应用程序“neovim.app”访问辅助功能的权限。
请按照@DGF的回答操作,应用此脚本并创建一个名为“neovim.app”的应用程序,并像正常应用程序一样使用它。

0

对于在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 "$@"

将其保存为应用程序,并从“打开方式”中选择它!

注意:老实说,处理启动kitty(如果它尚未运行)的逻辑有点不稳定。但是,当kitty已经在运行时,它似乎运行得非常好,这对我来说当然大多数时间都是这样的。此外,如果kitty正在运行但没有窗口,则根本无法工作。:\


-1

通过像这样的txt文件子菜单选择nvim作为默认应用程序,以便预览PDF:

https://cdn.osxdaily.com/wp-content/uploads/2011/10/set-pdf-files-open-with-preview-mac-os-x.jpg


为什么要给我负评?那会很有帮助。我无法测试,因为我没有Mac电脑。如果翻译不正确,我可以删除它,但我需要反馈。 - questionto42

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接