6

Is there a way to make NeoVim as default text/code editor (without any bad side effects) ?
Trust me, I looked to lots of StackOverflow question/answers and tried a few things but nothing worked for me.

Note: I'm on macOS Big Sur (version 11.2.1). What I want is when I click on files to open in NeoVim.


--> For example, in ~/.zshrc (and added to ~/.bash_profile also just in case) I have:

Note: zsh is my default shell

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

When I do set in Terminal it shows:

EDITOR=nvim
VISUAL=nvim

And yes, I quit and started the terminal (I'm using iTerm2). I even reboot.

--> I will place my $PATH here just in case it has anything to do it that. When I do echo $PATH it shows:

enter image description here


--> And, just in case someone suggests:
I can't Select a File > Open With... and select NeoVim as default text editor, since that option doesn't show and I can't do Choose Other since I can't select NeoVim in that way.


If anyone needs more information, please say and I will edit the question with that info. Thanks!

DGF
  • 330
  • 1
  • 4
  • 11
  • See https://stackoverflow.com/questions/9370584/set-textmate-as-the-default-text-editor-on-mac-os-x/65359498#65359498 – xdhmoore Feb 21 '21 at 08:09
  • And https://stackoverflow.com/questions/43011299/how-to-change-default-text-editor-for-all-kind-of-text-based-files-no-matter-wh – xdhmoore Feb 21 '21 at 08:10

4 Answers4

4

Setting variables in the terminal will not affect the GUI file associations. To do that you have to change the OS's file associations.

Though it appears to be a small project and unsupported, I've had a good experience using duti. It's a wrapper around the Apple file extension API. The configuration did take me a minute to figure out. I'll post it if I can find it.

xdhmoore
  • 8,935
  • 11
  • 47
  • 90
3

After a while I found the answer to my own question, here it is how you can set NeoVim in Mac as the default text editor. Now, you will be able click on files and opening them in NeoVim:


Some people recommended me to have a look at the follow links:

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


That didn't work for me but it served as a reference to look up related topics (automator + neovim).

After a while, I discover this blog:

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


Go and have a look at the blog, but here it is how you do it:

  1. Launch Automator (Finder -> Applications -> Automator)
  2. New Document -> Choose a type for your document: Application
  3. In Actions search for Run AppleScript and drag that to where it says something like "Drag actions here..."
  4. Delete the default example of AppleScript
  5. Copy and Paste the code in the blog (where it says NeoVim.app) to where it previous had the default code
  6. Save the new Automator app (save as aplicattion format). Save it in the Applications folder
  7. Right-Click a file type you wish to open every time you click on them (e.g. .php file). Select Get Info or do cmd + i, it will open informations about that file. Scroll to wher it says Open With and select Other. Then just go to Aplicattions folder and select your new NeoVim "app".
  8. Do the same to other file types if you wish.
  9. You can now double click on your PHP files (or others if you did the same) and open them in NeoVim. Enjoy!

Note: You really need to do Right-Click, Get Info and look for Open With to change in all files with that extension. If you skip Get Info and just Right-Click + Open With, it will only work for that specific file...


This is the code from the blog:

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

This would open a new window even if you already had one open.


I change it so that it would open in a tab:

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

Note: I'm using iTerm2. If you are using another Terminal Emulator, change where it says iTerm to the name of your terminal...

DGF
  • 330
  • 1
  • 4
  • 11
0

For anyone using Kitty on MacOS, I found a pretty simple way to accomplish this using the remote control feature.

First you need the following set in your kitty.conf:

allow_remote_control yes
listen_on unix:/tmp/mykitty

Using Automator like in @DGF's answer, I created an Application with the "Run Shell Script" action, and this is the script:

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 "$@"

Save that as an application somewhere, and select it from "Open with"!

Note: to be honest, the logic to handle starting up kitty if it's not already running is a little flaky. But it seems to work great when kitty is already running, which of course it is most of the time for me. Also, it doesn't work at all if kitty is running but has no windows. :\

LinusR
  • 1,149
  • 10
  • 17
-1

Choose nvim as the default application by means of a txt file sub-menu like here with Preview for PDFs:

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

questionto42
  • 7,175
  • 4
  • 57
  • 90
  • Downvote for what? That would help. I cannot test it, I do not have a Mac. If it is just wrong, I can remove it, but I need feedback. – questionto42 Jun 07 '23 at 20:13