1

I would like to open a (text) file with Vim in nautilus. This part is easy: Right-Click -> Open With Vim starts a terminal with vim inside running, file open already.

When I close vim, the terminal closes too. I would like the terminal to remain open, with the working directory set to the directory the file is in.

I'm sure there is a simple way to achieve this by creating a custom bash script and adding it to the context menu (or even as the default action on Enter when the file is selected) but I don't know how. So, How to do this?

I am on ubuntu 18.04 but I hope it is similar for later versions.

1 Answer 1

2

Maybe not exactly what you want, but the following script does the thing you want, when put as a Nautilus script under the ~/.local/share/nautilus/scripts/ directory:

#!/bin/bash
file=$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | head -1)
dir=$(dirname "$file")
##zenity --info --no-wrap --no-markup --title="DEBUG" --text="$file : $dir"
gnome-terminal -- /bin/bash -c "/bin/vim \"$file\" ; cd \"$dir\" ; exec /bin/bash"

Usage: Please, add the above script contents as a new file with a meaningful name (like VIM) under the ~/.local/share/nautilus/scripts/ directory. Then, using Nautilus, select a single text file, right click and on the popup menu select ScriptsVIM.

__

Disclaimer: Tested only under Ubuntu 21.10


If you want to insert the above functionality into the vim.desktop file provided by the system, you can "localize" the contents of this file by copying it to the directory ~/.local/share/applications/ and changing the line Exec=vim %F to call your custom shell script which can be created based on the script I provided above (Hint: Replace the line file=… in my script with file="$1" in your new script.)

1
  • 1
    Thanks! This works perfectly. In the copy of the vim desktop file I replaced Exec=vim %F with Exec=/opt/persistentvim.sh %U (where the bash script of yours is pasted inside, witfile="$1" ), and changed the name of the copied desktop file and inside gave it a different display name. Then all that remained to do was to right click my file and in the properties set the default "open with" to this version of vim. Now I can open it with doubleclick or enter, like I wanted to.
    – lucidbrot
    Nov 25, 2021 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.