为VIM添加GoLang语法高亮。

huangapple go评论87阅读模式
英文:

Add GoLang syntax highlighting for VIM

问题

我正在尝试在Ubuntu上为VIM添加Go语言的语法高亮,参考资源和指导在这里提供:http://go-lang.cat-v.org/text-editors/vim/。

Go自带一个名为go.vim的文件,其中包含了VIM的语法设置,上述页面提供了以下指令:

将$GOROOT/misc/vim/syntax/go.vim放置在~/.vim/syntax/目录下,并在~/.vim/ftdetect/go.vim中添加以下内容:

au BufRead,BufNewFile *.go set filetype=go 

这基本上是我在其他地方看到的自定义vim语法的相同步骤(https://stackoverflow.com/questions/12962233/vim-7-3-on-ubuntu-12-10-doesnt-have-ftplugin-directory-anywhere和https://github.com/jnwhiteh/vim-golang/blob/master/readme.txt)。

所以我认为我在创建以下目录时是正确的:
~/.vim
~/.vim/syntax
~/.vim/ftdetect

并按照上述指令添加了go.vim到~/.vim/syntax/,并在~/.vim/ftdetect/中创建了一个名为go.vim的文件,其中包含以下内容:

au BufRead,BufNewFile *.go set filetype=go

然而,语法高亮似乎没有发生。我需要做些什么来强制VIM查看这些新的设置文件吗?

英文:

I'm trying to add Go language syntax highlighting to VIM on ubuntu with resources and direction supplied here http://go-lang.cat-v.org/text-editors/vim/.

Go comes with a go.vim file that contains syntax settings for VIM and the above page offers the following instructions

Place $GOROOT/misc/vim/syntax/go.vim in ~/.vim/syntax/ and put the following in ~/.vim/ftdetect/go.vim:

au BufRead,BufNewFile *.go set filetype=go 

This is more or less the same vein of procedure for customizing vim syntax I've seen elsewhere
(https://stackoverflow.com/questions/12962233/vim-7-3-on-ubuntu-12-10-doesnt-have-ftplugin-directory-anywhere and https://github.com/jnwhiteh/vim-golang/blob/master/readme.txt)

So I think I'm doing the right thing when I create directories:<br>
~/.vim<br>
~/.vim/syntax<br>
~/.vim/ftdetect<br>

and follow the above instructions by adding

go.vim to ~/.vim/syntax/
and creating a file, go.vim, in ~/.vim/ftdetect/ which contains

au BufRead,BufNewFile *.go set filetype=go

Yet syntax highlighting does not seem to occur. Is there something I need to do to force VIM to look at these new settings files?

答案1

得分: 65

标准的Go发行版中包含了用于Vim的Go文件,位于go/misc/vim/目录下。该目录包含一个readme.txt文件,其中包含了安装说明。

> readme.txt

> Go的Vim插件 (http://golang.org)
>
> 要使用所有的Vim插件,请将以下内容添加到您的$HOME/.vimrc文件中。
>
> " 一些Linux发行版在/etc/vimrc中设置了filetype。
> " 在更改runtimepath之前清除filetype标志,以强制Vim重新加载它们。
> filetype off
> filetype plugin indent off
> set runtimepath+=$GOROOT/misc/vim
> filetype plugin indent on
> syntax on
>
> 如果您想选择较少的插件,请使用本文件中的其他说明。
>
> <<..SNIP..>>

英文:

UPDATE:

> Go 1.4 Release Notes
>
>
> Miscellany
>
> The standard repository's top-level misc directory used to contain Go
> support for editors and IDEs: plugins, initialization
> scripts and so on. Maintaining these was becoming time-consuming and
> needed external help because many of the editors listed were not used
> by members of the core team. It also required us to make decisions
> about which plugin was best for a given editor, even for editors we do
> not use.
> The Go community at large is much better suited to managing this information. In Go 1.4, therefore, this support has been removed from
> the repository. Instead, there is a curated, informative list of
> what's available on a wiki page.


The standard Go distribution includes Go files for Vim in go/misc/vim/. This directory contains a readme.txt file which contains installation instructions.

> readme.txt

> Vim plugins for Go (http://golang.org)
>
> To use all the Vim plugins, add these lines to your $HOME/.vimrc.
>
> " Some Linux distributions set filetype in /etc/vimrc.
> " Clear filetype flags before changing runtimepath to force Vim to reload them.
> filetype off
> filetype plugin indent off
> set runtimepath+=$GOROOT/misc/vim
> filetype plugin indent on
> syntax on
>
> If you want to select fewer plugins, use the instructions in the rest
> of this file.
>
> <<..SNIP..>>

答案2

得分: 40

在Debian上,我想在ubuntu上也是一样的,你只需要执行以下命令:

sudo apt-get install vim-gocomplete gocode vim-syntax-go
vim-addon-manager install go-syntax
vim-addon-manager install gocode
英文:

On Debian, I suppose it's the same on ubuntu, you just :

sudo apt-get install vim-gocomplete gocode vim-syntax-go
vim-addon-manager install go-syntax
vim-addon-manager install gocode

答案3

得分: 25

你可以将以下代码添加到你的~/.vimrc文件中:

set rtp+=$GOROOT/misc/vim
filetype plugin indent on
syntax on

编辑:在这些代码之前(即.vimrc文件的开头),假设已经设置了filetype plugin indent off,如果没有设置可能会导致问题。请参考下面@peterSO的回答以获取更安全的版本。

英文:

you can just add these lines to your ~/.vimrc:

set rtp+=$GOROOT/misc/vim
filetype plugin indent on
syntax on

EDIT This assumes filetype plugin indent off before these lines (i.e. beginning of .vimrc file) and may cause problems if it's not. See @peterSO's answer below for the safer version.

答案4

得分: 21

对于最好的语法高亮,请尝试使用https://github.com/fatih/vim-go

这是一个整合了许多vim插件并添加了许多功能的新项目。从自述文件中可以看到:

  • 改进的语法高亮,如函数、运算符、方法等
  • 通过gocode支持自动完成
  • 保存时更好的gofmt,保持光标位置并不会破坏撤销历史
  • 使用godef进行符号/声明跳转
  • 通过goimports自动导入包
  • 编译和构建您的包,并使用go install进行安装
  • 快速运行当前文件/文件的go run
  • 运行go test并在快速修复窗口中查看任何错误
  • 使用golint对代码进行静态检查
  • 通过go vet运行代码以捕获静态错误
  • 使用oracle进行高级源代码分析
  • 列出所有源文件和依赖项
  • 使用errcheck检查未检查的错误
  • 集成和改进的代码片段。支持ultisnips或neosnippet
  • 将当前代码分享到play.golang.org
英文:

For the best syntax highlighting try https://github.com/fatih/vim-go

It's a new project that consolidates many vim plugins and adds a lot of features. From the readme:

  • Improved Syntax highlighting, such as Functions, Operators, Methods..
  • Auto completion support via gocode
  • Better gofmt on save, keeps cursor position and doesn't break your undo history
  • Go to symbol/declaration with godef
  • Automatically import packages via goimports
  • Compile and go build your package, install it with go install
  • go run quickly your current file/files
  • Run go test and see any errors in quickfix window
  • Lint your code with golint
  • Run your code trough go vet to catch static errors.
  • Advanced source analysis tool with oracle
  • List all source files and dependencies
  • Checking with errcheck for unchecked errors.
  • Integrated and improved snippets. Supports ultisnips or neosnippet
  • Share your current code to play.golang.org

答案5

得分: 9

请参阅 https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins,因为现在Go存储库中的所有编辑器和shell支持已被移除(https://codereview.appspot.com/105470043)。

英文:

on 25/Jan/2015

Please see https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins as now all editor & shell support in Go repo is removed (https://codereview.appspot.com/105470043)

答案6

得分: 5

我在CentOS 7上没有找到关于打开vim语法高亮的指令。已经在CentOS 7.3.1611上测试了以下指令。首先,在您的主目录中创建以下目录:

$ mkdir ~/.vim/ftdetect/

然后,在上述目录中创建一个名为go.vim的文件,内容如下:

au BufRead,BufNewFile *.go set filetype=go

下载Go的语法定义文件:vim.go。将其转移到正确的系统范围目录,以便多个用户可以共享:

$ sudo mv -i go.vim /usr/share/vim/vim74/syntax/
英文:

I did not find instructions on turning on vim syntax highlighting for CentOS 7. Have tested the ensuing instructions to work on CentOS 7.3.1611. First, create the following directory in your home directory:

$ mkdir ~/.vim/ftdetect/

Then, create a file named, go.vim inside the above directory with the contents:

au BufRead,BufNewFile *.go set filetype=go

Download the syntax definition file for Go: vim.go. Transfer it to the right system-wide directory so that multiple users can share:

$ sudo mv -i go.vim /usr/share/vim/vim74/syntax/

答案7

得分: 4

由于某种原因,我们在开发虚拟机上通过Debian软件包安装了Golang。从我搜索的结果来看,这个特定版本的vim没有任何有关vim的好东西。无论如何,为了快速将这些好东西部署到所有这些开发虚拟机上,我决定采用vundle的方法。如果你愿意,你可以将这种方法整合到puppet或其他工具中,但我们没有这样做。无论如何,以下是我所做的:

步骤1:安装vundle:
https://github.com/gmarik/vundle

步骤2:将以下代码放入你的.vimrc文件中(当然是从这里拷贝的:https://github.com/jnwhiteh/vim-golang ),然后从命令行运行vim +BundleInstall +qall或在vim中运行:BundleInstall

Bundle 'jnwhiteh/vim-golang'

步骤3:将以下bash脚本保存为govim.sh或其他名称,运行chmod +x govim.sh,然后像这样运行它./govim.sh

脚本如下:

#!/bin/bash
mkdir $HOME/.vim/ftdetect
mkdir $HOME/.vim/syntax
mkdir $HOME/.vim/autoload
mkdir $HOME/.vim/autoload/go
mkdir $HOME/.vim/ftplugin
mkdir $HOME/.vim/ftplugin/go
mkdir $HOME/.vim/indent
mkdir $HOME/.vim/compiler
mkdir $HOME/.vim/plugin
mkdir $HOME/.vim/plugin/godoc
ln -s $HOME/.vim/bundle/vim-golang/ftdetect/gofiletype.vim $HOME/.vim/ftdetect
ln -s $HOME/.vim/bundle/vim-golang/syntax/go.vim $HOME/.vim/syntax/
ln -s $HOME/.vim/bundle/vim-golang/autoload/go/complete.vim $HOME/.vim/autoload/go/
ln -s $HOME/.vim/bundle/vim-golang/ftplugin/go.vim $HOME/.vim/ftplugin/
ln -s $HOME/.vim/bundle/vim-golang/ftplugin/go/*.vim $HOME/.vim/ftplugin/go/
ln -s $HOME/.vim/bundle/vim-golang/indent/go.vim $HOME/.vim/indent/
ln -s $HOME/.vim/bundle/vim-golang/compiler/go.vim $HOME/.vim/compiler/
ln -s $HOME/.vim/bundle/vim-golang/plugin/godoc/godoc.vim $HOME/.vim/plugin/godoc/
ln -s $HOME/.vim/bundle/vim-golang/syntax/godoc.vim $HOME/.vim/syntax/

完成!你现在已经安装了所有的好东西,如果我说错了,请有人纠正我,但也许比官方的Golang发行版提供的还要多。我还没有尝试过这个,所以我也不确定,但是我认为如果你在这里使用Vundle,runtimepath/rtp可能会被覆盖。

英文:

For whatever reason outside of my own decision making, we got Golang installed on our dev VMs by Debian packages. This particular distribution of vim doesn't come with any of the goodies for vim as far as I was able to tell from a search around for it. Anyways, I decided to go the vundle route in order to rapidly deploy the goodies to all these dev VMs. You could probably work this method into puppet or something if you'd like, we didn't do that though. Anyways, here's what I did:

Step 1: Install vundle:
https://github.com/gmarik/vundle

Step 2: put this line in your .vimrc (It's from here, of course: https://github.com/jnwhiteh/vim-golang ), and then run vim from the command line like vim +BundleInstall +qall or from within vim with :BundleInstall

Bundle &#39;jnwhiteh/vim-golang&#39;

Step 3: Save this little bash script I whipped up as govim.sh or whatever, chmod +x govim.sh, and run it like ./govim.sh

Script as follows:

#!/bin/bash
mkdir $HOME/.vim/ftdetect
mkdir $HOME/.vim/syntax
mkdir $HOME/.vim/autoload
mkdir $HOME/.vim/autoload/go
mkdir $HOME/.vim/ftplugin
mkdir $HOME/.vim/ftplugin/go
mkdir $HOME/.vim/indent
mkdir $HOME/.vim/compiler
mkdir $HOME/.vim/plugin
mkdir $HOME/.vim/plugin/godoc
ln -s $HOME/.vim/bundle/vim-golang/ftdetect/gofiletype.vim $HOME/.vim/ftdetect
ln -s $HOME/.vim/bundle/vim-golang/syntax/go.vim $HOME/.vim/syntax/
ln -s $HOME/.vim/bundle/vim-golang/autoload/go/complete.vim $HOME/.vim/autoload/go/
ln -s $HOME/.vim/bundle/vim-golang/ftplugin/go.vim $HOME/.vim/ftplugin/
ln -s $HOME/.vim/bundle/vim-golang/ftplugin/go/*.vim $HOME/.vim/ftplugin/go/
ln -s $HOME/.vim/bundle/vim-golang/indent/go.vim $HOME/.vim/indent/
ln -s $HOME/.vim/bundle/vim-golang/compiler/go.vim $HOME/.vim/compiler/
ln -s $HOME/.vim/bundle/vim-golang/plugin/godoc/godoc.vim $HOME/.vim/plugin/godoc/
ln -s $HOME/.vim/bundle/vim-golang/syntax/godoc.vim $HOME/.vim/syntax/

Kaching! You now have all the goodies installed, and someone correct me if I'm wrong on this but perhaps more than what comes with the official Golang distribution. I don't know about this either having not tried it yet, but I think that the runtimepath/rtp gets clobbered if you use Vundle with the other answers here anyways.

答案8

得分: 4

应该很容易,就像1、2、3一样:

  1. 下载文件vim.go,并将其放置在~/.vim/syntax目录下,重命名为go.vim(如果没有该目录,请创建)。

  2. 如果你还没有文件~/.vim/ftdetect/go.vim,请创建它(如果需要,请创建文件夹)。

  3. .vim/ftdetect/go.vim中,添加以下行:au BufRead,BufNewFile *.go set filetype=go

英文:

It should be easy as 1, 2, 3 :

  1. Download the file vim.go and place in in ~/.vim/syntax under the name go.vim (create the directory syntax if you don't already have it).

  2. If you don't already have the file ~/.vim/ftdetect/go.vim, create it (and the folder if necessary).

  3. In .vim/ftdetect/go.vim, add the following line : au BufRead,BufNewFile *.go set filetype=go

答案9

得分: 3

这个页面说:

将$GOROOT/misc/vim/syntax/go.vim放入~/.vim/syntax/,
并将以下内容放入~/.vim/ftdetect/go.vim:

au BufRead,BufNewFile *.go set filetype=go

对我来说有效,只是我一开始没有找到/misc/vim/go.vim目录。所以我从另一台已经在/usr/local/go/...上安装了go的计算机上复制了文件。

英文:

This page says that:

Place $GOROOT/misc/vim/syntax/go.vim in ~/.vim/syntax/ 
and put the following in ~/.vim/ftdetect/go.vim:

au BufRead,BufNewFile *.go set filetype=go

It worked for me, only i did not find the /misc/vim/go.vim directory at first. So i copied the files from another computer that had installed go on /usr/local/go/...

答案10

得分: 0

这是在MAC上适用的方法:

let g:tagbar_type_go = {
        \ 'ctagstype' : 'go',
        \ 'kinds'     : [
                \ 'p:package',
                \ 'i:imports:1',
                \ 'c:constants',
                \ 'v:variables',
                \ 't:types',
                \ 'n:interfaces',
                \ 'w:fields',
                \ 'e:embedded',
                \ 'm:methods',
                \ 'r:constructor',
                \ 'f:functions'
        \ ],
        \ 'sro' : '.',
        \ 'kind2scope' : {
                \ 't' : 'ctype',
                \ 'n' : 'ntype'
        \ },
        \ 'scope2kind' : {
                \ 'ctype' : 't',
                \ 'ntype' : 'n'
        \ },
        \ 'ctagsbin'  : 'gotags',
        \ 'ctagsargs' : '-sort -silent'
\ }

let g:go_highlight_structs = 1
let g:go_highlight_methods = 1
let g:go_highlight_functions = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1
syntax on
  • 安装gotags - 例如:brew install gotags
  • 生成ctags - 例如:gorags -R . > ./tags
  • 从新的终端打开vim
英文:

This is what worked for me in MAC

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

let g:tagbar_type_go = {
\ &#39;ctagstype&#39; : &#39;go&#39;,
\ &#39;kinds&#39;     : [
\ &#39;p:package&#39;,
\ &#39;i:imports:1&#39;,
\ &#39;c:constants&#39;,
\ &#39;v:variables&#39;,
\ &#39;t:types&#39;,
\ &#39;n:interfaces&#39;,
\ &#39;w:fields&#39;,
\ &#39;e:embedded&#39;,
\ &#39;m:methods&#39;,
\ &#39;r:constructor&#39;,
\ &#39;f:functions&#39;
\ ],
\ &#39;sro&#39; : &#39;.&#39;,
\ &#39;kind2scope&#39; : {
\ &#39;t&#39; : &#39;ctype&#39;,
\ &#39;n&#39; : &#39;ntype&#39;
\ },
\ &#39;scope2kind&#39; : {
\ &#39;ctype&#39; : &#39;t&#39;,
\ &#39;ntype&#39; : &#39;n&#39;
\ },
\ &#39;ctagsbin&#39;  : &#39;gotags&#39;,
\ &#39;ctagsargs&#39; : &#39;-sort -silent&#39;
\ }
let g:go_highlight_structs = 1
let g:go_highlight_methods = 1
let g:go_highlight_functions = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1
syntax on
  • Install gotags - e.g. brew install gotags
  • Generate ctags - e.g. gorags -R . > ./tags
  • Open vim from the new shell

答案11

得分: -1

~/.vim/syntax/go.vim应该与~/.vim/ftdetect/go.vim具有相同的内容

只有~/.vim/ftdetect/go.vim必须附加au BufRead,BufNewFile *.go set filetype=go

如果按照字面意思来理解,这些指示告诉你创建一个只包含以下内容的文件~/.vim/ftdetect/go.vim

         au BufRead,BufNewFile *.go set filetype=go

我想这就是需要上下文知识的地方。只是我以前从未这样做过,也没有这样的上下文。谢谢大家!

英文:

Turns out the directions above were slightly ambiguous.

~/.vim/syntax/go.vim should have the same contents as ~/.vim/ftdetect/go.vim

only ~/.vim/ftdetect/go.vim must be appended with au BufRead,BufNewFile *.go set filetype=go.

If taken literally, the directions tell you to create a file ~/.vim/ftdetect/go.vim containing only

         au BufRead,BufNewFile *.go set filetype=go

I suppose that's where contextual knowledge should kick in. Only I'd never done this before and had no such context. Thanks all!

huangapple
  • 本文由 发表于 2013年3月23日 05:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/15579822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定