Vim print buffer names to stdout on :q

时间:2016-03-04 17:50:17

标签: vim

Let me jump right in.

What I'm trying to do is simply print out the file path of any open buffer when I exit vim. This is useful because I often open other files in vim buffers with a vim script I wrote that can search through my codebase for a specific function call.

I figure I can set up an autocommand, either for when I open a file or when I leave vim, and use the output from :ls to list all currently open buffers. The problem that I'm having is that I can't get any output to show up in terminal. I have tried various combinations of :!echo in my function, but to no avail. I have been trying something like the following in my .vimrc

function! PrintFileName()
    :!echo "hello"
    :exec "!echo world"
    "... etc
endfunction
au BufRead * call PrintFileName()

Both :!echo foobar and :call PrintFileName() work for me if I do it from the command line. I also figure I might have to use some form of silent/redraw! so I don't have to hit enter to continue.

Really the main problem here is that I can't see a way to get output to stdout from inside my function and called by an autocommand.

Thanks for any help.

1 个答案:

答案 0 :(得分:0)

好的,所以我找到了这个解决方案,只要我从终端的最后一行输入vim就行了。否则,这会打印出当前行下方的一行,并在按Enter键时被覆盖。如果有人知道如何解决这个让我知道,否则我会用这个。

function! PrintBuffers()
    redir => files
    :ls
    redir END
    " Regex to strip out everything from :ls but the buffer filenames
    let files = substitute(files, '^[^"]*"', '', 'g')
    let files = substitute(files, '"[^"]*\n[^"]*"', '\n', 'g')
    let files = substitute(files, '"[^"]*$','','g')
    " This is the magic line
    exe '!echo; echo ' . shellescape(&t_te . files)
endfunction
au VimLeave * call PrintBuffers()

*注意 - 当我写这篇文章时,我意识到如果你在某个时候做了:cd,这将不会显示正确的路径。所以我觉得它非常脆弱,但是它确实起到了作用。