问题
I am currently switching from csh to zsh I am writing a .zshrc trying to get all the options I am used to in this new shell.
I use autocd (to go into a directory just typing its name (without the cd command), and I wonder if it is possible that my first propose all the files existing in the current directory (like it's working in csh).
I am quite used to this way of having an overview of the files I can open or directory I can "autocd" into, before typing my command just pressing without anything written in my commandline yet.
Right now when I first press it does not trigger any completion mechanism but it just write an actual tabulation.
I did not find any solution yet, and if anyone has any magic options to get this results, feel free to enlighten me!
Thanks
回答1:
I found a way!
No need for autocd, though this option exists in zsh
To put in your ~/.zshrc
:
first-tab() {
if [[ $#BUFFER == 0 ]]; then
BUFFER="cd "
CURSOR=3
zle list-choices
else
zle expand-or-complete
fi
}
zle -N first-tab
bindkey '^I' first-tab
Thanks to this question: zsh tab completion on empty line
So press tab once and you will get "cd " and the existing directories.
Checkout man zshoptions
for other existing options which could be useful
(setopt menucomplete
could be useful to save a tab, but change behaviour for other completion as well.)
回答2:
Here is another more complex variation.
- It will list files on on an empty command line, and in the middle of any command.
- It will list directories on on an empty command line.
- It will list executables on on an empty command line.
It can be configured to prepend "cd " or "./" in those cases with a global variable.
export TAB_LIST_FILES_PREFIX
# 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
# If you want 'cd ' or './' to be prepended, write in your .zshrc 'export TAB_LIST_FILES_PREFIX'
# I recommend to 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
More details in this post
来源:https://stackoverflow.com/questions/28729851/zsh-first-tab-completion-with-autocd