165

I'm a pretty new Vim user and I've found that its learning curve is quite steep (at least for me). I just installed this vim script for JavaScriptLint error checking, which shows errors in vim's quickfix window once I save a buffer.

However, I don't know what to do next.. How do I 'scroll' through all the errors? How do I close the quickfix 'window'? How do I get it to check for errors after I've made changes to my code?

I've looked at the vim quickfix docs but the amount of commands are overwhelming and I can't seem to find what I want. Any help would be appreciated.

A side question: is there any way to have javascriptlint check for js errors for code residing in a .html file?

9 Answers 9

202

There are a lot of commands for quickfix as you have said, but I tend to find I only use a small subset of them:

:copen " Open the quickfix window
:ccl   " Close it
:cw    " Open it if there are "errors", close it otherwise (some people prefer this)
:cn    " Go to the next error in the window
:cp    " Go to the previous error in the window
:cnf   " Go to the first error in the next file
:.cc   " Go to error under cursor (if cursor is in quickfix window)

I tend to use this with :make and :vimgrep, so I can't comment on the Javascript lint checker, but this should give you something to get started.

Regarding the general use of JavascriptLint, I'm not a javascript programmer, but it looks like the script exposes a function called "JavascriptLint", so if you want to call it manually, you can use :call JavascriptLint(). However, it works on the disk copy of the file, so it'll have to be saved first. If (and only if) the command line jsl works on html files, you should be able to use :call JavascriptLint() on an html file to check the internal javascript. You could also do:

autocmd BufWritePost,FileWritePost *.html call JavascriptLint()

to automate it. If jsl doesn't support html files, then (short of patching the application or asking the author to change it), it's probably a lost cause...

4
  • Thanks, those commands are a good start, I'll just have to play around a bit and get used to how it works. If I use :cn to go to an error in the qf window, can I jump to the window with my code without exiting the qf window? Can it jump to the line that has an error? And your suggestion of using :call works, however it follows <script src> links which isn't what I want.
    – hora
    Nov 17, 2009 at 9:06
  • @hora: :cn shouldn't close the qf window, but it will switch to the window containing the error: is this what you mean by "jump"? You can quickly switch back with Ctrl-W p or :wincmd p, so you could do :command! CN :cn | wincmd p<CR> or something like that (untested). This should give you a :CN command that goes to the next error without switching out of the quickfix window. Alternatively you could do something like :au BufWinEnter quickfix nmap <buffer> <Enter> :.cc | wincmd p<CR> (again untested) and use Enter to select an error without switching out of the quickfix window.
    – DrAl
    Nov 17, 2009 at 9:45
  • 1
    For a beginner "You could also do: 'somecodehere' to automate it" does not mean much. It means that the shown line of code must be placed in ~/.vimrc (_VIMRC on windows AFAIK) and from the next start of VIM every time the file is saved this command will be invoked. Right?
    – Беров
    May 28, 2018 at 21:06
  • how to remove one entry from the :chi btw? Jan 30, 2022 at 13:38
75

The easiest way to navigate the quickfix list (or the location list, for that matter) is the unimpaired plugin.

