📜 ⬆️ ⬇️

Vim for the programmer

Vim’s favorite editor is not IDE. However, it has many great features that make it easier to work with the code.

Usually the cycle of working with the code is reduced to correct the code-compile-correct code-compile- ... In Vim there are tools sharpened specifically for this process. It looks like this:

: make calls the compiler with the necessary parameters, after the end of the compilation you can jump on errors in the source code (if any were found).
: cc goes to the location of the current error ,: cc [nr] goes to error number [nr].
: cnext and : cprevious go to the next and previous error location, respectively.
: clist shows a list of errors. However, the list of errors is inconvenient to constantly call with the command : clist , besides, you cannot search for this list (which is sometimes useful, especially when the list is large). Therefore, there is an alternative way to jump on errors - open a window (yes, in Vim there are windows ,: help windows if that) with a list of errors.
: copen opens this window. This is a normal window, it has all the commands for moving, searching, etc. 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 the error, the source will open 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 make life much easier, but they are too lazy to recruit. Therefore, you can assign them some buttons in the .vimrc file. For example, here is a piece of my .vimrc:
" build mappings
map < F7 > :wall \| make < Cr >
map < F4 > :cn < Cr > zvzz:cc < Cr >
map < S-F4 > :cp < Cr > zvzz:cc < Cr >


<F7> saves all files and executes the command: make.
<F4> this is no longer simple: cnext, but a little bit of magic. : cn <Cr> goes to the next error. zv expands fold (about this next time if you're interested), zz positions the current line in the center of the screen, and: cc <Cr> is needed to show the error message below, which will disappear by this time. Shift + F4 is all the same.

To make it all work, you configure Vim to your compiler. (The compiler in this case is a conditional concept, it can be either a real compiler, or an interpreter, run with the “check code for errors” keys or, for example, tidy, which will render HTML to you.) This is done with the command : compiler . : compiler without parameters will give you a list of supported "compilers", and : compiler foo will enable support for the foo compiler. This support is included in 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 run when you give Vim a command : make . For gcc, for example, it will be make , for perl - perl -Wc% , etc.
'errorformat' is a special kind of string that explains how to parse error messages that the compiler throws out.
So if your compiler’s support is not included in the Vim bundle and on www.vim.org in the Scripts section, you can write it yourself.

What I have been writing here is the very basics in order to be able to assess the possibilities. Read more in : help quickfix . There you will learn in particular that like : make you can use the command : grep to search for a substring in files and navigate through the found lines, as well as many other interesting things :)

Happy Vimming!

Source: https://habr.com/ru/post/28362/


All Articles