Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I use xdg-open a lot in the terminal; this opens a file in the default application. However, sometimes I'd like to open a file in a different (non-default) application. Is there a way to achieve this from the terminal?

(Obviously, some applications can take a file as an argument, e.g. vlc /path/to/movie.mp3, but I'm looking for a more generic way that works in all situations, similar to xdg-open.)

An advantage of an xdg-open-like command is that the terminal window can be closed after opening the file/application. However, if I use (e.g.) vlc /path/to/movie.mp3 &, then I'll need to keep the terminal window open.

share|improve this question
2  
Are you aware of any applications which DO NOT take a file as a command-line argument? I'm pretty sure all xgd-open does is finds an application matching the file type and then invoking it as (app_name) (file_name) –  Sergey Jan 18 '13 at 23:58
    
Good point. The main secondary program that I use quite often is pdf-xchange via wine. It's a long command to write, but maybe I just need to write an alias. The other point is, AFAIK if I open a file using xdg-open, I can then close the terminal. However, if I use (e.g.) vlc /path/to/movie.mp3 &, then I'll need to keep the terminal window open. –  Sparhawk Jan 19 '13 at 0:07

2 Answers 2

up vote 1 down vote accepted

xdg-open is, of course, designed to use the default applications. To use non-default applicaitons, I think you have three options.

1) Type programname filename as usual.

2) Do this outside of the terminal by a right-click in Nautilus and using open with.

3) Use your own script in the terminal. Let's assume you have a reasonably short list of default programs you might want to choose from.

#!/bin/bash
read filename
myvar=$(zenity --list --text="Chose a non-default program" --column="Programs" firefox gedit)
$myvar filename &

This would let you specify a file name, then choose the program to open it with. This is the minimal implementation, so it might need some additional work. But try it out.

There may not be a lot of benefits to this approach, but it gives you control over the process and options to suit whatever needs you have.

share|improve this answer
    
As per my comment above, a major advantage of xdg-open is that the terminal window can be closed after opening the file/application. I'll edit the question to make this clearer. Also, this question is part of my attempt to use the terminal as a file manager, and I don't even have nautilus installed! –  Sparhawk Jan 19 '13 at 1:23
    
I added & to the final command so you can close the terminal after launching the program :). If you are essentially making your own file manager, I think you would have to work with your own scripts. That said, there must be some file managers that work in the terminal if you look around. They may already have the functions you need. Plus, I think emacs and its add-ons might serve you well. It's almmost like its own desktop in the terminal at this point (or so I hear; I'm still not very familiar with it.) –  chaskes Jan 19 '13 at 1:36
    
I just came to the same conclusion as your edit: that putting the process in the background from within a script allows you to close the terminal window. Hence (3) is correct. I'm not sure why this is not considered a background job similar to typing program & directly into the terminal? Any ideas? –  Sparhawk Jan 19 '13 at 1:37
    
I did have a look at using ranger as a file manager, but I don't really like the idea of exclusively using a specific program. That is, I'd much rather use coreutils, and supplement them with "add-on" scripts. –  Sparhawk Jan 19 '13 at 1:38
    
I think is this case running it in the background means the same thing as detaching it from the window it started in so they are really just two ways of saying the same thing. Once the window is detached, you can close it or reuse it. –  chaskes Jan 19 '13 at 1:42

There are pseudo-solutions like "Gnome's Web browser" (gnome-www-browser) or "Gnome's text editor" (gnome-text-editor). Same thing for the X server with its browser (x-www-browser) and its terminal emulator (x-terminal-emulator). There is also "editor" for the terminal text editor (nano, vi or whatever else). You can configure them with the update-alternatives line command.

But there were not real solution. A generic program for opening applications like xdg-open is made for opening files with default applications, not the only one application the user want to use at the very moment where he want to open the file. You cannot do this except changing the default application before opening the file but it is not user friendly and it does not woth getting default applications.

So the best solution for you is forgetting generic solutions and opening the file just like the specific application you want to use does.

share|improve this answer
    
As per my comment above, a major advantage of xdg-open is that the terminal window can be closed after opening the file/application. I'll edit the question to make this clearer. –  Sparhawk Jan 19 '13 at 1:23
    
In this case launch your command with the window which appears when you type Alt + F2 ("Run" Dialog). No terminal window will be opened and your command will be launched. –  air-dex Jan 19 '13 at 1:28
    
I use the run dialogue sometimes, but I'd still prefer a command from the command line proper, so I can still cd, ls, grep, find, etc. –  Sparhawk Jan 19 '13 at 1:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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