Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know that xdg-open will open from terminal a file in the user's preferred application like this:

xdg-open filename

But I wonder how can I open a file from the current directory in its default application only by typing:


followed by Enter, of course. Nothing more.

share|improve this question
Presumably you could modify the command_not_found_handle() function in /etc/bash.bashrc to check for and xdg-open a file in the same directory? – chronitis Oct 24 '13 at 8:52
I can reduce some of your risk by using alias. alias open ="xdg-open" open is a variable simply. you can make it as short as you want. – Raja Oct 24 '13 at 8:59
up vote 19 down vote accepted

Use Ubuntu's command-not-found hook, as specified in Command Not Found Magic. It is currently used to suggest packages to install. Refer to /usr/share/doc/command-not-found/README which should be installed on your system.

Better yet, because it does not depend on the command-not-found package, (re)implement the Bash builtin command_not_found_handle to do an xdg-open if $1 is an existing file, and to delegate all other cases to the previous implementation.

# Save the existing code for the handler as prev_command_not_found_handle.
# Bit of a hack, as we need to work around bash's lack of lexical closure,
# and cover the case when it is not defined at all.
eval "prev_$(declare -f command_not_found_handle)" >& /dev/null \
     || prev_command_not_found_handle () { 
            echo "$1: command not found" 1>&2
            return 127

# Define the new implementation, delegating to prev_handler.
command_not_found_handle () {
    if [ -f "$1" ]; then
        xdg-open "$1"
        prev_command_not_found_handle "$@"

Good question, nifty feature.

Thinking it over some more: you might not like the feature as much as you think, unless you also extend the bash_completion handler. Imagine wanting to open file-with-a-long-name.txt, then setting

alias o='xdg-open'  

will make (about) four key presses suffice:

o f<Tab><Enter>

Whereas typing the full file name takes a tedious 26 - and that excludes backspacing over the inevitable typos.

share|improve this answer

Your Answer


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.