Reference: Learn Vimscript the Hard Way


"Setting variables
let myVariable = 1
let myString = 'Hello'

logical control

Reference: Comparisons – Learn Vimscript the Hard Way


if 3 >= 1
    echo 'True'
elseif 3 < 4
    echo 'True again'
elseif 3 == 3
    echo 'True true'
    echo 'False'

Strings and numbers in VIM can be directly compared, such as:echo '3' >= 3Return to 1.

Functions function

Reference: Functions – Learn Vimscript the Hard Way

Function names must begin with capital letters.

function MyFunction()

But if you overload the current vimrc, you will encounterfunction already existsWarning.
So it’s better to define a function as a rewritable function, that is, to change it tofunction!

function! MyFunction()



## Built-in function

### has(..)

- has ('Programming Language')
- has ('Hardware Architecture')
- has('xxx')

### system(...)

- system('rm /tmp/*')

### buffer

- bufnr ('%'): Returns the number of buffers in the current VIM

## autocmd event trigger

Refer to the excellent official VIM document (Chinese translation):

The format is `au [tocmd] [group] {event} {pat} [nested] {cmd}.`
In Chinese: `au [tocmd] [group] {event} {filename rule} [nested] {command}`

If the command is more complex, it is recommended to create a function and then `call func ()'in autocmd.

## Common Skills

## Get the file name, path, extension of the current buffer

[Reference: How can I see the full path of the current file?] (
[Reference: How do I get the name and extension of the current file?] (

As we all know, in VIM `%'represents the current buffer, we can add `filename-modifiers' to operate `%'to get the relevant information of the file associated with the buffer.

Register `%'returns the name of the current file. So we can run the current script with commands like `! Python%'.
In VIM, `%'can also do a lot of expansion:

:echo @% |” directory/name of file
:echo expand(‘%:p’) |” full path “PATH”
:echo expand(‘%:p:h’) |” directory containing file “HEAD”
:echo expand(‘%:t’) |” full name of file “TAIL”
:echo expand(‘%:t:r’) |” Only name of file “ROOT”
:echo expand(‘%:e’) |” Only extension of file “EXTENSION”

When we use vimrc, we can omit echo and expand. For example:
` nnoremap < C-g >:! CMD%: p: h < CR > so that when you press `Ctrl-g', you can execute the `CMD'command in the directory where the current file is located.

“Access Path
echo expand(‘%:p’) “/home/mool/vim/src/version.c

“Get the full name of the file
echo expand(‘%:t’) “version.c

“Get file names, excluding extensions
echo expand(‘%:t:r’) “version

“Get the extension
echo expand(‘%:e’) “c

## Multiple lines newline

Starting with \ and ending with \ can connect multiple lines of behavior.

au Filetype ruby

        \ setlocal ts=2  |
        \ setlocal sts=2 |
        \ ...