Trying to make a desktopfile for kio's context menu for opening files in a program

Hello, i am trying to make a desktopfile for kio’s context menu for opening files in a program and i am having some issues. Right now the context menu option is only showing for certain types of files not all of the text based ones and when i click on the context menu option i made the program launches normally but the file that was supposed to go into the program did not pass. I have tried to use %f %F %u and %U. Im on kde plasma 6.5.2 and the program that i am trying to open files to is neovim. Here is the code that i got so far.

[Desktop Entry]

Type=Service

X-KDE-Priority=TopLevel

MimeType=text/plain;text/*;

Actions=OpenInNeovim;

Terminal=true

[Desktop Action OpenInNeovim]

Name=Open in Neovim

Exec=nvim %f

it helps fro readability to encase your code in triple back ticks like this

[Desktop Entry]

Type=Service

X-KDE-Priority=TopLevel

MimeType=text/plain;text/*;

Actions=OpenInNeovim;

Terminal=true

[Desktop Action OpenInNeovim]

Name=Open in Neovim

Exec=nvim %f

you seem to be missing the the key-value pair

ServiceTypes=KonqPopupMenu/Plugin

text/plain should work but so should text/* so i would say just pick one.

is nvim on your path? will it run if you type it into a terminal?

if you want this service menu to open a terminal with nvim running it, you should use something like

Exec=konsole --hold -e nvim %f

Changed the setting that you told me still got the same issues. changing it to the konsole hold command opened up 2 terminal windows. 1 completely blank and the other with a empty instance of nvim as before. direclty putting the path of the file you want to open after nvim open the file correctly. putting the %f inside ‘‘ or ““ gives me a warning about a swap file
here is how the code stands now

[Desktop Entry]
Type=Service
X-KDE-Priority=TopLevel
MimeType=text/*;
Actions=OpenInNeovim;
Terminal=true
ServiceTypes=KonqPopupMenu/Plugin

[Desktop Action OpenInNeovim]
Name=Open in Neovim
Exec=nvim – %f

the issues seems to be with nvim

with your line changed to Exec=nano %f the service menu works as expected.

i don’t have nvim installed so i can’t test it, but you can type nvim --help to get more info

once you get the nvim command to work in a terminal, then you can just paste that command into your .desktop file.

Nano does not seem to work either for me for some reason

You have documentation at Creating Dolphin service menus | Developer

For your use case, you could just mime-type association to open those files with nvim.
You can use middle cilck on a file to open a file with the second application associated with the mimetype.

1 Like

does vi work?

you might have other issues with your distro.

same as nano, program opens but not with the file that is passed with the %f

Managed to get it to open correctly by just setting it as the default editor. The context menu open in nvim still does not work tho. im not sure why the system cant parse the % variables. Do you have any tips on how i can debug this?

what happens if you make it

Exec=konsole --hold -e echo hello

i get a new terminal window with the word “hello” followed by the path to the item i had clicked on to get the context menu.

and if i change echo hello to just vim, then i get the file open in a terminal with the vim editor.

echo hello opens 2 terminals, 1 blank and 1 with the word hello. the one with vim opens one blank and one with a empty vim session.

what folder are you keeping the .desktop file in?

sounds like a bug or something about your install is borked.

does running vim on a command line with the file name work as expected?

Try desktop-file-validate

The %* values correspond to

2 Likes

i would like this tool to be useful, but at present v0.27 is of little use.

this utility seems to throw a lot errors for things that are actually working.

have you used this on any of your service menus?

  • it really doesn’t like any sort of bash scripting in the Exec key value without double quotes and adding these breaks the scripting (unless i’m doing it wrong).
  • it seems confused by MimeType when not designated Type=Application but the MimeType is critical to making the service menu work with the target file types.
  • it cant seem to find some Actions even tho they are in the file and they are working
  • also it says the spec is 1.5 but if you follow the link the spec is only up to 1.1

~/.local/share/kio/servicemenus/ might be a permission issue maybe?

there seems to be some guides that uses things that has been deprecated. not sure what is wrong for now i have just set nvim as the default program for all text files.

apparently Type=Service is no longer supported, everything is Type=Application now…. tho Service still works on my old plasma 5 setup.

and all your ~/.local folders should be owned by your user… if they are not then that could be a problem.

you never said what happens when you type vim [filename] into a konsole terminal

if you can get vim to work in your service menu using

Exec=konsole --hold -e vim then your issue is the nvim, not the service menu.