Add GoLang syntax highlighting for VIM - 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
(Vim 7.3 on Ubuntu 12.10 doesn't 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:
~/.vim
~/.vim/syntax
~/.vim/ftdetect
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?

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..>>

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

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.

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

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)

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/

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 'jnwhiteh/vim-golang'
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.

It should be easy as 1, 2, 3 :
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).
If you don't already have the file ~/.vim/ftdetect/go.vim, create it (and the folder if necessary).
In .vim/ftdetect/go.vim, add the following line : au BufRead,BufNewFile *.go set filetype=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/...

This is what worked for me in MAC
Install vim-go - https://github.com/fatih/vim-go
Set the .vimrc to the following
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
Install gotags - e.g. brew install gotags
Generate ctags - e.g. gorags -R . > ./tags
Open vim from the new shell

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!

Related

How to fix duplicate cscope ? is it a better way?

It is several years I am programming with vim and I used ctags.
I am working with a reasonably large C/C++ package and I need to find definition of functions. I usually use grep + ctags.
Recently I tried to use cscope instead of ctags and installed it with Vundle.
I see the following error for some of my files
E568: duplicate cscope database not added
I searched the web and found this:
https://blogs.oracle.com/natarajan/entry/avoiding_duplicate_cscope_database_error
It doesn't work.
How can I fix this?
Expanding on Artem's answer:
The Vim help for cscopeverbose is as follows:
If 'cscopeverbose' is not set (the default), messages will not be printed
indicating success or failure when adding a cscope database. Ideally, you
should reset this option in your .vimrc before adding any cscope databases,
and after adding them, set it. From then on, when you add more databases
within Vim, you will get a (hopefully) useful message should the database fail
to be added.
The problem here is that (a) there are multiple scripts attempting to load the cscope.out file and (b) they're not following the best practices of disabling the "verbose" cscope warnings before loading the file then re-enabling it afterwards, as suggested by the help text above.
The full error output should tell you which script is triggering this warning; for me it looked like this:
Error detected while processing /home/me_and/.vim/plugin/cscope_maps.vim:
line 42:
E568: duplicate cscope database not added
The fix was then to edit the ~/.vim/plugin/cscope_maps.vim file to add set nocscopeverbose immediately before the cs add ... lines. My version of this file already had set cscopeverbose immediately after, but if yours doesn't you should add that too.
Found the solution which worked for me (here: http://thoughtsolo.blogspot.com/2014/02/cscope-issue-duplicate-cscope-database.html):
Just add this line "set nocscopeverbose " to your ~/.vimrc file.
As per the blog, "This error pops up when VIM is already compiled with 'CSCOPE' module and you have also installed "cscopemenu.vim"". I assume that you have a vim executable with has been configured with --enable-cscope option.
Here's what I do:
Download cscope source and build it, install the executable in a directory which is available in your PATH
Download vim source code and configure it with --enable-cscope, build the source and install the executable
Download cscope_maps.vim and place it under $HOME/.vim/plugin directory. This contains cscope settings for vim.
Create cscope database out of the source and header files. You may do something like the following
find $PROJECT_HOME -name *.c -o -name "*.cpp" -o -name "*.cc" -o -name "*.h" -o -name "*.hpp" > cscope.files
cscope -qbR -i cscope.files
You can add these commands in an alias and excute the alias every time you want to update your cscope database. These two commands create finally create cscope.out database file.
Update .vimrc file to have the following
if has("cscope")
set csprg=<location to cscope executable>
set csto=0
cs add <location to cscope.out>
endif
I hope after doing these steps you should be able to use cscope with vim easily.
Note that if you are working on multiple projects, you should be able to add appropriate environment variables to enable vim to pick the correct cscope database.
To answer your second question, may I suggest using tagbar. This will list your function names in the current source or header file. You can install it using Vundle. Add the following line to your .vimrc
Plugin 'majutsushi/tagbar'
Add this to your .vimrc to toggle tagbar view
nmap <F4> :TagbarToggle<CR>
Note that F4 is just an example and you may use any binding to do the same.

Installing VIM plugins to a central location and making them globally available to all users

I've installed syntastic VIM plugin by following instructions from the plugin docs via pathogen.
Install pathogen:
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
Install syntastic as pathogen bundle:
cd ~/.vim/bundle &&
git clone https://github.com/scrooloose/syntastic.git
At this point it's in my $HOME/.vim directory of my Linux user.
I'd like to install this plugin to be globally available for all users on the box, but I'm having trouble finding out how to go about doing it. All the instructions seem to talk about installing plugins under $HOME/.vim.
Recommended
Before you start, update your vim to the last version. If using debian, install vim.nox (python support) and run update-alternatives for vim, vi and vimdiff.
How-to
Using vim-plug I've managed to create a global plugin installation.
This is a step-by-step explanation;
create /etc/vim/autoload. Make sure others can read/execute the directory.
add plug.vim file in it. Make sure all users can read it. See below: Download
add to (preferred the begin of) your /etc/vim/vimrc.local
set runtimepath+=/etc/vim/autoload
" Initialize plugin system
call plug#begin('/etc/vim/plugged')
" Vimtemplates - templates for diverse files
Plug 'drbeco/vimtemplates', { 'do': '/etc/vim/plugged/vppinstall.sh' }
" VimColors8 - colorschemes for all
Plug 'drbeco/vimcolors8', { 'do': '/etc/vim/plugged/vppinstall.sh' }
call plug#end()
" End of initialization of plugin system
The two plugins (repositories) above, namely drbeco/vimtemplates and drbeco/vimcolors8 are optional and are there just to test the installation. You need some plugin there to run :PlugInstall and this two are small, easy and compatible. Feel free to change, but I recommend you first install all, check if it is ok, then change all vim-plug session to your taste.
Create a directory /etc/vim/plugged/. Make sure all users can read/execute it.
Add the following script to the plugged directory (make it executable):
$ cat /etc/vim/plugged/vppinstall.sh
#!/bin/bash
# notice
echo "vppinstall.sh (C) 2017 Dr. Beco: Correcting plugin's permissions"
# work in plugged directory
cd /etc/vim/plugged
# execute (open) and read directories
find . -type d ! -wholename "*/.git*" -exec chmod o+rx {} \;
# read all files
find . -type f ! -wholename "*/.git*" -exec chmod o+r {} \;
This script will run as post-installation hook to correct the permissions of the files. You can set stick-bits or redefine your UMASK instead of running this script, but unless you know the security risks I don't recommend.
Almost done.
Reload .vimrc and run :PlugInstall to install plugins.
For each new plugin you add to your vimrc.local, make sure it calls the post-installation script, or else users won't be able to use them.
--
Download
To download the plug.vim file, use:
curl -fLo /etc/vim/autoload/plug.vim https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Taglist: Failed to generate tags for macvim

Whenever I am trying to open a file in my Rails project using macVim. I am getting an error
Taglist: Failed to generate tags for .......
But it works perfectly in terminal vim. Why is this happening? I am a beginner and just installed everything using this dotvim repo.
I installed ctags using these commands that I got from this Gist:
$ ctags -R --exclude=.git --exclude=log *
ctags: illegal option -- R
usage: ctags [-BFadtuwvx] [-f tagsfile] file ...
#you need to get new ctags, i recommend homebrew but anything will work
$ brew install ctags
#alias ctags if you used homebrew
$ alias ctags="`brew --prefix`/bin/ctags"
#try again!
ctags -R --exclude=.git --exclude=log *
which ctags on terminal returning, same if I do from vim or gvim using ! (bang):
/usr/bin/ctags
You need to change the PATH order to make /usr/local/bin/ctags ahead of of /usr/bin/ctags. The way I prefer to achieve this is by add /usr/local/bin to the beginning of /etc/paths:
# for homebrew
/usr/local/bin
# original order
/usr/bin
/bin
/usr/sbin
/sbin
#/usr/local/bin
Or you can just set a variable like
let g:Tlist_Ctags_Cmd='/usr/local/bin/ctags' " Proper ctags location
in your .vimrc file.

Install multiple version of Vim, and make each use different .vimrc file, respectively

Make it on Linux. The reason to use more than one version of Vim, is because one version would be heavily hacked, for Lisp jobs. I want separate it and make it use it's own .vimrc file as well.
/usr/bin/vim use -> ~/.vimrc
/my/vim use -> ..../another_vimrc
Command line option
You can give the -u parameter to your command line. This parameter will force the vim to read the specific vimrc without reading the system wide configurations:
/my/vim -u /path/another_vimrc
You can even create a command alias, with which you can start this custom vim. Put this in your .bash_profile for e.g.:
alias customvim /my/vim -u /path/another_vimrc
And then start this custom vim with:
customvim
Building configuration
You can specify the prefix option to the configuration script of when you're building from source. If you set this, vim will look for configuration file in the prefixed directory.
For e.g. if you do with stow:
./configure --prefix=/usr/local/stow/vim-7.3/ && make install
Then the vim will be installed in /usr/local/stow/vim-7.3/ and the custom configuration should be in /usr/local/stow/vim-7.3/etc/vimrc
You can use the Predefined Vim variables(v:version).
Suppose you have installed both vim6 and vim7, you can create two .vimrc_X files:
~/.vimrc_6
~/.vimrc_7
Then you create another .vimrc file:
~/.vimrc
which contains:
if v:version >=700
source ~/.vimrc_7
elseif v:version >=600
source ~/.vimrc_6
endif
Take a look at Vim filetype plugin (search for ftplugin), it allows you to specify a configuration for given filetype.

How to get ctags working inside vim

I'm new to vim and wanted to get ctags integration working so I can more easily navigate a large java project.
I've pulled down the zip from source forge and extracted it but from here I'm not sure how to get it working with vim
Any help for a novice vim user would be great!
As nobody has given one critical function in these answers, I'll provide one more slightly superior answer.
The easiest way to use ctags with vim is by calling:
ctags -R *
from the root of your source repository. This will generate a tags file in that same directory.
In your ~/.vimrc file, add this short block:
" ctags optimization
set autochdir
set tags=tags;
" denotes a comment. set autochdir tells vim that if it doesn't find a tags file in the $PWD it will look in the directory parent for the tags file, recursively. set tags=tags; tells vim that the name of your tags file will always be the same as the default tags file generated by ctags.
So long as you run ctags -R * in your root source directory the first time and occasionally to update it (if you pull new changes from others) then you'll always have a fast and intuitive ctags symbol lookup in vim.
Using exuberant ctags, I use something like this in my project's base directory (excluding the "log" directory):
ctags -R --exclude=log *
You have to run the ctags command with the source files as arguments. This will create a tags file containing all information. Then you can open a file with vim, and e.g. press Ctrl-] when on a line with a function to jump to the code of that function. If vi isn't started in the same directory as the tag file, you can set it with :set tags=<file>
This is what I'm doing:
ctags -n -f [OUTPUT] [SOURCE] to generate the tags (NOTE: the -n applies to me but may not be necessary for your usage)
exec "set tags=" . [OUTPUT] inside of .vimrc to let vim become of aware of the tags
EDIT: I'm using
Exuberant Ctags 5.5.2
VIM 6.1
Additional info:
See ctags usages here
Tips and tricks from SO
look at this article: vim-easytags. i haven't tried this, but it looks quite good. manually creating and updating tags was really annoying. hope this will help. :)

Resources