空行上的zsh选项卡完成


12

我想要一种我找不到的tcsh'ism:在没有内容的空白行中,我想按Tab键并看到与ls等效的内容。就是说我要

$ <tab>

做其他事情然后给我一个\ t。我已经找到了用于完成命令的出色资源,但没有针对这种基本情况。任何帮助都会很棒!谢谢。

Answers:


8
# expand-or-complete-or-list-files
function expand-or-complete-or-list-files() {
    if [[ $#BUFFER == 0 ]]; then
        BUFFER="ls "
        CURSOR=3
        zle list-choices
        zle backward-kill-word
    else
        zle expand-or-complete
    fi
}
zle -N expand-or-complete-or-list-files
# bind to tab
bindkey '^I' expand-or-complete-or-list-files

井井有条。是否可以以某种方式再次隐藏列表?Tab-to-Show然后Tab-to-hide会很好。
帕克·考茨

感谢约翰,我发现您的解决方案并适于在这里stackoverflow.com/questions/28729851/...
lolesque

7

行首的行为Tab样式控制。但是,只有两种受支持的行为:insert-tab

  • 照常完成,根据 zstyle ':completion:*' insert-tab false
  • 在下方插入标签 zstyle ':completion:*' insert-tab true
  • 一个或另一个在 zstyle ':completion:*' insert-tab pending[=N]

如果您只想在该位置完成命令,zstyle ':completion:*' insert-tab true则可以。如果您需要其他功能,例如在当前目录中列出文件,则需要进行修改_main_complete

一个zsh的工人名单上最近的线程讨论insert-tab


太棒了!通过_main_complete,看来您正在引用C代码中的某个地方?我为这个愚蠢的问题感到抱歉,但是在哪里可以找到?

1
@ user535759:不,_main_complete是实现完成的zsh代码的一部分。它位于Completion/Base/Core/_main_complete源代码树中,通常安装在位置/usr/share/zsh/functions/Completion/Base/_main_complete
吉尔(Gilles)'所以

@llua更改与关联的样式-command-不会导致<Tab>列出当前目录中的文件。您要做的就是限制匹配项以省略命令名称。但是,仅列出将在此位置完成的内容,而不列出当前目录中的文件(仅取决于autocd和的目录和可执行文件PATH)。
吉尔斯(Gilles)'所以

3

当您在空白行上按Tab键时,这是zsh中tcsh自动列表的完整实现

% <TAB>

这里是:

# list dir with TAB, when there are only spaces/no text before cursor,
# or complete words, that are before cursor only (like in tcsh)
tcsh_autolist() { if [[ -z ${LBUFFER// } ]]
    then BUFFER="ls " CURSOR=3 zle list-choices
    else zle expand-or-complete-prefix; fi }
zle -N tcsh_autolist
bindkey '^I' tcsh_autolist

如果您想更紧密地模拟tcsh,请将其添加到您的.zshrc中:

unsetopt always_last_prompt       # print completion suggestions above prompt

2

我写了这个 zsh小部件,它不仅在空行中而且在键入命令时都增强了TAB的使用。

  • 它将在空命令行以及任何命令的中间列出文件
  • 它将在空命令行上列出目录
  • 它将在空命令行中列出可执行文件

在这种情况下,可以将其配置为在全局变量前加上“ cd”或“ ./”。

export TAB_LIST_FILES_PREFIX

tab_list_files_example

# 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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.