5

I would like to open a PDF with evince from the command line and after the Evince window pops up, I want to go back to the terminal command with $ already waiting for the next command.

Now if I run this, the PDF opens in a window, and when I go back to the terminal, I need to press enter to be able to get $ waiting for the next command.

$ evince foo.pdf &

Any idea how to prevent the need for 2nd hit on enter to be able to type the next command?

I don't care whether the opened window will close after closing the command line (i.e., nohup is not needed).

I use a bash version 5.1.16.

1

3 Answers 3

11

You don't actually need to press enter, you can use the terminal directly. What happens is that evince can print out various messages to standard error, so those make your temrinal look like it isn't ready. However, you can just ignore them and type a command and you'll see it works.

Regardless, I realize this isn't very practical, so what you can do is redirect standard error to /dev/null:

evince foo.pdf 2>/dev/null &

To avoid writing this every time, you can ake a little function. Add these lines to your ~/.bashrc (or equivalent, for other shells):

evince(){
   command evince "$@" 2> /dev/null
}

This uses command as a way of getting the real evince, ignoring the function name, and just passes whatever arguments you use directly to the function ("$@"), redirecting stderr to dev/null.

Save the file, open a new terminal, and you're all set to use evince the way you want.

3

If you are using gnome you can use:

xdg-open myfile

The advantage is that this works with any type of file or directory, and also you don't need the ambersant and the redirection.

4
  • 2
    This does not just work for Gnome, it works for all Free Desktop (XDG) compliant desktop environments: portland.freedesktop.org/doc/xdg-open.html Similarly, open on macOS and start on Windows do more or less the same thing. Oct 9, 2023 at 17:13
  • @JörgWMittag: Thanks, that's useful to know!
    – user000001
    Oct 9, 2023 at 17:32
  • @JörgWMittag and user000001 - does it always return immediately instead of waiting for the software that was opened to exit? unix.stackexchange.com/q/74605/70524 indicates it might wait for the software to exit
    – muru
    Oct 10, 2023 at 6:38
  • @muru: That's not my experience on Debian 12 (gnome). I don't think I have ever seen it block, just tested it with firefox and evince, and it returned immediately. Firefox did cause some output though on the terminal.
    – user000001
    Oct 10, 2023 at 7:39
2

Here is one possible workflow that I use often.

I use a terminal emulator that supports HTML-like explicit hyperlinks (OSC 8 escape sequence). There are quite a few such terminal emulators out there.

I have set up an alias from ls to ls --hyperlink=auto (and some other options as well that are irrelevant here). Note that it's ls from GNU coreutils, the default on most Linux systems, which has this command line option.

I issue the ls command to print the given filename. I could narrow down the listing using wildcards, or list the desired filename only (with the aid of tab completion). Or I can simply list the entire directory. Or if I know that it's one of the newest files in the directory then ls -ltr and it'll be at the end of the listing, I don't have to recall its name. Whichever feels the most convenient, or whichever I feel like typing at that very moment.

Then I Ctrl+click on the printed link (some terminal emulators might need a different action to open the link), which opens the file in its default handler application.

This way I don't need to remember the name of the default application (whether it's evince or whatever else), don't need to type that, nor type something generic like xdg-open or something overly long such as gapplication launch org.gnome.Evince shown in a comment above.

My PDFs and similar files often contain special characters (e.g. spaces) in their names, it's somewhat out of my control (I download many of them from the web or receive via other sources such as email, and I can't be bothered to rename them all to contain easy-to-use characters only). This method handles such characters safely, I don't need to fiddle with quoting or escaping.

The default application is launched externally to the terminal session, that is, the app's standard output or error won't appear and won't clutter my terminal session.

An obvious drawback is that it launches the default application for the given file type, I cannot pick one-off exceptions. (Theoretically there might be a terminal emulator out there which offers to pick a different application, e.g. in its right click menu. I don't know if there any.) If I need to open with a different application, I resort to more standard methods.

I personally love this method and find it super fast and convenient. Your mileage may vary. Give it a try and see if you like it or not.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .