Vim for the programmer
Vim's favorite editor is not an IDE. However, it has many great features that make it easier to work with code.
Usually, the cycle of working with code is reduced to correct code-compile-correct code-compile- ... Vim has tools tailored specifically for this process. It looks like this::
make calls the compiler with the necessary parameters, after the end of compilation you can jump over errors in the source (if any were found).
: cc goes to the current error location,: cc [nr] goes to the error number [nr].
: cnext and : cprevious go to the next and previous places of the error, respectively.
: clistshows a list of errors. However, the list of errors is inconvenient to constantly call the command : clist , in addition, you can not search for this list (which is sometimes useful, especially when the list is large). Therefore, there is an alternative way to jump by mistake - open a window (yes, Vim has windows ,: help windows if that) with a list of errors.
: copen opens this very window. This is an ordinary window, all the move, search, etc. commands work in it. The only difference is that initially the text in it is marked as unchangeable so that you do not accidentally distort it. When you press Enter on the line with an error, the source opens and the cursor is positioned on the line in which this error was actually found.
: cclose, as you probably already guessed, it closes the window.
These teams greatly facilitate life, but gain their laziness. Therefore, you can assign them any buttons in the .vimrc file. For example, here is a piece of my .vimrc:
saves all files and executes the command: make.
this is no longer simple: cnext, but a bit of magic. : cnproceeds to the next error. zv expands fold (more on that next time if interested), zz positions the current line in the center of the screen, and: ccneeded in order to show the error message below, which by this time will be gone. With Shift + F4, everything is similar.
To make this work, you configure Vim to fit your compiler. (The compiler in this case is a conditional concept, it can be either a real compiler, or an interpreter launched with the keys "check the code for errors" or for example tidy, which will validate your HTML.) This is done using the command : compiler . : compiler without parameters will give you a list of supported “compilers”, and : compiler foo will enable foo compiler support. This support is enabled by the banal execution of the compiler / foo.vim script (the script is searched in 'runtimepath') The script itself exposes several Vim options, of which two are most interesting to us:
'makeprg' is the program that will be executed when you give Vim the command : make . For gcc for example, it will be make , for perl - perl -Wc% , etc.
'errorformat' is a special kind of line explaining to Vim how to parse error messages that the compiler will throw out.
So if there is no support for your compiler in the Vim bundle and on www.vim.org in the Scripts section, you can write it yourself.
What I wrote here is the very basics, so that you can evaluate the possibilities. Read more at : help quickfix. There you will learn in particular that, like : make, you can use the : grep command to search for a substring in files and navigate through found lines, as well as many other interesting things :)
Happy Vimming!
Usually, the cycle of working with code is reduced to correct code-compile-correct code-compile- ... Vim has tools tailored specifically for this process. It looks like this::
make calls the compiler with the necessary parameters, after the end of compilation you can jump over errors in the source (if any were found).
: cc goes to the current error location,: cc [nr] goes to the error number [nr].
: cnext and : cprevious go to the next and previous places of the error, respectively.
: clistshows a list of errors. However, the list of errors is inconvenient to constantly call the command : clist , in addition, you can not search for this list (which is sometimes useful, especially when the list is large). Therefore, there is an alternative way to jump by mistake - open a window (yes, Vim has windows ,: help windows if that) with a list of errors.
: copen opens this very window. This is an ordinary window, all the move, search, etc. commands work in it. The only difference is that initially the text in it is marked as unchangeable so that you do not accidentally distort it. When you press Enter on the line with an error, the source opens and the cursor is positioned on the line in which this error was actually found.
: cclose, as you probably already guessed, it closes the window.
These teams greatly facilitate life, but gain their laziness. Therefore, you can assign them any buttons in the .vimrc file. For example, here is a piece of my .vimrc:
" build mappings
map :wall \| make
map :cnzvzz:cc
map :cpzvzz:cc To make this work, you configure Vim to fit your compiler. (The compiler in this case is a conditional concept, it can be either a real compiler, or an interpreter launched with the keys "check the code for errors" or for example tidy, which will validate your HTML.) This is done using the command : compiler . : compiler without parameters will give you a list of supported “compilers”, and : compiler foo will enable foo compiler support. This support is enabled by the banal execution of the compiler / foo.vim script (the script is searched in 'runtimepath') The script itself exposes several Vim options, of which two are most interesting to us:
'makeprg' is the program that will be executed when you give Vim the command : make . For gcc for example, it will be make , for perl - perl -Wc% , etc.
'errorformat' is a special kind of line explaining to Vim how to parse error messages that the compiler will throw out.
So if there is no support for your compiler in the Vim bundle and on www.vim.org in the Scripts section, you can write it yourself.
What I wrote here is the very basics, so that you can evaluate the possibilities. Read more at : help quickfix. There you will learn in particular that, like : make, you can use the : grep command to search for a substring in files and navigate through found lines, as well as many other interesting things :)
Happy Vimming!