Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Vim custom highlight grouping and summary of some practical skills

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

preface

The Vim code editor for Linux is simple and easy to use, but the keyword highlighting and color matching of the programming language itself need to be set by the user himself. The following article mainly introduces you about Vim custom highlight grouping and practical skills related content, need friends to take a look at it together.

highlight

In Vim, we can customize some color groupings and apply them to certain strings that match a particular pattern. These groupings become highlight groups.

We can type highlight directly from the command line without any parameters, so that we can see all the highlighted group information in the current Vim.

:highlight

Here we define a highlight group named myColor and set the background color to purple:

:highlight myColor ctermbg=purple guibg=purple

We use the highlight command to define the highlight group, followed by the group name and color pattern. where ctermbg represents the background color in the command line and guibg represents the background color in the GUI interface. Similarly, cterfg and guifg represent command line and GUI foreground colors, respectively.

You can also alias an existing highlight group directly by simply connecting the new group to the existing group.

highlight link {newgroup} {oldgroup}

After defining the highlight group, we can apply it to the text. Next, I will introduce you to several ways to set the highlight.

match

The match command sets highlight mode in the current window in the following format:

:match {group} /pattern/

For example, let's highlight all the numbers grouped with myColor:

:match myColor /\v\d+/

The feature of the match command is that only one highlight mode can be used at a time. When a new highlight mode is set, the old highlight mode is cancelled.

If you want to highlight multiple modes at once, you can use the 2match and 3match commands, which have the same syntax and functionality as match, but each command can only set one highlight mode at a time.

:2match myColor /anotherPattern/

Unhighlight

:match none:2match none:3match none

syntax match

The syntax match command is used to set syntax highlighting in the following format:

:syntax match {group} pattern

For example, we highlight the contents enclosed in quotes using myColor:

:syntax match myColor /\v"\w+"/

Unhighlight

:syntax clear

syntax keyword

The syntax keyword command is used to highlight keywords in the following format:

:syntax keyword {group} word1 word2 ...

For example, we highlight the words hello and world as keywords:

:syntax keyword myColor hello world

Unhighlight

:syntax clear

practical tips

Search Results Highlight

Search is the default highlighted grouping for matching text. We can customize the color of this grouping. The code below will make the search results appear on a green background.

:highlight Search ctermbg=green guibg=green

Extra Long Text Highlight

When writing code, we often encounter cases where a line of code is too long. We can detect whether there is too long code in real time by setting the highlight.

:highlight rightMargin term=bold ctermfg=blue guifg=blue:match rightMargin /.\%> 72v/

The above command highlights in blue the lines longer than 72 words.

End of Line Space Highlight

We often encounter extra spaces at the end of lines, but normally it is difficult to find these extra spaces. It is appropriate to highlight these spaces.

:highlight extraSpace ctermbg=red guibg=red:match extraSpace /\v\s+$/

summary

The above is the whole content of this article, I hope the content of this article can bring some help to everyone's study or work, if you have questions, you can leave a message to exchange, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report