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

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:
--> 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!

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.

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:
Launch Automator (Finder -> Applications -> Automator)
New Document -> Choose a type for your document: Application
In Actions search for Run AppleScript and drag that to where it says something like "Drag actions here..."
Delete the default example of AppleScript
Copy and Paste the code in the blog (where it says NeoVim.app) to where it previous had the default code
Save the new Automator app (save as aplicattion format). Save it in the Applications folder
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".
Do the same to other file types if you wish.
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...

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. :\

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

Related

MacOS Terminal: Is it possible to create a "drag-and-drop" action via script?

There are many posts that explain how to drag-and-drop things into an open Terminal window. But what I would like to do is to tell Terminal to drag-and-drop a previously selected directory onto another application like VSCode or Renamer. I have not found any documentation for that. Is it at all possible? And if so, would somebody please point me to a documentation?
UPDATE:
I'd like to clarify my question with what I intend to do:
Pre requisites:
a "work folder" contains folders and files that shall be renamed
the renaming is done by an application called "A better finder renamer" (which allows presets)
An "Automator" (MacOS app) action shall imitate these steps:
the "work folder" is right clicked
the folder is drag-and-dropped onto the ABFR, which initiates the currently active preset
other actions via bash (like 'mv .//.* ./') ...
It is the "drag-and-drop" part of the Automator action that presents a riddle for me.
The "drag-and-drop" operation is manual operation. In AppleScript, instead the command to open the file or folder is given to the destination application itself.
Second thing to keep in mind. Getting Terminal's current working directory is easy with its do script "pwd" command. But the result of the do script Terminal command returned to the script is always the window tab, not the result of the pwd shell command. One solution is to redirect (remember) the result of pwd in a temporary text file.
set tempFolder to (path to temporary items folder from user domain)
set temp to POSIX path of tempFolder & "workingDirectory.txt"
tell application "Terminal" to do script ("pwd > " & temp) in selected tab of window 1
set curDirPosixPath to paragraph 1 of (read file ((tempFolder as text) & "workingDirectory.txt"))
set curDirHFSPath to curDirPosixPath as POSIX file as Unicode text
tell application "Visual Studio Code" to open curDirHFSPath
.
NOTE: other possible solution (I don't like) is parsing the text contents of Terminal window after pwd command execution. You can get contents using property contents (of selected tab of front window).
Open Automator, choose create New Document.
Choose create new Quick Action (service).
Set workflow receives current folders in any application.
From library Add Run AppleScript action. Edit it contents:
on run {input, parameters}
set curDirHFSPath to (item 1 of input) as text
tell application "Visual Studio Code" to open curDirHFSPath
end run
Save this Quick Action as service. Now when right-clicking the folder, opens the context menu, where you can choose and run this service.

How to make julia.app launch in iTerm instead of macOS' Terminal.app?

I installed the Julia language for macOS as downloaded from the official website.
When I launch Julia, the REPL is launched in macOS's Terminal.app, but I wish to have it launched from iTerm. How can I tell julia to launch iTerm instead of Terminal.app?
The julia app uses apple script to tell the terminal to launch its process. You can modify the script and tell iTerm to be opened instead. For this:
Go to Go to Applications> press option-click (or right click if you have it) on the julia app you wish to modify and then click show package contents > contents > Resources > Scripts, and modify main.scpt.
My script reads
tell application "iTerm"
tell current window
create tab with default profile command "/Applications/Julia-1.5.app/Contents/Resources/julia/bin/julia"
activate
end tell
end tell
This makes the julia app to launch iTerm with the default profile instead of Terminal.app. It is possible that you need yo modify this path if the app is not called Julia-1.5, as in my case.
A second option is to create a symbolic link in your path to the Julia executable. For the example above, enter the following command in the terminal application of your choice:
ln -s /Applications/Julia-1.5.app/Contents/Resources/julia/bin/julia /usr/local/bin/julia
You will have to update this link to the path for the correct version whenever you upgrade Julia.
Now all you have to do to run the Julia REPL in iTerm (or any terminal application) is enter the command julia. (This assumes that /usr/local/bin is in your path.)
Add an alias in ~/.bash_profile or ~/.zshrc:
alias julia='/Applications/Julia-1.7.app/Contents/Resources/julia/bin/julia'
Change the version accordingly.
Note: Although this seems not the exact answer of the question, it leads to the same result of "making Julia launch in iTerm".

How to get clang format for Xcode 8?

After Xcode update to version 8. The very useful Alcatraz PlugIn Manager is locked out and superb utilities like clang-format, or highlight selected word occurrences, or resize the font by use of a shortcut are gone.
How can I reenable clang-format to format my current source code file on save with a template .clang-format in any parent directory of the source file?
You could create a shell script that is added to Xcode 8 as a behavior: Xcode > Behaviors > +(to create new one) > Run script: (select file here), add shortcut like Cmd+Shift+S.
The script asks Xcode to save the current document. Then it extracts its filepath and calls clang-format to format that file in-place. Clang-format has to be available e.g. by using brew as the package manager to download it and having its path published for command line access. As usual the style guide used by clang-format must have the name .clang-format and must be in any parent folder of the source file.
Here is the script:
#!/bin/bash
CDP=$(osascript -e '
tell application "Xcode"
activate
tell application "System Events" to keystroke "s" using {command down}
--wait for Xcode to remove edited flag from filename
delay 0.3
set last_word_in_main_window to (word -1 of (get name of window 1))
set current_document to document 1 whose name ends with last_word_in_main_window
set current_document_path to path of current_document
--CDP is assigned last set value: current_document_path
end tell ')
LOGPATH=$(dirname "$0")
LOGNAME=formatWithClangLog.txt
echo "Filepath: ${CDP}" > ${LOGPATH}/${LOGNAME}
sleep 0.6 ### during save Xcode stops listening for file changes
/usr/local/bin/clang-format -style=file -i -sort-includes ${CDP} >> ${LOGPATH}/${LOGNAME} 2>&1
# EOF
Please exchange the path /usr/local/bin to the one where your clang-format executable resides.
Happy coding!
The mapbox/XcodeClangFormat extension looks like a promising way to get clang format working with Xcode8.
Due to the limitations of source editor extensions, unfortunately you can only specify one .clang-format file for all your projects. "Format on save" also is not available.
Found a viable solution in this blog - code-beautifier-in-xcode
Basically, we can have clang-format running as a service by automator and invoke it through Xcode whenever we need to format the code. Refer the blog for more details.
Unfortunately your little script often does not update the formatted file in Xcode because it stops listening to file updates when saving. Increasing the sleep durations in the script does not make it more reliable and introduces a lot of waiting time for the common file-save & file-format action.
What I did in your situation was to get my mac backup and restore macOS and Xcode to the last version where all the productivity plugins from Alcatraz work fine again. This boosted my productivity.
It looks like Alcatraz plug-ins get be back to work in Xcode 8+ when unsigning them. Because I am not in the situation to try that, I can only point you to that resource:
Examine the header Installation on that github page ClangFormat-Xcode.

Opening Finder from terminal with file selected

I want to open Finder from the terminal with a specific file selected. I know that by using open . I can open the current directory in Finder, but I also want to select some file in the Finder window.
The basic thing I want to do is run a script that randomly selects a file among many in a folder and for that I need to open a new Finder window with the file selected.
The . in your open . command just means path at current location (which would be a folder) so open decides that the correct application to use is Finder. If you were to do open myTextFile.txt which is at your current location in the terminal open will decide to use a text editor instead. You can however specify the application to open the file with by using the -a flag so your command would look like this: open -a Finder myTextFile.txt.
What Faisal suggested will also work, the -R flag is an equivalent to using ⌘↩ (Command Return) in Spotlight.
this and some other nice shell tricks with the open command are described in this post: Shell tricks: the OS X open command
For me, code below works fine.
open -R your-file-path
You can do it like that
osascript -e "tell application \"Finder\"" -e activate -e "reveal POSIX file \"<your file path>\"" -e end tell

Open an ipython notebook via double-click on osx

I've downloaded a couple of ipython notebooks, and I'd like to open them in browser tabs without navigating to the directory I've downloaded them to and running ipython notebook notebook_name.ipynb.
I realize this probably means I'm lazy, but it seems like a common use case to me. Am I missing something obvious?
Use Pineapple application for opening and working on your IPython/Jupyter notebooks. It is pretty cool.
Update:
Now there is nteract, which is a new jupyter-like Desktop app. After installing it, make it the default app for opening .ipynb files. Then just double-click any notebook to start it right away.
I have found a nice way using Automator (Oct. 2017; with information from here):
open Automator and create a new Application
to add Run Shell Script drag and drop it from the list; might need these settings Shell: /bin/bash and Pass input: as arguments
insert the code below; if necessary adjust the path to jupyter
Code
#!/bin/sh
variable="'$1'"
the_script='tell application "terminal" to do script "/usr/local/bin/jupyter notebook '
osascript -e "${the_script}${variable}\""
save the script as an application (!)
try to open a .ipynb file and change the default app to this newly created one.
notes
This will open a terminal and run the jupyter notebook command, such that you can interrupt and stop the notebook-server from there.
Also note that you cannot test the app like that in Automator, but need to add the Get Specified Finder Items and insert some test notebook there (just for testing purposes).
As pointed out in the comments, there are two more notes:
To avoid spamming your browser history with notebooks, you can start your notebooks in incognito/private mode: Run jupyter notebook in incognito window
If you want to run notebooks in one server and don't mind an extra tool, Sachit Nagpal has pointed out (thank you), that one could also use nbopen. To use this workflow just replace "/usr/local/bin/jupyter notebook ' with "nbopen '. Any other tool should work alike.
pip install nbopen.
open Automator, create new Application
Drag'n drop Run Shell Script
Change Pass input to as arguments
Copy/paste this script:
variable="'$1'"
the_script='tell application "terminal" to do script "nbopen '
osascript -e "${the_script}${variable}\""
Save the new application to Applications directory as nb_open
Right click any ipynb file and select "Open with > Other" and select the nb_open in the Applications folder. Don't forget to check "Always Open With".
Select an ipynb file, get info (command + i) > Open With (select nb_open if not selected already) > Click Change All.... Done.
The application posted here worked pretty well for me: http://bioequity.org/2013/11/16/ipynbviewer/
You also need to download iTerm2, as described on that page.
Note that this doesn't work if there are spaces in the filename, but you can modify it so that it works with spaces in the filename. Control-click on the iPyNbViewer.app and select "Show package contents". Edit the file Contents/Resources/Scripts/main.scpt. Change three instances of "POSIX path" to "quoted form of POSIX path". Now it will work with spaces in the filename.
To set all of your .ipynb files to open with the app, you'll need to Get Info (command-I) on one of the files and select the iPyNbViewer app to open all .ipynb files.
It would be great if this was the default behavior of double-clicking on an iPython notebook file...
I came up with a way of doing it on Ubuntu. (It works for me but I can take no responsibility). It's explained here. In a nutshell, you need to create a new MIME type, then write a script that works as the app that launches them:
#!/bin/bash
netstat -tln |grep "8902"
# if not found - equals to 1, start it
if [ $? -eq 1 ]
then
ipython notebook / --no-browser --port=8902 &
sleep .5
fi
xdg-open http://localhost:8902/notebooks$1
This always opens the notebook server on port 8902, but first checks whether there is already a server running, and, if so, uses it.
Then you can use ubuntu tweak to select your script as a default application for the MIME type "IPython Notebook" you just created.
Not very elegant, but worth it, in my opinion.
PyCharm now supports Jupyter ipynb files:
which is from the documentation https://www.jetbrains.com/help/pycharm/editing-jupyter-notebook-files.html.
But I think this feature is only available in the Professional version now; hopefully it will be added to the Community version in the future.
Look at this link.
Put a bash script in the folder where you keep your ipython notebooks and simply double click it to open up a notebook instance. From the link above, the bash script has just:
path=$0 # path is path to this file
cd ${path%/*.*} # clip off the file name to get directory path and cd
ipython notebook --pylab inline
Finally, you need to chmod u+x the script to make it executable and you're done.
I have used the command line application 'nbopen' and put it in a Platypus wrapper to get drag'n drop and double click opening on Macos. 'nbopen' is downloadable using 'pip'
It works well when used as described above by DanHickstein.
Only problem with my code is that it requires the full path to the nbopen command. I know I should be able to use 'which nbopen' somehow but can't get it to work.
Heres my Platypus code:
#!/bin/bash
# Opens ipynb files in a Jupyter Notebook
# echo $1
# $1 is the path of the dropped file
/Users/robw/anaconda/bin/nbopen $1
# Based on an idea from
# https://www.quora.com/Is-there-a-straightforward-way-to-open-an-IPython-Notebook-in-Windows-by-double-clicking

Resources