如何在 Vim 中结合 :b 和 :ls 功能?

How to combine :b and :ls functionality in Vim?

在 Vim 中,有没有办法让 :b 列出打开的缓冲区旁边的缓冲区编号,其方式类似于 :ls 命令,但之后不必重新键入 :b?

这个由 a most valuable member of the community 推广的出色映射完全符合您的要求:

nnoremap gb :ls<CR>:b

更通用的方法……

Vim 使用该非交互式列表来显示许多其他有用命令的结果。我编写了这个简单的函数来插入正确的 "stub" 每次我在这些命令之一之后按 <CR> 时:

function! CmdCR()
    " grab the content of the command line
    let cmdline = getcmdline()

    if cmdline =~ '\C^ls'
        " like :ls but prompts for a buffer command
        return "\<CR>:b"

    elseif cmdline =~ '/#$'
        " like :g//# but prompts for a command
        return "\<CR>:"

    elseif cmdline =~ '\v\C^(dli|il)'
        " like :dlist or :ilist but prompts for a count for :djump or :ijump
        return "\<CR>:" . cmdline[0] . "jump  " . split(cmdline, " ")[1] . "\<S-Left>\<Left>"

    elseif cmdline =~ '\v\C^(cli|lli)'
        " like :clist or :llist but prompts for an error/location number
        return "\<CR>:silent " . repeat(cmdline[0], 2) . "\<Space>"

    elseif cmdline =~ '\C^old'
        " like :oldfiles but prompts for an old file to edit
        return "\<CR>:edit #<"

    else
        " default to a regular <CR>
        return "\<CR>"

    endif
endfunction

cnoremap <expr> <CR> CmdCR()