You are not logged in.
Pages: 1
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
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
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
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
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
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
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
}
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
@portix: thanks, I completely missed 'complete-filename' in the bash readline manuals, it seems
@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
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
@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
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
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
Pages: 1