* Suffix alias for README files
@ 2011-04-19 13:59 Anthony R Fletcher
2011-04-20 9:23 ` Sebastian Stark
0 siblings, 1 reply; 9+ messages in thread
From: Anthony R Fletcher @ 2011-04-19 13:59 UTC (permalink / raw)
To: zsh-users
I can use suffix aliases to display various .txt files. So
alias -s txt=less
./x.txt
actually runs
less ./x.txt
Can I do a similar thing for README files? So the "command"
/some/path/README
will really run the command
less /some/path/README
Anthony
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Suffix alias for README files
2011-04-19 13:59 Suffix alias for README files Anthony R Fletcher
@ 2011-04-20 9:23 ` Sebastian Stark
2011-04-20 12:41 ` Anthony R Fletcher
0 siblings, 1 reply; 9+ messages in thread
From: Sebastian Stark @ 2011-04-20 9:23 UTC (permalink / raw)
To: Anthony R Fletcher; +Cc: zsh-users
Am 19.04.2011 um 15:59 schrieb Anthony R Fletcher:
> I can use suffix aliases to display various .txt files. So
>
> alias -s txt=less
> ./x.txt
>
> actually runs
>
> less ./x.txt
>
> Can I do a similar thing for README files? So the "command"
>
> /some/path/README
>
> will really run the command
>
> less /some/path/README
It doesn't really answer your question, but are you aware of nullcmd redirection?
<x.txt
or
</some/path/README
will call whatever pager or viewer is set in $READNULLCMD. In the first case it's even less characters to type :)
Sebastian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Suffix alias for README files
2011-04-20 9:23 ` Sebastian Stark
@ 2011-04-20 12:41 ` Anthony R Fletcher
2011-04-20 15:06 ` Bart Schaefer
0 siblings, 1 reply; 9+ messages in thread
From: Anthony R Fletcher @ 2011-04-20 12:41 UTC (permalink / raw)
To: zsh-users
On 20 Apr 2011 at 11:23:07, Sebastian Stark wrote:
>
> Am 19.04.2011 um 15:59 schrieb Anthony R Fletcher:
>
> > I can use suffix aliases to display various .txt files. So
> >
> > alias -s txt=less
> > ./x.txt
> >
> > actually runs
> >
> > less ./x.txt
> >
> > Can I do a similar thing for README files? So the "command"
> >
> > /some/path/README
> >
> > will really run the command
> >
> > less /some/path/README
>
> It doesn't really answer your question, but are you aware of nullcmd redirection?
>
> <x.txt
>
> or
>
> </some/path/README
>
> will call whatever pager or viewer is set in $READNULLCMD. In the first case it's even less characters to type :)
>
>
> Sebastian
Thanks for the reply. Yes, I am aware of the nullcmd redirection and it
doesn't quite fit with the way I wanted to be lazy.
The use case is to have run something like
find . -name README
identify an interesting README file, triple click it with the mouse to
copy it (with trailing newline) and paste it into the current shell.
So I have to type "less " or "<" before I paste. The reality is I forget
and then use the history to repeat the now pasted line and add the
leading command.
So I wondered if you could do "the right thing" with files like x.txt
was there a way for README files?
Anthony.
--
Anthony R Fletcher
Room 2033, Building 12A, http://dcb.cit.nih.gov/~arif
National Institutes of Health, arif@mail.nih.gov
12A South Drive, Bethesda, Phone: (+1) 301 402 1741.
MD 20892-5624, USA.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Suffix alias for README files
2011-04-20 12:41 ` Anthony R Fletcher
@ 2011-04-20 15:06 ` Bart Schaefer
2011-04-20 16:44 ` Anthony R Fletcher
2011-04-20 17:25 ` John Eikenberry
0 siblings, 2 replies; 9+ messages in thread
From: Bart Schaefer @ 2011-04-20 15:06 UTC (permalink / raw)
To: zsh-users
> I can use suffix aliases to display various .txt files.
> Can I do a similar thing for README files? So the "command"
>
> /some/path/README
>
> will really run the command
>
> less /some/path/README
Depending on your version of zsh, you can do this either with the
zle-line-finish widget or by replacing the accept-line widget.
zle-line-finish() {
setopt localoptions extendedglob
if [[ -z "$PREBUFFER" && "$BUFFER" = ([^[:space:]]#/)#README ]]
then BUFFER="less $BUFFER"
fi
}
zle -N zle-line-finish
Or
accept-line() {
setopt localoptions extendedglob
if [[ -z "$PREBUFFER" && "$BUFFER" = ([^[:space:]]#/)#README ]]
then BUFFER="less $BUFFER"
fi
zle .accept-line "$@"
}
zle -N accept-line
Aside to zsh-workers:
If no external command is found but a function command_not_found_handler
exists the shell executes this function with all command line arguments.
Perhaps "command found but permission denied" should be treated the same
as "command not found"? Then one could handle this there as well, and
not have to mess with widgets. See also recent discussion of bash/zsh
differences when the command is literally an empty string.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Suffix alias for README files
2011-04-20 15:06 ` Bart Schaefer
@ 2011-04-20 16:44 ` Anthony R Fletcher
2011-04-20 17:25 ` John Eikenberry
1 sibling, 0 replies; 9+ messages in thread
From: Anthony R Fletcher @ 2011-04-20 16:44 UTC (permalink / raw)
To: zsh-users
On 20 Apr 2011 at 08:06:25, Bart Schaefer wrote:
> > I can use suffix aliases to display various .txt files.
> > Can I do a similar thing for README files? So the "command"
> >
> > /some/path/README
> >
> > will really run the command
> >
> > less /some/path/README
>
> Depending on your version of zsh, you can do this either with the
> zle-line-finish widget or by replacing the accept-line widget.
>
> zle-line-finish() {
> setopt localoptions extendedglob
> if [[ -z "$PREBUFFER" && "$BUFFER" = ([^[:space:]]#/)#README ]]
> then BUFFER="less $BUFFER"
> fi
> }
> zle -N zle-line-finish
>
> Or
>
> accept-line() {
> setopt localoptions extendedglob
> if [[ -z "$PREBUFFER" && "$BUFFER" = ([^[:space:]]#/)#README ]]
> then BUFFER="less $BUFFER"
> fi
> zle .accept-line "$@"
> }
> zle -N accept-line
Great, the last works for me everywhere and the first works on my newer
machines. Thanks.
> Aside to zsh-workers:
>
> If no external command is found but a function command_not_found_handler
> exists the shell executes this function with all command line arguments.
>
> Perhaps "command found but permission denied" should be treated the same
> as "command not found"? Then one could handle this there as well, and
> not have to mess with widgets. See also recent discussion of bash/zsh
> differences when the command is literally an empty string.
Thanks for mentioning "command_not_found_handler". I'd missed that one.
Anthony.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Suffix alias for README files
2011-04-20 15:06 ` Bart Schaefer
2011-04-20 16:44 ` Anthony R Fletcher
@ 2011-04-20 17:25 ` John Eikenberry
2011-04-20 18:40 ` ZyX
1 sibling, 1 reply; 9+ messages in thread
From: John Eikenberry @ 2011-04-20 17:25 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: text/plain, Size: 1355 bytes --]
Bart Schaefer wrote:
> > I can use suffix aliases to display various .txt files.
> > Can I do a similar thing for README files? So the "command"
> >
> > /some/path/README
> >
> > will really run the command
> >
> > less /some/path/README
>
> Depending on your version of zsh, you can do this either with the
> zle-line-finish widget or by replacing the accept-line widget.
>
> zle-line-finish() {
> setopt localoptions extendedglob
> if [[ -z "$PREBUFFER" && "$BUFFER" = ([^[:space:]]#/)#README ]]
> then BUFFER="less $BUFFER"
> fi
> }
> zle -N zle-line-finish
>
> Or
>
> accept-line() {
> setopt localoptions extendedglob
> if [[ -z "$PREBUFFER" && "$BUFFER" = ([^[:space:]]#/)#README ]]
> then BUFFER="less $BUFFER"
> fi
> zle .accept-line "$@"
> }
> zle -N accept-line
Is there any way to get either of these to work without changing the command
that is stored in history? So the history contains '/some/path/README' and not
'less /some/path/README'?
--
John Eikenberry
[ jae@zhar.net - http://zhar.net ]
[ PGP public key @ http://zhar.net/jae_at_zhar_net.gpg ]
________________________________________________________________________
"Perfection is attained, not when no more can be added, but when no more
can be removed." -- Antoine de Saint-Exupery
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 490 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Suffix alias for README files
2011-04-20 17:25 ` John Eikenberry
@ 2011-04-20 18:40 ` ZyX
2011-04-20 18:57 ` John Eikenberry
0 siblings, 1 reply; 9+ messages in thread
From: ZyX @ 2011-04-20 18:40 UTC (permalink / raw)
To: John Eikenberry, zsh-users
[-- Attachment #1: Type: Text/Plain, Size: 2627 bytes --]
Reply to message «Re: Suffix alias for README files»,
sent 21:25:43 20 April 2011, Wednesday
by John Eikenberry:
> Is there any way to get either of these to work without changing the
> command that is stored in history? So the history contains
> '/some/path/README' and not 'less /some/path/README'?
Yes, of course. I personally use the following code:
function zshaddhistory()
{
emulate -L zsh
if ! [[ -z "${_HISTLINE}" ]] ; then
print -sr -- "${_HISTLINE}"
unset _HISTLINE
else
print -sr -- "${1%%$'\n'}"
fi
fc -p
}
With this function if you set variable _HISTLINE somewhere before zshaddhistory
hook is run then contents of _HISTLINE will be put into the history. Widget
accept-line is called before zshaddhistory.
Here is where I actually use _HISTLINE (with just the same purpose: execute one
command, but add to history another):
function _-accept-line()
{
emulate -L zsh
local -r autopushd=${options[autopushd]}
options[autopushd]=off
cd $PWD || cd
options[autopushd]=$autopushd
if [[ ${BUFFER[1,3]} == ":h " ]] ; then
_HISTLINE=$BUFFER
BUFFER=":h ${(q)BUFFER[4,-1]}"
elif [[ ${BUFFER[1,4]} == "zmw " ]] ; then
_HISTLINE=$BUFFER
BUFFER="zmw "${(j. .)${(q)${(z)BUFFER[5,-1]}}}
fi
zle .accept-line
}
Original message:
> Bart Schaefer wrote:
> > > I can use suffix aliases to display various .txt files.
> > > Can I do a similar thing for README files? So the "command"
> > >
> > > /some/path/README
> > >
> > > will really run the command
> > >
> > > less /some/path/README
> >
> > Depending on your version of zsh, you can do this either with the
> > zle-line-finish widget or by replacing the accept-line widget.
> >
> > zle-line-finish() {
> >
> > setopt localoptions extendedglob
> > if [[ -z "$PREBUFFER" && "$BUFFER" = ([^[:space:]]#/)#README ]]
> > then BUFFER="less $BUFFER"
> > fi
> >
> > }
> > zle -N zle-line-finish
> >
> > Or
> >
> > accept-line() {
> >
> > setopt localoptions extendedglob
> > if [[ -z "$PREBUFFER" && "$BUFFER" = ([^[:space:]]#/)#README ]]
> > then BUFFER="less $BUFFER"
> > fi
> > zle .accept-line "$@"
> >
> > }
> > zle -N accept-line
>
> Is there any way to get either of these to work without changing the
> command that is stored in history? So the history contains
> '/some/path/README' and not 'less /some/path/README'?
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Suffix alias for README files
2011-04-20 18:40 ` ZyX
@ 2011-04-20 18:57 ` John Eikenberry
2011-04-20 19:22 ` ZyX
0 siblings, 1 reply; 9+ messages in thread
From: John Eikenberry @ 2011-04-20 18:57 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: text/plain, Size: 1497 bytes --]
ZyX wrote:
> Reply to message «Re: Suffix alias for README files»,
> sent 21:25:43 20 April 2011, Wednesday
> by John Eikenberry:
>
> > Is there any way to get either of these to work without changing the
> > command that is stored in history? So the history contains
> > '/some/path/README' and not 'less /some/path/README'?
> Yes, of course. I personally use the following code:
> function zshaddhistory()
> {
> emulate -L zsh
> if ! [[ -z "${_HISTLINE}" ]] ; then
> print -sr -- "${_HISTLINE}"
> unset _HISTLINE
> else
> print -sr -- "${1%%$'\n'}"
> fi
> fc -p
> }
> With this function if you set variable _HISTLINE somewhere before
> zshaddhistory hook is run then contents of _HISTLINE will be put into the
> history. Widget accept-line is called before zshaddhistory.
I'll play with this after work but I have a quick question. Do you know whether
this would play nice with the hist_ignore_all_dups setting? My experiments with
using print to manually add entries previously have not worked as they all
failed to respect hist_ignore_all_dups.
Thanks.
--
John Eikenberry
[ jae@zhar.net - http://zhar.net ]
[ PGP public key @ http://zhar.net/jae_at_zhar_net.gpg ]
________________________________________________________________________
"Perfection is attained, not when no more can be added, but when no more
can be removed." -- Antoine de Saint-Exupery
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 490 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Suffix alias for README files
2011-04-20 18:57 ` John Eikenberry
@ 2011-04-20 19:22 ` ZyX
0 siblings, 0 replies; 9+ messages in thread
From: ZyX @ 2011-04-20 19:22 UTC (permalink / raw)
To: John Eikenberry, zsh-users
[-- Attachment #1: Type: Text/Plain, Size: 1995 bytes --]
Reply to message «Re: Suffix alias for README files»,
sent 22:57:07 20 April 2011, Wednesday
by John Eikenberry:
> I'll play with this after work but I have a quick question. Do you know
> whether this would play nice with the hist_ignore_all_dups setting? My
> experiments with using print to manually add entries previously have not
> worked as they all failed to respect hist_ignore_all_dups.
I have histignorealldups set and everything works normally. But I remember I had
some bad issues when I tried to implement it without using zshaddhistory hook:
as far as I remember, `fc -p' call results were significantly different.
Original message:
> ZyX wrote:
> > Reply to message «Re: Suffix alias for README files»,
> > sent 21:25:43 20 April 2011, Wednesday
> >
> > by John Eikenberry:
> > > Is there any way to get either of these to work without changing the
> > > command that is stored in history? So the history contains
> > > '/some/path/README' and not 'less /some/path/README'?
> >
> > Yes, of course. I personally use the following code:
> > function zshaddhistory()
> > {
> >
> > emulate -L zsh
> > if ! [[ -z "${_HISTLINE}" ]] ; then
> >
> > print -sr -- "${_HISTLINE}"
> > unset _HISTLINE
> >
> > else
> >
> > print -sr -- "${1%%$'\n'}"
> >
> > fi
> > fc -p
> >
> > }
> >
> > With this function if you set variable _HISTLINE somewhere before
> > zshaddhistory hook is run then contents of _HISTLINE will be put into the
> > history. Widget accept-line is called before zshaddhistory.
>
> I'll play with this after work but I have a quick question. Do you know
> whether this would play nice with the hist_ignore_all_dups setting? My
> experiments with using print to manually add entries previously have not
> worked as they all failed to respect hist_ignore_all_dups.
>
> Thanks.
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2011-04-20 19:22 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-19 13:59 Suffix alias for README files Anthony R Fletcher
2011-04-20 9:23 ` Sebastian Stark
2011-04-20 12:41 ` Anthony R Fletcher
2011-04-20 15:06 ` Bart Schaefer
2011-04-20 16:44 ` Anthony R Fletcher
2011-04-20 17:25 ` John Eikenberry
2011-04-20 18:40 ` ZyX
2011-04-20 18:57 ` John Eikenberry
2011-04-20 19:22 ` ZyX
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).