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

How to realize automatic indentation of vi in linux

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

Share

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

This article mainly shows you "how to achieve automatic indentation in linux", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve automatic indentation in linux vi" this article.

Automatic indent

When writing code in some programming languages, indentation is an important part of the style to ensure that the code is more readable. If desired, you can indent automatically in the vi editor based on the style settings of the programming language. Use autoindent to turn automatic indentation on or off (see listing 3).

Listing 3. Open auto indent #! / bin/ksh##for file in/ etc/*doif [[- f ${file}]]; thenecho "${file} is a file" ~: set autoindent

After that, if you enter a space or tab at the beginning of a line, subsequent new lines will be indented to the same position. In command mode, type: set autoindent, and then press enter to turn on automatic indentation. Determine the indentation level by setting shiftwidth. For example, set shiftwidth=4 sets each level of indentation to four spaces (see listing 4).

Listing 4. Set indentation level #! / bin/ksh##for file in/ etc/*doif [[- f ${file}]]; thenecho "${file} is a file" elif [[- d ${file}]]; thenecho "${file} is a directory" fidone~~:set shiftwidth=4

In command mode, you can use the > > command to add one level of indentation to an existing line, and use (command to reduce one level of indentation. Add an integer to these commands to increase or decrease multiple lines by one level of indentation. For example, place the cursor at the beginning of line 6 in listing 4, and after entering command mode, type 5) to add one level of indentation to the following five lines. Listing 5 shows the results.

Listing 5. Indent code block #! / bin/ksh##for file in/ etc/*doif [[- f ${file}]]; thenecho "${file} is a file" elif [[- d ${file}]]; thenecho "${file} is a directory" fidone~~

You can use the: set noautoindent command to turn off automatic indentation. You can also use this command and the abbreviations of the autoindent command, namely: set ai and: set noai. You can also use: set ai sw=4 to turn on indentation in a command and set the indentation level.

If you want to enable automatic indentation and set the indentation level to four spaces each time you start a vi session, add the set ai sw=4 line to the .exrc file in the home directory.

The above is all the contents of the article "how to achieve automatic indentation in vi in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report