macosvimneovim

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


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!


Solution

  • 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...