【问题标题】:zsh - first tab completion with autocdzsh - 使用 autocd 完成第一个制表符
【发布时间】:2015-04-28 02:04:08
【问题描述】:

我目前正在从 csh 切换到 zsh 我正在编写一个 .zshrc 试图在这个新的 shell 中获得我习惯的所有选项。

我使用 autocd(进入一个目录,只需输入它的名称(没有 cd 命令),我想知道我是否有可能首先提出当前目录中存在的所有文件(就像它在 csh 中工作一样)。

我已经习惯了这种方式,在输入命令之前,我可以打开我可以打开的文件或我可以“autocd”进入的目录的概览,然后在我的命令行中写入任何内容。

现在当我第一次按下它时,它不会触发任何完成机制,它只是写一个实际的表格。

我还没有找到任何解决方案,如果有人有任何神奇的选择来获得这个结果,请随时赐教!

谢谢

【问题讨论】:

    标签: zsh tab-completion zsh-completion completion


    【解决方案1】:

    我找到了方法!
    不需要 autocd,虽然这个选项存在于 zsh
    输入您的~/.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
    

    感谢这个问题:zsh tab completion on empty line

    所以按一下tab,你会得到“cd”和现有的目录。

    结帐man zshoptions 了解其他可能有用的现有选项
    setopt menucomplete 可以用于保存选项卡,但也可以更改其他完成的行为。)

    【讨论】:

      【解决方案2】:

      这是另一个更复杂的变体。

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

      在这些情况下,可以将其配置为在带有全局变量的情况下添加“cd”或“./”。

      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
      

      更多详情this post

      【讨论】:

        猜你喜欢
        • 2013-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-12
        • 2013-02-14
        • 1970-01-01
        • 1970-01-01
        • 2015-12-16
        相关资源
        最近更新 更多