ESLint / Vim (or neovim) workflow - vim

I have used ESLint with Vim and various other plugins like Ale and Prettier. I am now having a lot of difficulty setting things up again. It would be useful for me to know what the ESLint plugin does without any other plugins at all or any other configuration of Vim or neovim.
I am finding the documentation very difficult to follow. I think this is partly because ESLint works in many contexts not just Vim` and also much of what is written assumes other plugins are installed e.g. Ale.
I am guessing that if you are editing a Vim file (perhaps only if it has .js extension?) you can then go into normal mode and invoke ESLint (e.g. :eslint) and then see any errors in the code indicated.

The basics
For the sake of simplification, ESLint is best thought of as a standalone command-line linter, that is not related in any way to Vim or any other text editor/IDE.
The default usage pattern is to execute it in your shell with files as arguments:
$ eslint goodfile.js
(no output)
$ eslint badfile.js
(list of errors)
It can also be used as filter:
$ echo 'console.log(1)' | eslint --stdin
(list of errors)
It can also be told to fix what it can instead of merely reporting it:
$ eslint --fix badfile.js
In Vim
Like lots of command-line programs (linters or not), ESLint can be used from Vim in lots of ways.
The simplest is to execute it directly on the current file:
:!eslint %
(list of errors)
or on the current buffer if it is not written to disk:
:w !eslint --stdin
(list of errors)
But that method, while informative, is not very useful because the errors disappear as soon as you press <CR>.
A much better approach is to use the :make command, which populates the quickfix list with the errors reported by the underlying linter or compiler or whatever. In general, it works like this:
Tell Vim to use program foo for :make with set makeprg=foo.
Tell Vim how to parse the output of foo with set errorformat+=<pattern>.
Do :make % to run the current file through foo.
Do :cwindow to show the list of errors reported by foo, if any.
In the case of ESLint, if your Vim is recent enough, it should come with what it calls a "compiler script" (that I wrote), which basically does steps #1 and #2 above for you. You can check if you have it with:
:compiler eslint
If you get an error, consider this as an opportunity to upgrade.
After you have selected a compiler, you can perform steps #3 and #4 above to lint your file, which is, let's be honest, tedious.
From there, you can choose one of three paths:
keep doing it like that because it's fine,
learn a little more of your editor in order to automatise the whole process,
give up on the tinkering and use a third-party plugin.
Path #2 looks like this. It is not particularly hard or complicated but I wouldn't recommend to a copy/pasting newbie.
Path #3 involves installing a plugin like ALE, which is throughly documented and has a dedicated issue tracker.

Related

Jump to make errors in source files with vim similar to a mouse click in VS Code terminal

I'm trying to make the jump from VS Code to pure terminal+vim development and I'm missing one efficiency booster from VS Code I used often. When I make some project, and errors are present, the stdout shows the path to the source file with the error, along with the line:column number. In the VS Code terminal I could just left-click with a mouse on that and it would open the VS Code text editor (with the VIM emulator enabled of course), to that location. Is there any similar time saver in the terminal and/or vim? I.e. something that would enable me to go directly from make error to editing that location in vim rather than reading the error log, memorizing the path:line:column and opening that file manually?
For example, lets say I had the following line within the make output:
/home/myusername/someProject/src/foo.c:30:5: warning: implicit declaration of function 'bar'; did you mean 'barr'? [-Wimplicit-function-declaration]
30 | bar(26);
| ^~~~~~~~~~~~~
| barr
Is there any easy way to jump to /home/myusername/someProject/src/foo.c:30:5 without highlighting it with a mouse and copy/pasting it?
The closest thing I found on SO so far was this but I'm guessing the make output will catch errors that only occur after attempting to compile, whereas something in live time like Syntastic is only going to catch syntax errors. I haven't used Syntastic yet, but I'm guessing what they catch has some union of items, but it obviously would be to costly to continuously be compiling in the background to check for compile errors.
I also note the errors themselves, i.e. -Wimplicit-function-declaration are hyperlinks. Maybe there is some setting in make/cmake to generate links for the src files as well?
The user manual, a mandatory read if you are serious about using Vim, dedicates a whole section to compiling: :help 30.1.
In a nutshell…
You choose what Vim calls a "compiler" with the :help :compiler command:
:compiler gcc
It tells Vim:
what external command to execute for compiling your code,
how to interpret the eventual error output.
You execute the :help :make command:
:make %
It tells Vim:
to execute the external command defined above,
and to capture any output in case there are errors to report.
You use the :help quickfix feature to display, navigate, and interact with eventual errors:
:cwindow " opens the quickfix window if there are valid errors
:cn " jump to next error
etc.
FWIW, Vim has very strong roots in C and it shows all over the place. Since running make is the default behavior of the :make command and the default :help 'errorformat' already handles most common C compilers, doing:
:make
followed by:
:cw
in your current project should give you a taste of what to expect:
Real life considerations…
Setting the compiler manually for each file you edit is a PITA. It makes a lot more sense to spend a few minutes putting together an autocommand.
Vim comes with a large and growing collection of "compilers". Check out $VIMRUNTIME/compilers.
Vim provides the basics of the "edit, compile, run" cycle but the experience may not be polished enough out of the box. For example, you might want to open the quickfix list automatically when there are errors, etc. There are basically two ways to address this: using some third-party plugins and/or writing your stuff yourself. I tend to like the latter the most (see this and this for example) but the third-party plugin path is valid, too.

Is it possible to reload compiler plugins in Vim?

I am creating a small compiler plugin in Vim and I'm finding very slow to debug it.
It seems like there are ways to debug the errorformat but some times I just want to force Vim to reload my compiler plugin that I created in ~/.vim/after/compiler/
It doesn't look like I can just source the file as it fails because of CompilerSet. Is there any other way of reloading a compiler plugin?
Simply running :compiler {name} should reload your full compiler script.
See :help :compiler with the details of how a compiler plug-in is loaded.
An alternative is to source the compiler plug-in file directly. In order to handle the :CompilerSet command, you can either define the user command yourself before you run the plug-in file, or use a snippet in your plug-in file itself to define it if not defined already.
Many of the compiler plug-ins shipped with Vim include this snippet at the top:
if exists(":CompilerSet") != 2 " older Vim always used :setlocal
command -nargs=* CompilerSet setlocal <args>
endif
So apparently that's all you need...
But using :compiler {name} each time to fully source it should be a better approach, assuming that's a possibility to you.
(Tangentially) I find tpope’s scriptease plugin to have many benefits when working on a vim plugin (and also when writing answers to vim questions where I have to dig deep into runtime files).
Highlights:
:PP is both a pretty-printer and a repl
:Runtime helps reload files
:Breakadd is much smarter
:Vedit and cousins open up files on the runtime path

Can you view the default Vim settings?

I’m starting to learn about creating my own .vimrc file and I keep noticing features that are missing with my custom version that were present in the default setup.
I was wondering if it is possible to view the default settings, so I can compare them to my own and look up all the standard inclusions I don't know to learn more about them.
I searched and couldn’t find an answer online, and if the reason why there isn’t one is that the answer to this question is glaringly obvious, I’m really sorry; I’m still a bit of a noob :p
No worries, it’s a perfectly valid question. Unfortunately, the answer is a bit complicated. First of all, Vim has certain defaults that are documented in the built-in help system.
Some of them are only used when Vi compatibility mode is disabled, and that’s the first customisation most people make:
:set nocompatible
On top of that, many distributions provide their own custom config, for example Debian/Ubuntu comes with /etc/vim/vimrc. To makes things even more confusing, Vim 8 comes with a sane configuration (called default.vim) that only gets applied when ~/.vimrc is not found. Not to mention that NeoVim comes with its own set of defaults.
In practice, I suggest to explicitly set any options you care about to make sure your config is portable between systems and versions of Vim. To see the current value of a given option, use a question mark:
:set showcmd?
To learn more about a given option (including the default value), use Vim’s comprehensive help system:
:help showcmd
Finally, you might want to check my annotated .vimrc for some inspiration, and there is also the vim-sensible plugin that provides some sane defaults most people would agree to.
The easiest way to see “vanilla” Vim options is to start it using:
$ vim -u NONE -N
It will start Vim without any of your customizations or plugins, but still in ‘nocompatible’ mode (i.e., basically, running full-fledged Vim, instead of its stripped down version emulating Vi).
Then, you can execute the following commands:
:set all
:map
:command
:let
:function
:autocmd
to see all options, mappings, commands, variables, functions, and auto-commands, respectively, that are currently in effect. (I cannot promise I haven’t forgotten a customization category.)
Vim also comes with a bunch of basic configurations that is skipped by the -u NONE option, that you can also include while still excluding your .vimrc, by using -u NORC, instead.
Based on #Amadan's answer, I came up with this file (ShowAllDefaults.vim) and command to run it and capture the output.
. In the mean time, learning that, if you have files under ~/.vim, they get executed if you use this:
vim -u ShowAllDefaults.vim -N +q
So the correct way to do it is:
vim -u NONE -N +"source ShowAllDefaults.vim" +q
Contents of ShowAllDefaults.vim:
set verbosefile=/tmp/ShowAllDefaults.log
set all
map
command
let
function
autocmd
I am trying after long time to get familiar with vim also, and I came across this because I had same question.
How I found answer from within vim was to pull up help on defaults and it explained to get defaults along with .vimrc for newer users and also gave the path to default script so you could open it right up in your editor and read & compare it.
I am not going to give my exact path because that might change in different versions, so best to get it from help documents inside vim.

Vim: Sourcecode output at startup

Today I installed vim on my new computer and installed a couple of plugins.
When I start vim (with no arguments and with files to edit) I get a lot of verbose output in my console which I never encountered before.
This seems to be something from the vim sourcecode or from a plugin but I couldn't encounter where it comes from and especially why.
I'm interested in tracking down this issue. How can I do that?
I tried searching for some of these lines in my plugin folder but got no results and starting with the -D flag also gave me no hint. Google and SO search also yielded no results for me.
Additionally I tried to disable each plugin seperately but this also failed.
Thanks.
PS: I would like to provide a picture but since the output is more than 150 lines long this is not a good idea since it doesn't seem to be related with a plugin and I don't have enough reputation.
Maybe a very small part of it:
syntaxset FileType
*exe "set syntax=" . expand("<amatch>")
filetypedetect StdinReadPost
...
svn-commit*.tmp
setf svn
Xresources*
call s:StarSetf('xdefaults')
*/app-defaults/*
Remark: This is not representative since it is randomly chosen from the terminal output, I just wanted to give an impression what is going on.
EDIT: This is NOT an error, its just printing out some kind of source code. Everything is working fine otherwise.
SOLUTION: Removing the autocmd line without any further text did the job. Thanks to FDinoff. Debugging with finish is really pleasant.
Some Vim commands, when argument(s) are left off, print all existing such artifacts. This is useful during interactive use, but if you forget to specify the argument in your (plugin / ~/.vimrc) configuration, this has the effect of "printing cryptic code" (an error would be more useful here).
Your output is likely from an :autocmd without arguments. Check your ~/.vimrc and other files read during startup (see :scriptnames).

How to make Vim Quickfix show errors in source files instead of the make file

So I am programming in C with VIM by using the make command to compile my code.
I would like to use the quickfix plugin to quickly move to the different compile errors but the only error that quickfix shows is the failed command in the make file.
I have failed to find any clues on google for this problem, is there something I could be missing to make this work?
The quickfix list (it's built in, not a plugin) parses the output of :make according to the rules in the 'errorformat' option, in order to extract file names, line numbers, and error messages.
Usually, you don't write those yourself, but you simply choose the appropriate compiler plugin. If your build uses GCC, you can set the compiler by
:compiler gcc
See :help compiler for details and a list of compiler plugins that ship with Vim.
I have finally solved the problem.
It turns out that if your shell is set to use FISH the output of the make command displays correctly but for some reason quickfix is unable to pick up any errors except for the failed part in the make file.
I solved the problem by switching vim to use bash instead by adding set shell=/usr/bin/bash to my .vimrc file.

Resources