The vim editor is one of the most powerful ediors i am aware of. It is available in nearly every OS i am aware of and therefore it made sense for me to learn how to use one editor in detail and to use it in every OS.
This means that you can do everything with just a keyboard - and this is also the reason why it has multiple modes how it can be operated:
normal | For navigation and manipulation of text. This is the mode that vim will usually start in, which you can usually get back to with ESC. | :help Normal-mode |
insert | For inserting new text. The main difference from vi is that many important "normal" commands are also available in insert mode - provided you have a keyboard with enough meta keys (such as Ctrl, Alt, Windows-key, etc.). | :help Insert-mode |
visual | For navigation and manipulation of text selections, this mode allows you to perform most normal commands, and a few extra commands, on selected text. | :help visual-mode |
select | Similar to visual but with a more MS-Window like behavior. | :help select-mode |
command-line | For entering editor commands - like the help command in the 3rd column. | :help Command-line-mode |
Ex-mode | Similar to the command-line mode but optimized for batch processing. | :help Ex-mode |
However - i assume that you already know this. If not - have a look at the following Wikibook: "Learning the vi editor"
The Task
I want to show you how to use the vim editor under Windows in combination with a homegrown build script as an Integrated development environment. Usually if you want / have to compile things from the commandline then you will setup your project as follows:
- Install / Setup the compiler
- Install all 3rd party libraries
- Make a project directory or pull a project from a sourcecodemanagementsystem like git or svn
- Setup a make-script to be able to compile
- Write your code / make your changes
- Let the build script do its job
- Watch and interpret the output of the compiler / linker
- If there are errors / warnings -> Correct them in the corresponding sourcefiles and rebuild the whole thing
- Test the code on the testsystsem
- Deliver it to the customer
The bold items are those where developers spend most of their time: Writing code and fighting with the compiler to get things (at least) syntactically correct. This is also the part of the development lifecycle where a developer is happy when he has a compiler that supports him as good as possible:
- Syntax highlighting
- Code folding
- Compiler integration
- Multiple Windows
- Code suggestions
- Templates
- ... and so on
In a graphical environment eclipse or Microsofts Visual Studio are a valuable chioce for a powerful IDE, Notepad++ is a very good editor (which can be used as IDE as well - but then you are bound to Window) but if you are a commandline ninja then this is clearly not an option.
Setup
If you write C code and you dont want to do everything from hand then you have to write a buildscript. Usually in *nix environments there is a toolchain called automake which enables you to run the C-Compilecycle in one step including a lot of other fancy stuff, but in general vim is satisfied with a pure make.bat file which is composed like this:
:: Cleanup old build artefacts
del *.dll
del *.obj
del *.ilk
:: Compile everything
compile file1.c file2.c file3.c
:: Link everything together
link file1.obj file2.obj file3.obj
Ok this is the 20.000 ft. view of a compilescript but it should point you into the right direction. Try to automize things as good as possible to ensure that you dont accidentially forget to compile something or you link old stuff together.
The next Step is to find an editor that is able to utilize this buildscript and which is also able to interpret the errormessages the compiler is telling us. This is where vim enters the game! With the vim editor you can edit your C files and also compile it from weithin your editor. You have to know the following commands:
:make | This will run the make utility for you. It assumes that all variables are set and there is nothing else todo that starting your make.bat file |
:copen | Opens the quickfix window (which is displayed on the bottom of your screen) to show the results of the compilation. |
:cfirst | Jumps to the first error in your code (if there are some) |
:cn | Takes the cursor to the next error |
:cp | Jumps to the previous error. |
:clast | Proceeds to the last error which can be found in the output of the buildscript |
Result
Here is a screenshot how this looks like:
The editor runs within a commandline window in the windows command shell. Whenever i have to recompile the whole thing then i just have to press :make and let thevi editor together with my buildscript do the rest.
As soon as there are errors the edior highlights them in the quickfix window, and by entering :cfirst respective :cn i navigate in between my syntactic errors to fix them.
A final remark: This approach works also for Java and other languages. It is not limited to the C language.
Happy Coding !