Change the indent width in Vim

To be familiar with Vim, practice is the best tutorial.

This is a HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>A Colorful Website</title>
  <meta charset="utf-8" />
</head>
<body>
  <h1 style="color: #07F">A Colorful Website</h1>
  <p>This is a website about color!</p>

  <h2 style="color: #C00">News</h2>
  <ul>
    <li>Nothing going on (yet)</li>
  </ul>
  <h2>Navigation</h2>
</body>
</html>

After insert some code before the </body>:

   <ul>
+  <li style="color: #F90">
+    <a href="orange.html">The Orange Page</a>
+  </li>
+  <li style="color: #00F">
+    <a href="blue.html">The Blue Page</a>
+  </li>
   </ul>
 </body>
 </html>

Now switch to VISUAL mode, select the added code and press >> to indent it:

<ul>
        <li style="color: #F90">
          <a href="orange.html">The Orange Page</a>
        </li>
        <li style="color: #00F">
          <a href="blue.html">The Blue Page</a>
        </li>
</ul>

Too many whitespace are inserted before the tag which is not what we want.

So we have to change the default indent space width in vim.

Refer to this great answer, we can append set tabstop=8 softtabstop=0 expandtab shiftwidth=2 smarttab to ~/.vimrc file which should be created if not exist.

Note: The mere existence of a user vimrc will change Vim’s behavior by turning off the compatible option such as syntax highlighting.

To enable vim syntax highlighting, append syntax on to ~/.vimrc.

Now pressing >> one time is to indent two whitespace.

  <ul>
    <li style="color: #F90">
      <a href="orange.html">The Orange Page</a>
    </li>
    <li style="color: #00F">
      <a href="blue.html">The Blue Page</a>
    </li>
  </ul>
vim
本作品采用《CC 协议》,转载必须注明作者和本文链接
日拱一卒
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
93
粉丝
85
喜欢
153
收藏
121
排名:71
访问:11.4 万
私信
所有博文
社区赞助商