Configuring a perfect editor for frontend development
Konstantin
Posted on October 21, 2019
I've seen a lot of tweets recently that basically state that vim is not suitable for frontend development. So I decided to do a small overview of my setup(I'm mostly a frontend developer although I do some backend and devops stuff as well).
My setup
You can find all configuration and instructions on how to install it in my github repo:
The main file is init.vim that is symlinked to .vimrc during the installation. All the configuration scripts are stored in after/plugin/*.vim. This way I can just add another script to that folder(with a name of a plugin for instance) and it will be automatically loaded during vim startup. It also helps me to keep things modular.
Spoiler
I should point out right away that perfect editor doesn't exist or at least it's different for everyone.
Why vim
I think there are many great editors and IDEs out there but there is one thing among others that stands out if you're using vim: you don't switch environment. So if you're in terminal and you've cloned a project, once you open it you're still in terminal. Even if you quit vim you're still in the same environment. For me, that's the most important thing about using vim(beside many other things, obviously).
With that out of the way let's take a look at some key features of my config.
Opening a project
In my mind any project is strongly linked with a repository(except monorepos of course), so when I open any file in a repository I want my editor to go to the root of the project(i.e. where the .git folder is located). There is a plugin for that:
A Vim colorscheme based on Github's syntax highlighting as of 2018.
But I also installed the base16(this is a fork, due to some problems with the original repo) themes and configured vim so that it looks into ~/.vimrc_background file and takes current base16 theme from there:
" set colorscheme" if you have base16 installed take that colorschemetryiffilereadable(expand("~/.vimrc_background"))let base16colorspace=256source~/.vimrc_background
letg:airline_theme='base16'elsecolorscheme github
" https://github.com/cormacrelf/vim-colors-github/pull/5hi! link SignColumn LineNr
letg:airline_theme="github"endifcatch/^Vim\%((\a\+)\)\=:E185/colorscheme default
endtry
This way vim stays consistent with terminal theme.
By default you won't have an autocompletion in javascript. To enforce typescript to enable autocompletion within javascript you have to add:
// @ts-check
At the top of the file. In my setup I have
a templates folder that contains predefined templates for new files(config can be found in templates.vim). So once I create new js file it already contains that comment.
Jumping between files
Coc already provides different shortcuts to jump to function definition and what not:
However vim provides a magic shortcut gf that allows you to jump to a file under cursor. The magic is that you can alter it's behavior per file type. For instance, in javascript we want to use the following algorithm to resolve the file:
Check file locally
If it doesn't exist check file in node_modules
If it exists and it is a folder check the package.json for main field. If it is present open it.
Here I highlighted key features of my configuration. I encourage you to go check the full list of extensions that I use in the init.vim file and the configuration scripts in the after/plugin/ folder. I listed all the file types and commands for every extension explicitly so that there is nothing that fires up randomly.