zsh-users
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: Alan <8fvebtoeq87@gmail.com>
Cc: zsh-users@zsh.org
Subject: Re: Don't append slash when auto completing a symbolic link pointing to a directory
Date: Fri, 29 May 2020 22:54:21 +0000	[thread overview]
Message-ID: <20200529225421.4535d31a@tarpaulin.shahaf.local2> (raw)
In-Reply-To: <CAL7A0-Mr+neHDxbH1b7ERbL1KQtskBxfDpCSOfPkqkviaNYe0w@mail.gmail.com>

Alan wrote on Thu, 28 May 2020 17:56 -0400:
> On Thu, May 28, 2020 at 4:53 PM Mikael Magnusson <mikachu@gmail.com> wrote:
> > On 5/28/20, Alan <8fvebtoeq87@gmail.com> wrote:  
> > > $ ls sym<TAB>
> > >
> > > At this point, in bash, you would get:
> > > $ ls symblink
> > >
> > > In zsh, I'm getting:
> > > $ ls symblink/
> > >
> > > Also, in bash, hitting a <TAB> when the full symbolic link is already
> > > present would then append the slash to the symbolic link:
> > > $ ls symblink<TAB>
> > > $ ls symblink/
> > >
> > > Is there anyway to get this behavior with zsh? I looked through "man
> > > zshoptions" this time and couldn't find anything specific to this.  
> >
> > If it makes you feel better, hitting enter will remove the / and just
> > run "ls symblink". (Although this makes no difference to ls unless you
> > also give -l). The / is just inserted temporarily in case you want to
> > continue typing/completing things inside the directory, but a space,
> > enter or movement etc will remove it again. This should be indicated
> > by a bold/standout font.
> >  
> 
> Unfortunately, I have autoremoveslash disabled (i.e. 'unsetopt
> autoremoveslash'), and prefer it that way for other things, so the trailing
> slash isn't removed when pressing enter.

It's possible to achieve this with a custom widget:

[[[
expand-or-complete()
{
	local old=$LBUFFER
	{
		zle .$WIDGET -- "$@"
	} always {
		[[ ${LBUFFER} == */ && $LBUFFER != ${old}/ && -L ${${(z)LBUFFER}[-1]%/} ]] && LBUFFER=${LBUFFER%/}
	}
}
zle -N expand-or-complete
]]]

I realize this isn't exactly an obvious solution.  I suspect there's
a simpler solution that I'm overlooking.

In English, what it does is wrap the «expand-or-complete» widget (which
handles <TAB> presses — see `bindkey $'\t'`) with a function that, after
the built-in widget does its thing, removes the slash if the word before
the cursor is a symlink with slash appended.  The second conjunct is to
let «symlink<TAB>» complete to «symlink/».

You might want to add a «$old != $LBUFFER» and/or «$? -eq 0» to the list
of conditions so it doesn't remove the slash when trying to complete
filenames in an empty directory.

s/(z)/(zZ+c+)/ if you set the INTERACTIVE_COMMENTS option.

Cheers,

Daniel

      reply	other threads:[~2020-05-29 22:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-28 20:04 Alan
2020-05-28 20:53 ` Mikael Magnusson
2020-05-28 21:56   ` Alan
2020-05-29 22:54     ` Daniel Shahaf [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200529225421.4535d31a@tarpaulin.shahaf.local2 \
    --to=d.s@daniel.shahaf.name \
    --cc=8fvebtoeq87@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).