Vim is one of the most powerful and popular text editors for developers and sys admins. It is highly configurable with extensive features and key-bindings that make the development a breeze.
In this tutorial, we will learn how to configure the Vim text editor for Go development including all the plugins and other QOL improvements.
Install Vim
If you do not have Vim installed on your system, you can do so using your system’s package manager. For example, on Debian, run the “apt” command as follows:
You can use Homebrew on macOS:
Basic Vim Configuration
Before we dive into enabling and adding external plugins and features, let us configure the basics of the editor.
In Vim, we need to create a file called “.vimrc” file in the home directory. This is where we add the configuration for the editor.
Edit the file and add the following configurations:
Use the following command to enable the line numbers in the editor:
Use the following command to enable the syntax highlighting:
You can also enable the line wrapping using config.
If you prefer to use four spaces for tabs, add the “tabstop” and shift the “width” configs as follows:
set shiftwidth=4
set expandtab
To enable the search highlighting as we type, use the following command:
Next, enable the case-insensitive searching using the following commands:
set smartcase
We can also enable the editor to show the line and column number in status line.
If you prefer to interact with the mouse in the editor, you can add the mouse support using config:
Lastly, we can enable the clipboard support. This requires Vim with +clipboard.
Once we have the desired configuration enabled, we can proceed and setup the external packages and plugins.
Install and Manage the Plugins
To make the process of installing and configuring the plugins in Go, we can use the vim-plug which is a powerful and easy-to-use plugin manager for Go.
To install it, add the following to your “.vimrc”:
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
" Initialize Vim-Plug
call plug#begin('~/.vim/plugged')
Once installed, we can add the new plugins by adding the following entry in the “.vimrc” file:
Install Vim-Go
Vim-Go is a plugin that adds the Go language support for Vim with extensive features and support such as quick compile and execution of binaries, improved syntax highlighting, and more.
We can then add the vim-go plugin as follows:
Save the changes to the “.vimrc” file.
Conclusion
In this tutorial, we learned how to quickly configure the Vim text editor for Go development. Keep in mind that there is more you can add such as key bindings, custom commands, and more.