Home > Article > Backend Development > vim golang automatic jump
When using Vim to edit Go code, we often encounter situations where we need to jump to a function or variable definition. In Vim, we can use some plug-ins or tricks to achieve this. This article will introduce one of the methods - using gutentags and vim-go to achieve automatic jump.
1. Install gutentags
gutentags is a Vim plug-in used to generate tags files, which can help us quickly browse the code and find variable and function definitions in the file. If you have not installed gutentags, you can use the following command to install it:
//Vundle Plugin 'ludovicchabant/vim-gutentags' //NeoBundle NeoBundle 'ludovicchabant/vim-gutentags' //vim-plug Plug 'ludovicchabant/vim-gutentags'
After the installation is complete, execute ":help gutentags" in Vim to view the instructions for using gutentags.
2. Install vim-go
vim-go is a Vim plug-in designed for Go language developers, which can help us quickly write, debug and test Go language programs. If you have not installed vim-go, you can use the following command to install it:
//Vundle Plugin 'fatih/vim-go' //NeoBundle NeoBundle 'fatih/vim-go' //vim-plug Plug 'fatih/vim-go'
After the installation is complete, execute ":help go" in Vim to view the instructions for using vim-go.
3. Configure gutentags and vim-go
After installing gutentags and vim-go, we need to configure them so that they can work together and realize the automatic jump function.
Add the following configuration in the .vimrc file:
let g:gutentags_project_root = ['.git', '.svn', '.hg'] let g:gutentags_cache_dir = '~/.vim/tags' let g:gutentags_file_list_command = 'git ls-files' let g:gutentags_generate_on_write = 1
The meanings of these configuration items are:
Add the following configuration in the .vimrc file:
let g:go_def_mode='gopls' let g:go_auto_sameids = 1 let g:go_list_type = "quickfix" let g:go_list_autowin = 1 let g:go_fmt_command = "goimports" let g:go_complete_unimported = 1 let g:go_def_mapping_enabled = 0
The meanings of these configuration items are:
4. Implement automatic jump
After configuring gutentags and vim-go, we can open any Go file in Vim and move the cursor to a function or on the variable, and then press the "gd" shortcut key to automatically jump to the definition position.
During the automatic jump process, gutentags will automatically generate the tags file. If the file already exists, it will be used directly. When jumping to different function or variable definitions in the same file, vim-go will automatically update the tags file to ensure that the next jump can accurately locate the definition location. Using the above method can greatly improve our writing efficiency and quickly browse and locate variable and function definitions in the code.
The above is the detailed content of vim golang automatic jump. For more information, please follow other related articles on the PHP Chinese website!