Vim’s simplicity makes it possible to start working with it easily and quickly. The minimalistic interface allows users to concentrate on the main task of writing. Understanding the main concepts behind the program also helps users learn and make use of its deeper functionalities.

The power behind Vim allows users to accomplish certain tasks better, faster, and much more easily. Its powerful functionality allows users to put in minimum effort while getting maximum efficiency, giving them the capability to solve simple and complex problems.

Vim is actively used by programmers. Its functionality, usability, and flexibility make it a great choice for those who write a lot of code.

Increasing Efficiency

Lighting

A basic option in Vim is syntax lighting which allows users to “see” the code more easily. This change contributes to faster code reading and writing.

If you edit a file in Python, launch :set filetype=python and Vim will add colors. To view the list of available languages, open the catalogue $VIMRUNTIME/syntax/.

Indents

A programmer’s code usually uses indentation. Vim can help programmers write code with the correct indentation. For example, if you made an indent to a certain point and want the following lines of code to be written using the same indent, use :set autoindent.

If you start a new operator's block, use :set smartindent to create the following lines with one more indent level.

Moves

There are many ways to move within code.

With Vim, files can be opened by placing the cursor on the file’s name in the code and pressing gf.

By placing the cursor on a variable’s name and pressing gd, Vim will move you to a local definition of the variable’s name. Gd does the same for global contents, searching from the beginning of a file.

Easy Writing

Omni completion

“Omni completion” was added to Vim 7 and is one of the most useful functions of the program. This allows users to automatically finish text according to the current context.

For example, if the long name of a variable is used, you can set a key-combination for autocompletion and Vim will do the rest.

Vim solves this task with the help of ftplugins.

Here is an example with a simple program in Python:

def hello():
    print 'hello world'

def hey_body():
    print 'what’s up?'

After entering this program, create a new line in this file, type he and press ctrl-x ctrl-o. You will be shown variants for autocompletion.

Vim automatically uses the first variant for autocompletion and allows users to choose the next or previous option with the help of ctrl-n and ctrl-p, respectively.

Use of fragments

Code fragments are small pieces of code which, as a rule, are constantly repeating. Users may use plugins to help insert fragments in the code, as many good programmers do.

Let’s use plugin SnippetsEmu as an example.

1. Open a new file, e.g. test.py.

2. Press keys d, e, f and then <tab>.

3. SnippetsEmu creates a function structure.

def <{fname}>(<{args}>):
        """
        <{}>
        <{args}>
        """
        <{pass}>
        <{}>

Note: If all you can see is def<tab> , it’s probably that the fragments plugin is not loaded. Enter :runtime! ftplugin/python_snippets.vim.

4. Now the cursor will be placed at the function name, i.e. fname.

5. Type a function name, e.g. test.

6. Press <tab> and the cursor will automatically move to arguments. Pressagain to precede with the next item’s completion.

7. Now enter a comment, e.g. No.

8. Press <tab> again and enter Hello World.

9. Press <tab>.

10. The program is ready!

You should now see the following:

def test():
    """
    Just say Hi
    """
    print 'Hello World'

The best thing about this is that SnippetsEmu creates a standard format which must be followed and nothing will be “forgotten”.

Advice and Recommendations to Make Your Work Easier

1.Work with several files

If you are a software developer or somebody who uses Vim as a main editor, there is a high chance that you need to work with several files simultaneously. Instead of opening different files in different cover tabs, you can open several files in one tab, sending their names as arguments in Vim commands. For example:

vim file1 file2 file3 The first file (file1 in the example) is a current file and is read out in the buffer. Within the editor, use commands :next or :n to pass over to the next file and :prev or :p to go back to the previous one.

To quickly switch to the first or the last file, use :bfand :bl commands, respectively.

To open and start editing another file, use :e with the filename as an argument (use the full procedure in case the file is absent from the current catalogue).

2.Autocomplete

Want to save time and increase accuracy? Use abbreviations. They will come in handy when writing long or complex words, especially if they are repeated several times within a file. The Vim command for contractions is :ab.

For example:

:ab asap as soon as possible

Every time asap is used, it will automatically be substituted by the words ‘as soon as possible’.

3.Split windows

In some cases, you’ll want to copy a part of code or text from one file to another. Although this process is simple when working with editors with graphic interface, it becomes a bit tiresome and laborious with the command line editor. Fortunately, Vim minimizes the time and effort needed for this.

You can open one of two files and then split the Vim window in order to open another file with the help of :split command and the filename as an argument, e.g. :split test.py.

It should be pointed out that the command will split the window horizontally. If you want to split it vertically, use command :vsplit. As soon as both files open, copy material from one file to another and press ctrl+w to switch the activity to another file.

4.Preserving indents

Most experienced programmers work in Vim using default indents. Although this is meant to save time, it causes problems when you insert code with an indent of its own.

You can use pastetoggle to solve this problem. Add this line

set pastetoggle=<F3>

to a vimrc file and press F3 in insert mode just before inserting the code. This will preserve the original indentation. Note that you can substitute F3 with any other key if this one is already used for another functionality.

Conclusion

Text editing, in general, and code editing, in particular, require additional time and effort to be invested into a project. By learning new commands and making a habit of using them, the task of editing can be streamlined and made so much easier because of programs like Vim.

This introductory article was devoted to familiarizing users with Vim, its basics, and its capabilities. We hope it is a useful guide for beginners who want to understand what Vim is. Future posts will look at a more advanced specifics and more complicated aspects of this editor.