Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript is responsible for coding


May 24, 2021 Vim


Table of contents


So far we've covered a bunch of Vim commands, which allow you to quickly customize Vim. All commands except automatic command groups are single-line commands that you can add to ~/.vimrc

The next part of the book will focus on Vim scripting as a real programming language, but before that, I'll start with something to be aware of when writing a lot of Vim scripts.

Comments

Vim scripts are powerful, but for programmers who want to get into the field, in recent years it seems to have gradually become like a curvy maze, making it hard for those entering to find their way home.

Vim's options and commands are often short and hard to read, and dealing with compatibility issues increases the complexity of your code. Writing a plug-in and allowing users to customize it adds a level of complexity.

Be defensive when writing lots of Vim scripts. To get used to adding comments about what a piece of code does, if you have a related help topic, it's best to explain it in the comments!

Not only will this make your future maintenance easier, but if you share your ~/.vimrc files with Bitbucket or GitHub (which is highly recommended), these comments will also help others understand your script.

Group

The mapping we created earlier allows us to quickly and easily edit and load ~/.vimrc Unfortunately, this ~/.vimrc to grow so fast that it gets out of control and becomes difficult to read and browse.

Our approach to this situation is to use Vim's code folding feature, which organizes multiple lines of code into one part and then collapses the part of the code. I f you've never used Vim's folding feature, you should take a look at it as soon as possible. Many people , myself included , consider code folding to be essential in everyday coding .

First we need to set up a collapse for the Vim script file. Add ~/.vimrc file:

augroup filetype_vim
    autocmd!
    autocmd FileType vim setlocal foldmethod=marker
augroup END

This tells Vim to use the marker folding method marker script file.

Now execute in the window that displays the file of ~/.vimrc :setlocal foldmethod=marker I f you don't execute this command, you'll find that loading ~/.vimrc doesn't work because Vim already has FileType for this file, and automatic commands are only executed when you set the file type. This allows you not to do this manually in the future.

Now add two lines at the beginning and end of the automatic command group, like this:

" Vimscript file settings ---------------------- {{{
augroup filetype_vim
    autocmd!
    autocmd FileType vim setlocal foldmethod=marker
augroup END
" }}}

Switch to common mode, place the cursor on any line of the text, and tap za V im collapses all {{{ the row that contains the . . . to the }}} that contains the . . . Tapping za za all these lines.

At first you might think it would be unreasonable to comment on the source code for code folding, and I thought so at first. F or most documents I still don't think it's appropriate. Because not everyone uses the same editor, folding comments added to the code are like noise for those who don't use Vim.

But the Vim script file is special because a person who doesn't use Vim is less likely to read your code, and most importantly, if you don't group the code, it says you don't know where to write it, and the severity can run through the veins and vomit blood.

Try it yourself first, maybe you'll gradually like it.

Short Names

For most commands and options, Vim supports the use of their abbreviations. For example, the following two commands do exactly the same thing:

:setlocal wrap
:setl wrap

I strongly caution you not to use these ~/.vimrc or plug-ins you write. V im scripts are already obscure enough for beginners; Even if you know what an abbreviation means, others may not understand it.

In other words, abbreviations are useful only when you manually execute commands during coding. After you press the enter key, no one will see what you enter, so you don't have to enter more characters.

Practice

Check your ~/.vimrc file and organize all relevant lines. Y ou can start with: Basic Settings, FileType-specific settings, Mappings, and Status Line. Then add collapsed tags and titles in each section.

Think about how to get Vim to automatically collapse all the lines that set up collapsed comments the first time you open the file . ~/.vimrc Read :help foldlevelstart know how to do it.

Check your ~/.vimrc change the abbreviations of all commands and options to full names.

Check your ~/.vimrc make sure there is no sensitive information in it. Then create a git or Mercury repository, put the ~/.vimrc file inside, and then link the ~/.vimrc

Submit the repository you just created and put it on Bitbucket or GitHub so that everyone else can see and learn about it. Remember to submit and push to the repository frequently so that your changes are also recorded.

If you don't just use Vim on one machine, you can clone that repository and link the file to ~/.vimrc This allows you to use the same Vim configuration on all machines.