当前位置:网站首页>Match VIM from zero (0) -- Introduction to vimscript

Match VIM from zero (0) -- Introduction to vimscript

2022-07-07 01:22:00 aluluka

Through the previous series of articles , I believe you guys should have been right vim There was a strong interest in , Maybe many friends become like me through slow use , Leave vim You won't be able to write code . If you want to use it for a long time vim, Even the vim As the main code editor , Then spend some time studying vim The configuration of is very useful . If you just because linux The platform is installed by default vi/vim , Usually, it is mainly used to change the matching file , Temporary use , Then I'd better quit this article , So as not to waste time .

The title says to build from zero basis vim A bit of a headline party , To understand the content of the following series of articles, at least understand vim Some of the basic concepts in it , I think before reading this series of columns , You should know the following

  1. know vim, And know vim Basic operation , For example, enter the insertion mode 、 Move the cursor , And know how to save and exit
  2. know vim Related terms , Buffer list 、 buffer 、 window 、 Command mode 、 Insert mode and so on
  3. know vim Where is your configuration file

If you don't know , Please move to my column , Follow the contents of the column to do experiments . Understand these concepts mentioned inside .

About configuration , I am going to introduce the following aspects :

  1. vimscript Basic content of , And add some things that have not been introduced before vim Characteristics of
  2. neovim lua Corresponding interface
  3. Some plug-ins and related configurations are recommended , Do throw bricks and attract jade

There may be a little friend to ask ,vimscript Both writing and reading are obscure , and neovim Has adopted a more understandable and modern lua 了 , It is necessary to learn vim script Do you ?

I think it's necessary , First neovim yes vim Another implementation of , Want to learn well neovim Nature can't get around vim, And learn well vim Naturally, I can't get around vimscript . secondly ,lua The interface in the script inherits from vimscript . Want to use lua The premise of configuration is to know how to use vimscript To configure . They are like learning MFC I can't get around it Win32 API equally . So I think I want to learn neovim The configuration of naturally cannot be bypassed vimscript . and neovim Fully compatible with vim Don't worry about it vimscript stay neovim Problems that cannot be implemented in . indicating contrast neovim It didn't happen vimscript 100% function , Sometimes it still depends on vimscript To achieve .

Demo environment

The previous series of articles were intended to be in mac For demonstration on , But what I found was that mac It's troublesome to record on the screen , and mac Upper neovim I've configured it , Behavior may be naked vim There's a gap ( Although you can start without loading the configuration file ), So I used WSL2 Ubuntu Upper neovim Do a demonstration .

Just this demo environment is not right vim Configured . It is also convenient to demonstrate from 0 Start configuration .

The environment of this tutorial is linux in neovim Of 0.6.1 edition , Although the official has released 0.8 edition , however Ubuntu It seems that the official source is still 0.6. Let's continue to demonstrate with it

first vimscript Script

Any language I learn seems to be from printing hello world At the beginning , I'm not immune . Our tutorial also starts from printing hello world Let's get started

vimscript Can be used in echo and echom To print statements , For example, we can enter echo "hello world". We found that at the bottom of the screen hello world word .
 Insert picture description here

Then we use echom "hello world" Find out , It is still printed below hello world word . What's the difference ?

To understand the difference between them, we need to introduce a new command :message . This command allows you to view message-history The content in , We can simply understand this command as viewing vim Operation log of ( Although not checking the log ). Executing this order, we found ,message-history Only one of them hello world. At this time, their differences come out , One will write the printing information while printing message-history, The other is just printing .
 Insert picture description here
You just need to print different sentences to know echom The content will be written to the log , and echo Can't . Here's a place for you to experiment , I'm not going to do a demonstration .

Now let's finish a little exercise , Use vim Script to write a welcome message, such as hello, jack, We let users see this welcome message every time they open it . For users here, we will fix it for the time being , Later, we can make dynamic changes Welcome users .

We know that every time we open vim, It will load the configuration file , We just need to write the command to be executed into the configuration file . Remember where the configuration file is ? in the light of neovim It supports vimscript and lua To configure ( Of course, it also supports other languages ). Let's switch the directory to ~/.config/nvim . If you have been configured before , Please make a backup of the configuration first . And make sure init.lua (lua To configure ) and init.vim (vimscript To configure ) There can only be one

Let's try to use vimscript To complete this function . We create a new document ~/.config/nvim/init.vim. Then write a sentence in it echo 'hello, jack', And then quit . We find that every time we enter neovim after , There will always be such a sentence below .
 Insert picture description here
 Insert picture description here

Let's see how to use it lua To print such a sentence ?lua The corresponding function in is print . We can go straight to Previously backed up init.lua Write in print("hello, jack") To complete this work ( Remember now before init.vim Backup ). It should be noted that lua Medium print Write write the information together to message in , That is to say, it is connected with echom The effect is the same . If you want to execute echo Operation can be used api.vim.nvim_echo() . Its usage is complicated , I won't introduce it now , Interested partners can check the official website , Try it to implement this example of printing welcome messages .
 Insert picture description here
This concludes the content of this article , Finally, I'll give you a summary :

  1. neovim If you want to load vimscript To configure , You can put the configuration file in ~/.config/nvim/init.vim in , If you want to load lua To configure , You can put the file in ~/.config/nvim/init.lua in .
  2. echoechom Will print strings , The difference is echom Send the printed content to a place called message-history Write in the right place .lua Medium print Have echom The function of
  3. have access to message Command view message-history The content of
  4. from vimscript From the use of , It says vim Some orders of , We write the command in the file and let vim To carry out .vim Command and vimscript It's a little bit like shell Command and shell The relationship between scripts
原网站

版权声明
本文为[aluluka]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061734046053.html