Once the quickfix window is populated, [q and ]q go forward and back (respectively) in the quickfix list. [Q and ]Q go to the beginning and end (which is especially handy if you only have one item in the list; this makes vim complain about [q and ]q). So the workflow is:

  1. Run whatever command populates the quickfix list
  2. Type [Q to go to the first item
  3. Scroll through subsequent items (if any) with [q and ]q

If you're using Syntastic, you'll get the location list instead of the quickfix list. No problem; just use [L, ]L, [l, and ]l in the same way.

unimpaired has loads of other handy mappings too -- [e and ]e "bubble" lines up and down, [<Space> and ]<Space> insert blank lines above and below, etc. I was surprised nobody mentioned it here before; that's probably because it didn't exist until January 2010, though the question was asked in 2009.

34

Put the following two lines in your .vimrc file:

map <C-j> :cn<CR>
map <C-k> :cp<CR>

Now you can navigate through the errors using ctrl-j and ctrl-k, which mimics the standard down and up motion commands j and k.

4
  • 1
    <c-j> sometime (depends on the terminal?!) is erroneous or altogether not working, because it means the same as <NL> // the easiest ist, to to use <c-n> and <c-p> instead // otherwise: stackoverflow.com/questions/8304421/…
    – MacMartin
    Jan 21, 2019 at 10:24
  • I decided to use F8 and F9, like this: noremap <F7> :cprevious<CR> and noremap <F8> :cnext<CR> in .vimrc
    – Flimm
    Mar 5, 2020 at 9:14
  • I already use <C-j> and <C-k> to navigate between vim splits and tmux panes with vim tmux navigator. The combinations <C-M-j> and <C-M-k> might be an alternative. May 11, 2020 at 9:20
  • <M-n> and <M-N> feel right to me since they match the / search jumping, whereas j and k usually deal with one line at a time. Aug 20, 2020 at 18:12
26

You can also use :cc 2 (or any other number) to jump to, in this case, the second error in the quickfix window. Navigating with :cn, :cc 4, etc will put the cursor on the line in question.

19

In addition to @DrAl's great answer about how to open and close the quick window and navigate between entries, I made an image to show some of the other quick fix navigation commands.

Each group of 3 files below represents a set of quickfix results, e.g. from a vimgrep. cnewer and colder are for going through historic result sets. enter image description here

5
  • 2
    Very handy intuitive reference, I will keep this as a desktop wallpaper perhaps Feb 25, 2021 at 17:54
  • Thanks, I didn't know about older/newer! Is there anyway to browse the older lists without blindly stepping backwards? And for example see how many old quickfix lists there are?
    – Moberg
    May 28, 2021 at 11:41
  • @Moberg I NEED TO KNOW THIS TOO. Could you help? Jan 29, 2022 at 16:02
  • 2
    @Moberg ,VimNing and anyone else finding this in the future. You're probably looking for :chi[story]
    – EVK
    Jan 9 at 15:41
  • Great explanation with picture, thanks! Feb 28 at 16:05
9

Maybe this option didn't exist when this question was written (or maybe I'm embarrassing myself because there's something in my .vimrc that makes this happen) but when I get a Quickfix List, I just navigate it with j and k then hit <CR> (i.e. the Enter key) to jump to that place in the file.

Then, to get back to the Quickfix List I type Ctrl+W j for "move down a window" and I'm back.

Finally, when I'm done, I just type :q, like I would to close any normal window.

2
  • That's what I would like to do, but I get "Cannot make changes, 'modifiable' is off".
    – rtf
    Oct 5, 2016 at 20:23
  • 3
    You probably have over-ridden your Enter key. I had for example nmap <CR> o<Esc>, so I could add new-lines in normal mode.
    – ilvez
    Mar 5, 2019 at 16:07
8

The best-practice way of integrating JavaScript syntax-checking is using the Syntastic Vim plugin, which is using Vim's location-list (which is parallel to the quickfix) window.

I've written answers for this question and this question explaining how to do it, plus also how to get source-code browsing / tag-list for Vim using the community-driven jshint.com (which is way better than JSLint IMO) and Mozilla's DoctorJS (formerly jsctags).

6

the quickfix window is operated mostly like any other vim window: j down a line, k up a line, :cn to jump to the next error/warning, etc.

experiment!

1
  • 2
    +1 for experimentation, that's pretty much how I learned everything.
    – hora
    Nov 17, 2009 at 9:10
0

Although this requires > Vim 7.4.858, the cdo (or ldo for location lists) command allows updating a non-contiguous set of lines in a way you could once only do with sed:

:vimgrep /re/ %:p
:cdo! norm @a
# or
:cdo! s/re/repl/

The above shows running a recorded macro or a simple search and replace. Missing seems to a be a way to pipe through and external command as you can with :range! command

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.