Home >Operation and Maintenance >Linux Operation and Maintenance >Create a colorful Vim editor
I believe everyone will notice when writing code using various powerful IDEs that various types of keywords in the code will be marked with unique colors, and then form a set of syntax highlighting rules. This is not only beautiful, but also makes the code easier to read. In the ancient artifact Vim, what we usually see is white text on a black background without any other colors. Is our Vim so boring? Of course not, Vim hides a lot of very practical functions and techniques that even experienced users won’t know about.
Let’s introduce it in detail below. Before we start, we need to check whether the Vim editor is installed on the system. Generally speaking, Ubuntu system comes with it by default. If you don’t have it, please install it yourself first.
$ sudo apt-get install vim
Then we run the following command to check the version of the installed Vim editor.
$ vim -version
Next, we can add a color scheme to the Vim editor. In order to see the effect, we have prepared a bash file named login.sh. Its content is as follows: login.sh
#!/bin/bash echo "Type your username" read username echo "Type your password" read password if [[ ( $username == "admin" && $password == "secret" ) ]]; then echo "Authorized user" else echo "Unauthorized user" fi
Run the following command to open the file using the Vim editor:
$ vim login.sh
Enable/disable syntax highlighting
Some distributions of Vim editors have enabled syntax highlighting for you by default, while some distributions But no. If syntax highlighting is not turned on by default, then we need to turn it on ourselves. The method of opening is actually very simple. After opening the login.sh file in the Vim editor, press the ESC key and type :syntax on. The syntax highlighting will be turned on, which is very convenient.
Similarly, turning off syntax highlighting is easy, just press the ESC key and type :syntax off.
Permanently enable or disable syntax highlighting
The method just now to turn on syntax highlighting It's only temporary. Once the current file is closed, you need to turn syntax highlighting back on the next time you open the file. If you want to permanently enable or disable syntax highlighting, you need to modify the .vimrc file. First, we use Vim to open the .vimrc file.
$ sudo vim ~ / .vimrc
Then, add a syntax on command to the open file to permanently enable syntax highlighting. Finally, enter :wq to save and close the file.
If you want to permanently disable syntax highlighting, just add the syntax off command.
Change the color scheme
By default, Vim applies a default color scheme for you after turning on syntax highlighting. In fact, Vim also has many color schemes available for us to use, and we can modify them by ourselves. After installing Vim, the color scheme file is located in /usr/share/vim/vim*/colors/. We can run the following command to see a list of Vim color scheme files.
$ ls -l /usr/share/vim/vim*/colors/
It can be seen that Vim provides us with a lot of color schemes, and we can choose freely according to our needs. Suppose, we have an html file of hello.html, and we want to change its color scheme to morning type.
hello.html
<html> <head> <title> Welcome </title> </head> <body> <p> Hello Everybody, Welcome to our site </p> </body> </html>
We first run the following command to open this html file in Vim.
$ vim hello.html
Press ESC and type :colorscheme morning so that we change the current color scheme of the file.
After applying the new color scheme. However, this change is still temporary and the color scheme will be reset after closing the editor.
如果想要永久设置特定的配色方案及背景,那么我们需要再次打开 .vimrc 文件并添加以下命令文本。下面配置的是夜晚配色方案,会将背景颜色设置为深色。
color evening set background=dark
根据语言选择配色方案
Vim 编辑器支持多种语言的语法,例如 PHP,Perl,Python,awk 等。根据不同语言,它会应用一套相应语言的配色方案。比如现在有一个名为 average.py 的 Python 文件,我们用 Vim 编辑器打开它。通过 Vim 编辑器的默认配色方案。
这是一个 Python 文件,但如果我们想以 Perl 的语法风格来显示,要怎么操作?我们只需输入 :set syntax=perl ,按 Enter 键就可以了,效果如下图所示。
:set syntax=perl
自定义配色方案
前面所介绍的那些配色方案,其实都是系统自带的,我们只是选择一下而已。如果我们想要个性化一点,自定义一套配色方案,要怎么操作?强大的 Vim 也是支持你这个需求的!一般而言,语法需要高亮的无非就是 变量、注释、常量 之类的,Vim 编辑器支持的语法高亮组有如下 9 个:
##Comment |
|
Type |
Data types, such as int, double, string, etc. |
PreProc |
Preprocessor statements, such as #include |
Constant | ##Constant, such as numbers, quoted strings, true/false, etc.|
|
|
linux system tutorial column, welcome to learn!
The above is the detailed content of Create a colorful Vim editor. For more information, please follow other related articles on the PHP Chinese website!