OYB software, shell

List files without stopping to type in zsh

This zsh widget allows you to list files in the current directory without interrupting the command you are typing.

Usage
  1. Hit <tab>  in the middle of any command. It will list all files below the command line.
  2. Hit <space><tab>  on an empty command line. It will list directories.
  3. Hit <space><space><tab>  on an empty command line. It will list executables.

Regular completion is unaffected.

Installation

Get the code from github, and append it to your .bashrc or .zshrc.

To do it in one step, paste the following in your zsh terminal

wget https://raw.githubusercontent.com/nachoparker/tab_list_files_zsh_widget/master/tab_list_files_zsh.sh -O - >> ~/.zshrc

It will be available next time you open a zsh session.

Configuration

You can configure cases 2 and 3 above to include a cd  and ./  as a prefix for a nice and handy shortcut. Include in your .zshrc

export TAB_LIST_FILES_PREFIX

Extras

I recommend complementing this with the push-line-or-edit  zsh builtin. Include the following in your .zshrc

bindkey '^q' push-line-or-edit

After this, you can cancel any ongoing command that you might be typing by hitting <CTRL>q , perform another command and then come back just to where you left off.

Code
# List files in zsh with <TAB>
#
# Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
# GPL licensed (see end of file) * Use at your own risk!
#
# Usage:
#   In the middle of the command line:
#     (command being typed)<TAB>(resume typing)
#
#   At the beginning of the command line:
#     <SPACE><TAB>
#     <SPACE><SPACE><TAB>
#
# Notes:
#   This does not affect other completions
#   For 'cd ' or './' to be prepended, add to .zshrc 'export TAB_LIST_FILES_PREFIX'
#   Complement this with push-line-or edit (bindkey '^q' push-line-or-edit)
function tab_list_files
{
  if [[ $#BUFFER == 0 ]]; then
    BUFFER="ls "
    CURSOR=3
    zle list-choices
    zle backward-kill-word
  elif [[ $BUFFER =~ ^[[:space:]][[:space:]].*$ ]]; then
    BUFFER="./"
    CURSOR=2
    zle list-choices
    [ -z ${TAB_LIST_FILES_PREFIX+x} ] && BUFFER="  " CURSOR=2
  elif [[ $BUFFER =~ ^[[:space:]]*$ ]]; then
    BUFFER="cd "
    CURSOR=3
    zle list-choices
    [ -z ${TAB_LIST_FILES_PREFIX+x} ] && BUFFER=" " CURSOR=1
  else
    BUFFER_=$BUFFER
    CURSOR_=$CURSOR
    zle expand-or-complete || zle expand-or-complete || {
      BUFFER="ls "
      CURSOR=3
      zle list-choices
      BUFFER=$BUFFER_
      CURSOR=$CURSOR_
    }
  fi
}
zle -N tab_list_files
bindkey '^I' tab_list_files

# uncomment the following line to prefix 'cd ' and './' 
# when listing dirs and executables respectively
#export TAB_LIST_FILES_PREFIX

# these two lines are usually included by oh-my-zsh, but just in case
autoload -Uz compinit
compinit

# uncomment the following line to complement tab_list_files with ^q
#bindkey '^q' push-line-or-edit

# License
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA

References

http://zsh.sourceforge.net/Guide/zshguide04.html

http://sgeb.io/posts/2016/11/til-bash-zsh-half-typed-commands

Author: nachoparker

Humbly sharing things that I find useful [ github dockerhub ]

2 Comments on “List files without stopping to type in zsh

    1. Unfortunately bash is way less flexible. You can do the following with by default emacs-based shortcuts

      ^A^K (whatever) ^Y

      ^A – place cursor at the beginning of the line
      ^K – delete the whole line from the cursor
      ^Y – “yank”, or paste last thing you deleted

      You can save the ^A bit if you include to your .inputrc
      “\C-k”: kill-whole-line

      Then, ^K would delete the whole line regardless of the position of the cursor

      … or you can just switch to zsh 🙂

Leave a Reply to nachoparker Cancel reply

Your email address will not be published. Required fields are marked *