Turning Vim into a JavaScript IDE
Integrated development environment (IDE) is an important tool for software developers. In simple terms, IDE is a powerful text editor for creating programs.
Vim is a lightweight, fast, and free text editor. Vim is well known for its hotkeys that make coding faster and more comfortable. Developers created a lot of plugins for Vim.
So we’re going to build a fast, flexible, lightweight IDE using Vim with plugins. Let’s define the necessary functions of IDE:
- Syntax highlighting
- Snippets
- Lint (analyzes source code to flag programming errors, bugs)
- Code formatting tool
- Autocomplete
The commands below are intended for Linux. I use Ubuntu 16.04.
Install Vim
We are going to use modern Vim plugins which require Vim 8 (latest version). Install Vim 8 using:
Install Vundle
Vundle is a plugin manager for Vim (as npm for JavaScript). Install Vundle using git clone:
Put this at the top of your ~/.vimrc
to use Vundle:
Launch Vim and run :PluginInstall
Install the syntax highlighting plugin
A syntax highlighting assists programmers when reading and analyzing code. We will install this plugin: https://github.com/pangloss/vim-javascript
We need add this line to .vimrc
into the plugins section:
Plugin 'pangloss/vim-javascript'
Then we need to install the plugin using vundle. Start Vim and type :PluginInstall
command. The plugin is installed:
Before:
After:
Now it looks better.
Snippets
Snippets make coding faster. You can quickly insert loops, conditions, functions and other language constructions by using hotkeys.
We need to these plugins: https://github.com/garbas/vim-snipmate https://github.com/grvcoelho/vim-javascript-snippets
Add to .vimrc
:
Also remap the hotkey to Ctrl+j
It works!
Lint
A lint checks a source code for bugs, stylistic errors. It’s an important tool for writing high-quality code.
ALE is a modern lint plugin for Vim. ALE checks your code in the real time mode (while you edit your code). Actually, ALE is a layer between Vim and a lint tool. We will use ESlint as a lint tool for a JS code.
Add to .vimrc
:
Run :PluginInstall
Configure ESLint:
The lint works but I would like to adjust messages style. I added these lines to .vimrc
:
Line numbers
Line numbers are disabled by default.
Add this to .vimrc
:
Code formatter
A code formatter (a code beautifier) converts your code into the correct format. You can quickly write some code and to format it with just one click. ESLint and ALE will do this job.
These options activate code formatting on saving a file:
Autocomplete
Autocomplete increases your typing speed. It predicts the word a user intends to enter. Install plugin dependencies:
Install the plugin:
YCM has a client-server architecture. The commands above installs the client. Here we install YCM server:
Cool, it works!
File explorer
NERDTree is a file system explorer https://github.com/scrooloose/nerdtree
I added this to my .vimrc
to open NERDTree
with Ctrl+n:
These line configure different highlighting for different file extensions:
System clipboard
System copy provides copying/pasting text to the system clipboard.
Install dependencies:
Add Plugin 'christoomey/vim-system-copy'
to .vimrc
, run :PluginInstall
.
Use cp
to copy into your system clipboard and cv
to paste from system clipboard.
My current .vimrc
You can find the latest version of my .vimrc in my repository: https://github.com/maxim-danilov/vim-config-maxim-danilov
Conclusion
We just made a good tool to write a JS code and edit any config files. It’s lightweight, fast, сustomizable and I can use it on any device via SSH.