2

In the fish shell, when you press Alt+l it displays the output of the ls command nicely. It keeps the command you were writing before you pressed it. I would like to have this in zsh.

This was my very wonky attempt:

function myls {
    awk '{len = length($0); for(i=0;i<len;i++) printf "\b"}' <<< "$LBUFFER"
    zle push-input
    zle accept-line
    print $(ls --color=always --indicator-style=slash)
}
zle -N myls
bindkey -- '^[l' myls

I struggled to get the existing command to clear, and ended up using hacky \b's. It does not work correctly on long and multi lines.

Can anyone do any better?

2 Answers 2

5

Use zle -R after the push-input to redisplay the line without the buffer.

For the most part, I find zsh's completion listing obviates the need for a widget to run ls. It does ls style colors and file-type suffixes. I have the following binding which does file completion in any context and does a long, ls -l style listing:

zstyle ":completion:file-complete::::" completer _files
zle -C file-complete complete-word _generic
zstyle ':completion:file-complete:*' file-list true
bindkey '^X^F' file-complete
1

Thanks to input from @okapi I came up with this.

function myls {
    [[ "$CONTEXT" = cont ]] && return
    zle push-input
    zle -R
    zle accept-line
    print -n $(ls --color=always --indicator-style=slash)
}

zle -N myls
bindkey -- '^[l' myls

It's much better, but it wasn't working with the continuation like in loops 'for> ...' and 'while> ...' so I have it just return in them for now.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .