Home  >  Article  >  Backend Development  >  vim golang automatic jump

vim golang automatic jump

王林
王林Original
2023-05-14 16:31:07995browse

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.

  1. Configuring gutentags

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:

  • g:gutentags_project_root: Specify the project root directory. Searching for tags files will be performed in the project root directory and its subdirectories.
  • g:gutentags_cache_dir: Specify the saving path of the tags file. gutentags will cache the generated tags file, so you don’t need to generate the tags file again next time you search for the same file or variable.
  • g:gutentags_file_list_command: Specify how to list the files in the current project. Here we used the git ls-files command to get the file list, but other commands can also be used.
  • g:gutentags_generate_on_write: Specify whether to automatically generate tags files when saving files.
  1. Configure vim-go

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:

  • g:go_def_mode: Specifies the use of gopls as the backend for definition jumps. This is a language server officially provided by the Go language.
  • g:go_auto_sameids: Specify whether to automatically update the tags file when jumping to different function or variable definitions in the same file.
  • g:go_list_type: Specify using the quickfix window to display related information.
  • g:go_list_autowin: Specify whether to automatically jump to the first error item when opening the quickfix window.
  • g:go_fmt_command: Specify the use of goimports to format code.
  • g:go_complete_unimported: Specifies whether to include unimported packages in completion.
  • g:go_def_mapping_enabled: Specifies whether to enable shortcut key mapping for GoDef commands.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn