You are not logged in.

#1 2010-05-18 05:48:24

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

bash - open file with

Hello guys,

I'd like to type in just the filename in a bash shell and the file should then be opened with the program I'd defined previously.

Example

Instead of zathura name.pdf, the pdf should be opened with zathura when I only type in name.pdf.

Is that possible?

It's not directly an alias so I don't come up with a solution yet.

Regards

Offline

#2 2010-05-18 06:18:16

Ogion
Member
From: Germany
Registered: 2007-12-11
Posts: 367

Re: bash - open file with

alias -s PDF="zathura"
alias -s pdf="zathura"

zsh can do.

Also i think there is something in relation to xdg.

Ogion


(my-dotfiles)
"People willing to trade their freedom for temporary security deserve neither and will lose both." - Benjamin Franklin
"Enlightenment is man's leaving his self-caused immaturity." - Immanuel Kant

Offline

#3 2010-05-18 06:20:02

VoodooSteve
Member
From: Vancouver, BC
Registered: 2009-03-31
Posts: 43

Re: bash - open file with

Are you running GNOME? When I was using it you can use gnome-open to open files with the GNOME default. I put the following in my .bashrc as well:

function o() {
     gnome-open "$1" 2> /dev/null
}

so that errors would not pollute my shell.

If you're not using GNOME, there's something similar called xdg-open but I've never used it.

Offline

#4 2010-05-21 09:59:25

quigybo
Member
Registered: 2009-01-15
Posts: 223

Re: bash - open file with

xdg-open figures out what DE you are running and calls gnome-open, kde-open, exo-open etc as appropriate, thus I would recommend using it as a DE-independent way of opening files.

@VoodooSteve: I would use "$@" over "$1" so multiple files can be opened

function o() {
    xdg-open "$@" 2> /dev/null
}

also, if you want to see stderr (or dont care if you do) all you need is an alias

alias o='xdg-open'

Offline

#5 2010-05-21 11:23:22

portix
Member
Registered: 2009-01-13
Posts: 757

Re: bash - open file with

If you don't care about stderr being displayed you can also add

prompt_command() {
    if [ $? -eq 127 ]; then 
        local FILE="$(history 1 | awk '{print $2}')"
        [ -f "${FILE}" ] && xdg-open "${FILE}"
    fi
}
PROMPT_COMMAND=prompt_command

to your .bashrc.

Last edited by portix (2010-05-21 11:55:46)

Offline

#6 2010-05-21 13:51:02

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: bash - open file with

With bash4 you could also do

o () {
    xdg-open "$READLINE_LINE"
    history -s "$READLINE_LINE"
    READLINE_LINE=""
}    
bind -x '"\C-o": o' 

...

$ foo.pdf<C-o>

On a related note, does anyone know how to teach bash to complete a bare path down to a non-executable file?

Offline

#7 2010-05-22 00:08:00

portix
Member
Registered: 2009-01-13
Posts: 757

Re: bash - open file with

I also have a xdg-open independent solution, in .bashrc i have

. ~/.bash_suffix

add_filetype 'pdf|ps|djvu|dvi' evince
add_filetype 'jpg|JPG|jpeg|JPEG|png|PNG' gqview

prompt_command() {
    if [ $? -eq 127 ]; then 
        local -a FILES=( $(history 1) )
        local FILE=${FILES[1]}
        local COMMAND="$(get_program ${FILE})"
        if [ -f "${FILE}" ] && [ "${COMMAND}" ]; then 
            echo -en "\e[1A\e[2K"  # Erase error message 
            "${COMMAND}" "${FILE}" 
        fi
    fi 
}
PROMPT_COMMAND=prompt_command

and ~/.bash_suffix:

#!/bin/bash
declare -A MIMETYPES

add_filetype() {
    OLD_IFS="$IFS"
    IFS="|"
    local COMMAND="$2"
    set $1                      #EDIT
    for SUFFIX in $@; do 
        MIMETYPES["$SUFFIX"]="${COMMAND}"
    done
    IFS=$OLD_IFS
}
get_program() {
    local SUFFIX="${1##*.}"
    if [ "${SUFFIX}" ]; then
        local COMMAND="${MIMETYPES[$SUFFIX]}"
        [ "$COMMAND" ] && echo $COMMAND
    fi
}
hbekel wrote:

On a related note, does anyone know how to teach bash to complete a bare path down to a non-executable file?

In vi-mode "\" will complete filenames even if the filename is the first token or you can bind complete-filename to a key (default is M-/).

Last edited by portix (2010-05-22 16:15:46)

Offline

#8 2010-05-22 08:41:53

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: bash - open file with

Wow thanks for your detailed solutions. But they're all looking quite complicated. *g*

What exactly do I have to do to open a pdf for example with zathura?

Can anyone give me an example?

Offline

#9 2010-05-22 11:55:50

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: bash - open file with

@portix: thanks, I completely missed 'complete-filename' in the bash readline manuals, it seems wink

@orschiro: I'd suggest to use the method described in portix' last post. Just add the first piece of code to your ~/.bashrc, then create the file ~/.bash_suffix and insert the second piece of code there. Then, in the first "add_filetype" line of ~/.bashrc, replace "evince" with "zathura". That should do it.

Offline

#10 2010-05-22 12:22:47

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: bash - open file with

xdg-open figures out what DE you are running and calls gnome-open, kde-open, exo-open etc as appropriate, thus I would recommend using it as a DE-independent way of opening files.

Those of us who prefer to not use a DE despise xdg-open though, because it tries to open everything in $BROWSER.

Offline

#11 2010-05-22 16:14:45

portix
Member
Registered: 2009-01-13
Posts: 757

Re: bash - open file with

@orschiro
If you use my solution, see hbekels post, but there is a bug in the code I posted  yesterday. In function add_filetype() it must be

...
    set $1
    for SUFFIX in $@; do 
...

instead of

...
    for SUFFIX in $(set $1); do
...

To use zathura just  put

add_filetype pdf zathura

to your .bashrc

Last edited by portix (2010-05-22 16:19:42)

Offline

#12 2010-05-22 18:52:57

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: bash - open file with

Wow this is working very fine. smile

The only thing that needs to be adjusted is tab completion. When I only type the filename I don't have completion enabled.

Can this be added?

Offline

#13 2010-05-22 19:46:48

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: bash - open file with

Tab completion is the problem here... when bash sees a bare path, it only completes down to executable files. Afaik it's not possible to complete to non-executable files using just tab...

What you can do is bind a key combo to readline's complete-filename function, e.g. add this to your .bashrc:

bind '"\e[Z": complete-filename'

to complete filenames with shift-tab. I'm trying to get used to that at the moment. I can complete to a directory with tab, and then use shift-tab to get at the files.

See man bash (section "Readline Notation") for details of binding keys and specifying key combos.

Offline

#14 2010-05-22 19:51:05

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: bash - open file with

Hello hbekel,

thank you for that snippet, I can work with it so far. smile

Best regards

Last edited by orschiro (2010-05-22 19:51:19)

Offline

#15 2010-06-16 08:09:41

tzervo
Member
From: Athens
Registered: 2009-04-03
Posts: 86

Re: bash - open file with

You can also check mimeman/mimeo or pcmanfm (filemanager which many also use for mime opening) and alias it to "o" as some others mentioned above.

Offline

Board footer

Powered by FluxBB