2014-10-31 17:30:24 -04:00
|
|
|
# vim-go
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
Go (golang) support for Vim, which comes with pre-defined sensible settings (like
|
|
|
|
auto gofmt on save), with autocomplete, snippet support, improved syntax
|
|
|
|
highlighting, go toolchain commands, and more. If needed vim-go installs all
|
2014-10-31 17:30:24 -04:00
|
|
|
necessary binaries for providing seamless Vim integration with current
|
|
|
|
commands. It's highly customizable and each individual feature can be
|
|
|
|
disabled/enabled easily.
|
|
|
|
|
|
|
|
![vim-go](https://dl.dropboxusercontent.com/u/174404/vim-go-2.png)
|
|
|
|
|
|
|
|
## Features
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
* Improved Syntax highlighting with items such as Functions, Operators, Methods.
|
2014-10-31 17:30:24 -04:00
|
|
|
* Auto completion support via `gocode`
|
2015-12-08 08:20:04 -05:00
|
|
|
* Better `gofmt` on save, which keeps cursor position and doesn't break your undo
|
2014-10-31 17:30:24 -04:00
|
|
|
history
|
2015-07-13 06:22:46 -04:00
|
|
|
* Go to symbol/declaration with `:GoDef`
|
2015-12-08 08:20:04 -05:00
|
|
|
* Look up documentation with `:GoDoc` inside Vim or open it in browser
|
2015-07-13 06:22:46 -04:00
|
|
|
* Automatically import packages via `:GoImport` or plug it into autosave
|
2015-12-08 08:20:04 -05:00
|
|
|
* Compile your package with `:GoBuild`, install it with `:GoInstall` or test
|
|
|
|
them with `:GoTest` (also supports running single tests via `:GoTestFunc`)
|
|
|
|
* Quickly execute your current file/files with `:GoRun`
|
|
|
|
* Automatic `GOPATH` detection based on the directory structure (i.e. `gb`
|
|
|
|
projects, `godep` vendored projects)
|
2015-07-13 06:22:46 -04:00
|
|
|
* Change or display `GOPATH` with `:GoPath`
|
2016-04-12 04:31:09 -04:00
|
|
|
* Create a coverage profile and display annotated source code to see which
|
|
|
|
functions are covered with `:GoCoverage`
|
2015-12-08 08:20:04 -05:00
|
|
|
* Call `gometalinter` with `:GoMetaLinter`, which invokes all possible linters
|
|
|
|
(golint, vet, errcheck, deadcode, etc..) and shows the warnings/errors
|
2015-07-13 06:22:46 -04:00
|
|
|
* Lint your code with `:GoLint`
|
2015-12-08 08:20:04 -05:00
|
|
|
* Run your code through `:GoVet` to catch static errors
|
2016-05-14 07:57:54 -04:00
|
|
|
* Advanced source analysis tools utilizing guru, such as `:GoImplements`,
|
2015-12-08 08:20:04 -05:00
|
|
|
`:GoCallees`, and `:GoReferrers`
|
2015-07-13 06:22:46 -04:00
|
|
|
* Precise type-safe renaming of identifiers with `:GoRename`
|
2014-10-31 17:30:24 -04:00
|
|
|
* List all source files and dependencies
|
2015-12-08 08:20:04 -05:00
|
|
|
* Unchecked error checking with `:GoErrCheck`
|
|
|
|
* Integrated and improved snippets, supporting `ultisnips` or `neosnippet`
|
2015-07-13 06:22:46 -04:00
|
|
|
* Share your current code to [play.golang.org](http://play.golang.org) with `:GoPlay`
|
|
|
|
* On-the-fly type information about the word under the cursor. Plug it into
|
|
|
|
your custom vim function.
|
2016-02-20 08:13:10 -05:00
|
|
|
* Go asm formatting on save
|
2014-10-31 17:30:24 -04:00
|
|
|
* Tagbar support to show tags of the source code in a sidebar with `gotags`
|
2015-12-08 08:20:04 -05:00
|
|
|
* Custom vim text objects such as `a function` or `inner function`
|
|
|
|
list.
|
2016-04-12 04:31:09 -04:00
|
|
|
* Jump to function or type declarations with `:GoDecls` or `:GoDeclsDir`
|
2016-02-20 08:13:10 -05:00
|
|
|
* A async launcher for the go command is implemented for Neovim, fully async
|
|
|
|
building and testing (beta).
|
|
|
|
* Integrated with the Neovim terminal, launch `:GoRun` and other go commands
|
|
|
|
in their own new terminal. (beta)
|
|
|
|
* Alternate between implementation and test code with `:GoAlternate`
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-08-02 08:48:32 -04:00
|
|
|
Checkout the official [tutorial](https://github.com/fatih/vim-go-tutorial)
|
|
|
|
that goes literally over all features and shows many tips and tricks. It shows
|
|
|
|
how to install vim-go and explains many unknown use cases. Recommended for
|
|
|
|
beginners as well as advanced users: https://github.com/fatih/vim-go-tutorial
|
|
|
|
|
2014-10-31 17:30:24 -04:00
|
|
|
## Install
|
|
|
|
|
2016-08-02 08:48:32 -04:00
|
|
|
Master branch is supposed to be a development branch. So stuff here can break
|
|
|
|
and change. Please try use always the [latest
|
|
|
|
release](https://github.com/fatih/vim-go/releases/latest)
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2015-12-16 08:53:53 -05:00
|
|
|
Vim-go follows the standard runtime path structure, so I highly recommend to
|
|
|
|
use a common and well known plugin manager to install vim-go. Do not use vim-go
|
|
|
|
with other Go oriented vim plugins. For Pathogen just clone the repo. For other
|
|
|
|
plugin managers add the appropriate lines and execute the plugin's install
|
|
|
|
command.
|
2015-02-24 05:45:22 -05:00
|
|
|
|
|
|
|
* [Pathogen](https://github.com/tpope/vim-pathogen)
|
|
|
|
* `git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go`
|
|
|
|
* [vim-plug](https://github.com/junegunn/vim-plug)
|
|
|
|
* `Plug 'fatih/vim-go'`
|
|
|
|
* [NeoBundle](https://github.com/Shougo/neobundle.vim)
|
|
|
|
* `NeoBundle 'fatih/vim-go'`
|
|
|
|
* [Vundle](https://github.com/gmarik/vundle)
|
|
|
|
* `Plugin 'fatih/vim-go'`
|
2016-08-02 08:48:32 -04:00
|
|
|
* [Vim packages](http://vimhelp.appspot.com/repeat.txt.html#packages) (since Vim 7.4.1528)
|
|
|
|
* `git clone https://github.com/fatih/vim-go.git ~/.vim/pack/plugins/start/vim-go`
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
Please be sure all necessary binaries are installed (such as `gocode`, `godef`,
|
2015-12-08 08:20:04 -05:00
|
|
|
`goimports`, etc.). You can easily install them with the included
|
|
|
|
`:GoInstallBinaries` command. If invoked, all necessary binaries will be
|
2015-02-24 05:45:22 -05:00
|
|
|
automatically downloaded and installed to your `$GOBIN` environment (if not set
|
2015-12-16 08:53:53 -05:00
|
|
|
it will use `$GOPATH/bin`). Note that this command requires `git` for fetching
|
|
|
|
the individual Go packages. Additionally, use `:GoUpdateBinaries` to update the
|
|
|
|
installed binaries.
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
### Optional
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
* Autocompletion is enabled by default via `<C-x><C-o>`. To get real-time
|
2014-10-31 17:30:24 -04:00
|
|
|
completion (completion by type) install:
|
2016-02-20 08:13:10 -05:00
|
|
|
[neocomplete](https://github.com/Shougo/neocomplete.vim) for Vim or
|
|
|
|
[deoplete](https://github.com/Shougo/deoplete.nvim) and
|
2016-08-02 08:48:32 -04:00
|
|
|
[deoplete-go](https://github.com/zchee/deoplete-go) for NeoVim or
|
|
|
|
[SimpleAutoCmplPop](https://github.com/roxma/SimpleAutoComplPop)
|
2015-12-08 08:20:04 -05:00
|
|
|
* To display source code tag information on a sidebar install
|
2014-10-31 17:30:24 -04:00
|
|
|
[tagbar](https://github.com/majutsushi/tagbar).
|
2015-12-08 08:20:04 -05:00
|
|
|
* For snippet features install:
|
2016-02-20 08:13:10 -05:00
|
|
|
[neosnippet](https://github.com/Shougo/neosnippet.vim) or
|
|
|
|
[ultisnips](https://github.com/SirVer/ultisnips).
|
|
|
|
* Screenshot color scheme is a slightly modified molokai:
|
|
|
|
[fatih/molokai](https://github.com/fatih/molokai).
|
|
|
|
* For a better documentation viewer checkout:
|
|
|
|
[go-explorer](https://github.com/garyburd/go-explorer).
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
Many of the plugin's [features](#features) are enabled by default. There are no
|
2015-01-18 07:58:28 -05:00
|
|
|
additional settings needed. All usages and commands are listed in
|
2015-07-13 06:22:46 -04:00
|
|
|
`doc/vim-go.txt`. Note that help tags needs to be populated. Check your plugin
|
|
|
|
manager settings to generate the documentation (some do it automatically).
|
|
|
|
After that just open the help page to see all commands:
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
:help vim-go
|
|
|
|
|
2016-05-14 07:57:54 -04:00
|
|
|
## Example Mappings
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
vim-go has several `<Plug>` mappings which can be used to create custom
|
2016-05-14 07:57:54 -04:00
|
|
|
mappings. Unless otherwise specified, none of these mappings are enabled
|
|
|
|
by default. Here some examples you might find useful:
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
Run commands such as `go run` for the current file with `<leader>r` or `go
|
2016-02-20 08:13:10 -05:00
|
|
|
build` and `go test` for the current package with `<leader>b` and `<leader>t`
|
|
|
|
respectively. Display beautifully annotated source code to see which functions
|
|
|
|
are covered with `<leader>c`.
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
```vim
|
2015-07-13 06:22:46 -04:00
|
|
|
au FileType go nmap <leader>r <Plug>(go-run)
|
|
|
|
au FileType go nmap <leader>b <Plug>(go-build)
|
|
|
|
au FileType go nmap <leader>t <Plug>(go-test)
|
|
|
|
au FileType go nmap <leader>c <Plug>(go-coverage)
|
2014-10-31 17:30:24 -04:00
|
|
|
```
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
By default the mapping `gd` is enabled, which opens the target identifier in
|
|
|
|
current buffer. You can also open the definition/declaration, in a new vertical,
|
|
|
|
horizontal, or tab, for the word under your cursor:
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
```vim
|
2015-07-13 06:22:46 -04:00
|
|
|
au FileType go nmap <Leader>ds <Plug>(go-def-split)
|
|
|
|
au FileType go nmap <Leader>dv <Plug>(go-def-vertical)
|
|
|
|
au FileType go nmap <Leader>dt <Plug>(go-def-tab)
|
2014-10-31 17:30:24 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
Open the relevant Godoc for the word under the cursor with `<leader>gd` or open
|
|
|
|
it vertically with `<leader>gv`
|
|
|
|
|
|
|
|
```vim
|
|
|
|
au FileType go nmap <Leader>gd <Plug>(go-doc)
|
|
|
|
au FileType go nmap <Leader>gv <Plug>(go-doc-vertical)
|
|
|
|
```
|
|
|
|
|
|
|
|
Or open the Godoc in browser
|
|
|
|
|
|
|
|
```vim
|
|
|
|
au FileType go nmap <Leader>gb <Plug>(go-doc-browser)
|
|
|
|
```
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
Show a list of interfaces which is implemented by the type under your cursor
|
|
|
|
with `<leader>s`
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
```vim
|
2015-07-13 06:22:46 -04:00
|
|
|
au FileType go nmap <Leader>s <Plug>(go-implements)
|
2014-10-31 17:30:24 -04:00
|
|
|
```
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
Show type info for the word under your cursor with `<leader>i` (useful if you
|
|
|
|
have disabled auto showing type info via `g:go_auto_type_info`)
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
```vim
|
2015-07-13 06:22:46 -04:00
|
|
|
au FileType go nmap <Leader>i <Plug>(go-info)
|
2014-10-31 17:30:24 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
Rename the identifier under the cursor to a new name
|
|
|
|
|
|
|
|
```vim
|
|
|
|
au FileType go nmap <Leader>e <Plug>(go-rename)
|
|
|
|
```
|
|
|
|
|
|
|
|
More `<Plug>` mappings can be seen with `:he go-mappings`. Also these are just
|
|
|
|
recommendations, you are free to create more advanced mappings or functions
|
|
|
|
based on `:he go-commands`.
|
|
|
|
|
|
|
|
## Settings
|
2016-02-20 08:13:10 -05:00
|
|
|
Below are some settings you might find useful. For the full list see `:he
|
|
|
|
go-settings`.
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
By default syntax-highlighting for Functions, Methods and Structs is disabled.
|
|
|
|
To change it:
|
2014-10-31 17:30:24 -04:00
|
|
|
```vim
|
2015-07-13 06:22:46 -04:00
|
|
|
let g:go_highlight_functions = 1
|
|
|
|
let g:go_highlight_methods = 1
|
2016-06-11 09:56:50 -04:00
|
|
|
let g:go_highlight_fields = 1
|
2016-07-03 07:53:59 -04:00
|
|
|
let g:go_highlight_types = 1
|
2015-07-13 06:22:46 -04:00
|
|
|
let g:go_highlight_operators = 1
|
|
|
|
let g:go_highlight_build_constraints = 1
|
2014-10-31 17:30:24 -04:00
|
|
|
```
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
Enable goimports to automatically insert import paths instead of gofmt:
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
```vim
|
2015-07-13 06:22:46 -04:00
|
|
|
let g:go_fmt_command = "goimports"
|
2014-10-31 17:30:24 -04:00
|
|
|
```
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
By default vim-go shows errors for the fmt command, to disable it:
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
```vim
|
2015-07-13 06:22:46 -04:00
|
|
|
let g:go_fmt_fail_silently = 1
|
2014-10-31 17:30:24 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
Disable auto fmt on save:
|
|
|
|
|
|
|
|
```vim
|
|
|
|
let g:go_fmt_autosave = 0
|
|
|
|
```
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
Disable opening browser after posting your snippet to `play.golang.org`:
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
```vim
|
2015-07-13 06:22:46 -04:00
|
|
|
let g:go_play_open_browser = 0
|
2014-10-31 17:30:24 -04:00
|
|
|
```
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
By default when `:GoInstallBinaries` is called, the binaries are installed to
|
|
|
|
`$GOBIN` or `$GOPATH/bin`. To change it:
|
|
|
|
|
2014-10-31 17:30:24 -04:00
|
|
|
```vim
|
2015-07-13 06:22:46 -04:00
|
|
|
let g:go_bin_path = expand("~/.gotools")
|
|
|
|
let g:go_bin_path = "/home/fatih/.mypath" "or give absolute path
|
2014-10-31 17:30:24 -04:00
|
|
|
```
|
|
|
|
|
2016-06-11 09:56:50 -04:00
|
|
|
Disable updating dependencies when installing/updating binaries:
|
|
|
|
```vim
|
|
|
|
let g:go_get_update = 0
|
|
|
|
```
|
|
|
|
|
2016-02-20 08:13:10 -05:00
|
|
|
### Using with Neovim (beta)
|
|
|
|
|
|
|
|
Note: Neovim currently is not a first class citizen for vim-go. You are free
|
|
|
|
to open bugs but I'm not going to look at them. Even though I'm using Neovim
|
|
|
|
myself, Neovim itself is still alpha. So vim-go might not work well as good as
|
|
|
|
in Vim. I'm happy to accept pull requests or very detailed bug reports.
|
2015-12-16 08:53:53 -05:00
|
|
|
|
|
|
|
|
2016-02-20 08:13:10 -05:00
|
|
|
Run `:GoRun` in a new tab, horizontal split or vertical split terminal
|
2015-12-16 08:53:53 -05:00
|
|
|
|
|
|
|
```vim
|
2016-02-20 08:13:10 -05:00
|
|
|
au FileType go nmap <leader>rt <Plug>(go-run-tab)
|
|
|
|
au FileType go nmap <Leader>rs <Plug>(go-run-split)
|
|
|
|
au FileType go nmap <Leader>rv <Plug>(go-run-vertical)
|
2015-12-16 08:53:53 -05:00
|
|
|
```
|
|
|
|
|
2016-02-20 08:13:10 -05:00
|
|
|
By default new terminals are opened in a vertical split. To change it
|
|
|
|
|
|
|
|
```vim
|
|
|
|
let g:go_term_mode = "split"
|
|
|
|
```
|
|
|
|
|
|
|
|
By default the testing commands run asynchronously in the background and
|
|
|
|
display results with `go#jobcontrol#Statusline()`. To make them run in a new
|
|
|
|
terminal
|
|
|
|
|
|
|
|
```vim
|
|
|
|
let g:go_term_enabled = 1
|
|
|
|
```
|
2015-12-16 08:53:53 -05:00
|
|
|
|
|
|
|
### Using with Syntastic
|
2016-02-20 08:13:10 -05:00
|
|
|
Sometimes when using both `vim-go` and `syntastic` Vim will start lagging while
|
|
|
|
saving and opening files. The following fixes this:
|
2015-02-04 05:43:54 -05:00
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
```vim
|
|
|
|
let g:syntastic_go_checkers = ['golint', 'govet', 'errcheck']
|
|
|
|
let g:syntastic_mode_map = { 'mode': 'active', 'passive_filetypes': ['go'] }
|
|
|
|
```
|
2015-02-04 05:43:54 -05:00
|
|
|
|
2016-05-14 07:57:54 -04:00
|
|
|
Another issue with `vim-go` and `syntastic` is that the location list window
|
|
|
|
that contains the output of commands such as `:GoBuild` and `:GoTest` might not appear.
|
|
|
|
To resolve this:
|
|
|
|
|
|
|
|
```vim
|
|
|
|
let g:go_list_type = "quickfix"
|
|
|
|
```
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
## More info
|
2015-02-04 05:43:54 -05:00
|
|
|
|
2016-02-20 08:13:10 -05:00
|
|
|
Check out the [Wiki](https://github.com/fatih/vim-go/wiki) page for more
|
|
|
|
information. It includes
|
|
|
|
[Screencasts](https://github.com/fatih/vim-go/wiki/Screencasts), an [FAQ
|
|
|
|
section](https://github.com/fatih/vim-go/wiki/FAQ-Troubleshooting), and many
|
|
|
|
other [various pieces](https://github.com/fatih/vim-go/wiki) of information.
|
2015-02-04 05:43:54 -05:00
|
|
|
|
2016-05-14 07:57:54 -04:00
|
|
|
## Donation
|
|
|
|
|
|
|
|
People have asked for this for a long time, now you can be a fully supporter by
|
2016-08-02 08:48:32 -04:00
|
|
|
[being a patron](https://www.patreon.com/fatih)!
|
|
|
|
|
|
|
|
By being a patron, you are enabling vim-go to grow and mature, helping me to
|
|
|
|
invest in bug fixes, new documentation, and improving both current and future
|
|
|
|
features. It's completely optional and is just a direct way to support Vim-go's
|
|
|
|
ongoing development. Thanks!
|
2016-05-14 07:57:54 -04:00
|
|
|
|
|
|
|
[https://www.patreon.com/fatih](https://www.patreon.com/fatih)
|
|
|
|
|
2014-10-31 17:30:24 -04:00
|
|
|
## Credits
|
|
|
|
|
|
|
|
* Go Authors for official vim plugins
|
2016-05-14 07:57:54 -04:00
|
|
|
* Gocode, Godef, Golint, Guru, Goimports, Gotags, Errcheck projects and
|
2016-02-20 08:13:10 -05:00
|
|
|
authors of those projects.
|
2015-07-13 06:22:46 -04:00
|
|
|
* Other vim-plugins, thanks for inspiration (vim-golang, go.vim, vim-gocode,
|
|
|
|
vim-godef)
|
|
|
|
* [Contributors](https://github.com/fatih/vim-go/graphs/contributors) of vim-go
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
The BSD 3-Clause License - see `LICENSE` for more details
|