Create a colorful Vim editor

angryTom
angryTomforward
2019-11-30 16:22:533734browse

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.

Create a colorful Vim editor

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

Create a colorful Vim editor

Then we run the following command to check the version of the installed Vim editor.

$ vim -version

Create a colorful Vim editor

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

Create a colorful Vim editor

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.

Create a colorful Vim editor

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

Create a colorful Vim editor

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/

Create a colorful Vim editor

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 个:

##Constant, such as numbers, quoted strings, true/false, etc.Special##Underlined
Identifier
##Variable
Statement
Keywords, such as if, else, do, while, etc.
Comment
##Comment
Type
Data types, such as int, double, string, etc.
PreProc
Preprocessor statements, such as #include
Constant
##Special symbols, such as "\t" , "\n" etc
Underlined text
##Error
Error
Here is an example. Open the Python script file named leap.py in the Vim editor. By default, the file has syntax highlighting as shown in the image below.

Create a colorful Vim editor

What if we want to turn the keyword into red? Just enter :hi Statement ctermfg=red. At this time, the color of if and else will change to red. :hi Statement ctermfg=red


Create a colorful Vim editor

Of course, we can also use the same method to change the color for comments, constants, and data types. In this way, you will form your own color scheme. Doesn’t it feel so refreshing?

This article comes from the php Chinese website,

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!

Statement:
This article is reproduced at:微信. If there is any infringement, please contact admin@php.cn delete