03 May 2019

How to Add Top Features From Other Text Editors to Vim


add-features-vim

If you’re like many people, you know Vim as that editor you open to tweak a config file then can’t manage to exit. On the other hand, if you frequently use Vim, you know how powerful its modal editing features are. If you run Linux or any other Unix flavor, Vim is worth learning.

That said, Vim shows its age pretty easily. By default, it lacks many of the features we’ve come to rely on in modern text editors. That said, install a few packages and Vim can hold its own with Visual Studio Code, Sublime Text, and more.

Plugin Management: Vim-Plug

Installing plugins in Vim-Plug

One key feature in modern text editors is the ability to extend them with plugins. While Vim added native package management in version 8.0, many find it cumbersome compared to third-party package managers. One of the most popular package managers is Vim-Plug.

Before you can start using Vim-Plug, you’ll need to install it. On a Unix system like Linux or macOS, run the following in a terminal to download and install vim-plug.

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
 https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

If you’re using Vim in Windows, you can install Vim-Plug by pasting the following into PowerShell.

md ~\vimfiles\autoload
$uri = 'https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
(New-Object Net.WebClient).DownloadFile(
 $uri,
 $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(
 "~\vimfiles\autoload\plug.vim"
 )
)

Now, you’ll be able to install plugins by adding them to your ~/.vimrc file. You’ll need to add two new lines to the file:

call plug#begin('~/.vim/plugged')
call plug#end()

To install a plugin, add Plug, followed by the part of its GitHub URL that follows http://www.github.com in single quotes. For example, to install the Solarized color scheme, your config file would contain the following:

call plug#begin('~/.vim/plugged')

Plug 'altercation/vim-colors-solarized'

call plug#end()

For more information on how to install the package manager, see the Vim-Plug GitHub page.

Error Checking: Syntastic

Syntastic for Vim

Another feature many have come to rely on is your editor of choice telling you when the code you’ve written is invalid. This is often known as “linting.” It won’t keep you from writing code that won’t run, but it will catch basic syntax errors you may not have noticed.

As the name hints at, Syntastic is a syntax checking plugin for Vim. It doesn’t actually do much by itself for many languages. Instead, you’ll need to install a linter or syntax checker for the language or languages of your choice. Syntastic will then integrate the checker into Vim, checking your code every time you save the file.

Syntastic supports more languages than we can list here, so it’s highly likely that the language you’re using is supported. For instructions on how to configure the plugin, see the Syntastic GitHub page.

Code Completion: YouCompleteMe

Autocompletion in YouCompleteMe

Syntax checking is nice, but if you come from Visual Studio Code or a similarly feature-packed editor you’re probably missing something else. This is code competition, also known as Intellisense in the Visual Studio world. If you’re using vim for more than editing config files, it will make your life a lot easier.

Code completion makes writing code easier by popping up suggestions as you type. This is nice if you’re using a method that is heavily nested, so you don’t have to remember the entire string.

YouCompleteMe is a code completion engine for Vim, and it’s one of the more powerful plugins you can install. It’s also somewhat trickier to install than other plugins. You can install the basics with a package manager like Vim-Plug, but you’ll need to compile it.

The easiest way to compile the plugin is to use the included install.py script. To do this on macOS or Linux, enter the following:

cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer

Note that on Linux you’ll have to install development tools, CMake, and the required headers before you can compile YouCompleteMe.

For instructions on installing and compiling YouCompleteMe on other systems or for more information, see the YouCompleteMe GitHub page.

Fuzzy Search: CtrlP

CtrlP for Vim

If you’re working on a project with many different files, Vim’s method of opening files might frustrate you. The :e command has basic autocomplete, but you’ll still need to know where your file is located. You could drop to the command line to find it, but wouldn’t it be better if you could do this right from Vim?

Fortunately, you can. The CtrlP plugin can search files, but it can also do much more. The CtrlP GitHub page describes it as a “full path fuzzy file, buffer, mru, tag, … finder for Vim.” The plugin is similar to Sublime Text’s “Goto Anything” command which, surprise surprise, has the keyboard shortcut of Ctrl + P or Command + P.

This feature or an equivalent can be found in most modern text editors, and if you find yourself missing it, it’s nice to have in Vim.

File Browsing: NERDTree

NERDTree running in Vim

You might prefer a more traditional type of file browsing. If you miss the left-hand panel display of files found in many editors, you’ll be glad to know it’s available in Vim. This is thanks to the NERDTree plugin.

Unlike the left menu in Sublime Text, Visual Studio Code, and others, NERDTree is a full file system explorer. Instead of displaying just your project directory, you can navigate anywhere on your computer. If you’re working with files across multiple projects, this can be a very handy feature to have.

To open NERDTree inside Vim, just use the :NERDTree command. If you’d rather bind it to a command, you can do this with a ~/.vimrc option like the following:

map <C-n> :NERDTreeToggle<CR>

This would let you simply hit Ctrl + N to open and close the NERDTree panel.

Git Integration: fugitive.vim

Add Fugitive to Vim

Git integration has become a must-have feature in modern text editors, so it’s good to know that it’s available in Vim too. The project GitHub page describes fugitive.vim as “a Git wrapper so awesome, it should be illegal.”

Running :GStatus will bring up something similar to what you’d see with the git status command. If you’ve finished your work on a file and are ready to commit it, run :GCommit %. This will let you edit the commit message inside the currently running Vim window.

There are too many commands to list here, plus you can run any standard Git command by running :Git. For more information, including screencasts, see the fugitive.vim GitHub page.

Are You Looking for Even More Vim Tips?

The above tips will help to modernize Vim, but they’re far from the only thing you can do to customize the editor to your liking. As you may have already guessed from reading this article, Vim is an extremely tweak-able editor.

If you’re ready for more, take a look at our list of Vim customizations to make it even better.

Read the full article: How to Add Top Features From Other Text Editors to Vim


Read Full Article

No comments:

Post a Comment