Score:1

Nautilus open text file with vim and make terminal persist after closing

de flag

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.

Score:2
cn flag

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

lucidbrot avatar
de flag
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, wit`file="$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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.