Search code examples
shellvimvineovim

z jump around equivalent for editing files?


Is there an equivalent to z jump around for opening most edited files with a terminal editor like vim in my case? I have browsed GitHub but couldn't find anything.


Solution

  • Vim remembers recently opened files by v:oldfiles which is loaded from ~/.viminfo file.


    Custom completion

    With fzf and custom-fuzzy-completion, we can search for file from history more naturally, like vim **<TAB> with follows

    # Custom fuzzy completion for "vim" command
    #   e.g. vim **<TAB>
    _fzf_complete_vim() {
      _fzf_complete --multi --reverse --prompt="vim> " -- "$@" < <(
          cat ~/.viminfo | grep '^>' | sed 's/^> //'
      )
    }
    

    In zsh you're good to go. In bash, you have to manually call thecomplete command.

    [ -n "$BASH" ] && complete -F _fzf_complete_vim -o default -o bashdefault vim
    

    As the documentation says, the custom completion API is experimental and subject to change.


    The previous answer using a new command

    For instance, we can read history from it and search for file using fzf as follows

    #!/bin/bash
    
    file=$(cat ~/.viminfo | grep '^>' | sed 's/^> //' | fzf)
    file=${file/#\~/$HOME}
    if [ ! -z "$file" ]; then
      vim $file
    fi
    
    • Read .viminfo. Get the opened file list. Pass it to fzf
    • Replace ~ with $HOME (Vim doesn't expand ~ in the file argument)
    • Open the file if not empty

    You can save it as vim-history.sh and run it in the terminal.