vim-go:如何打开GoFiles返回的文件

Vim-go plugin has a :GoFile function to show source files that depends on the current package. The output looks like this:

['/home/tretkow/tut/main.go', '/home/tretkow/tut/test.go']

How to open files from the list?

:GoFiles only echoes the output of go#tool#Files().

From the look of your snippet, it should be possible to extract a filename with something like:

:e <C-r>=go#tool#Files()[0]<CR>

or put that list in a scratch buffer:

:vnew<CR>
:0put=join(go#tool#files(), '')<CR>

/home/tretkow/tut/main.go
/home/tretkow/tut/test.go

and use gf to jump to the file under the cursor.


Here is a more sophisticated solution: the :GoFile command lets you choose what file from the :GoFiles command to edit via custom tab-completion.

" the command
command! -nargs=1 -complete=customlist,GoFilesComplete GoFile call GoFile(<f-args>)

" the completion function
function! GoFilesComplete(ArgLead, CmdLine, CursorPos)
    return filter(go#tool#Files(), 'v:val =~ a:ArgLead')
endfunction

" the :edit wrapper
function GoFile(file)
    execute "edit " . a:file
endfunction

Usage:

:GoFile <Tab>