# 设置vim缩进和TAB

在程序开发中，设置默认缩进4个空格，并且将`TAB`替换成空格，比较符合常用的编程规范。

`vim`的配置文件`~/.vimrc`添加如下内容

```
set tabstop=4       " The width of a TAB is set to 4.
                    " Still it is a \t. It is just that
                    " Vim will interpret it to be having
                    " a width of 4.

set shiftwidth=4    " Indents will have a width of 4

set softtabstop=4   " Sets the number of columns for a TAB

set expandtab       " Expand TABs to spaces
```

## 显示编辑文件中的TAB

在编辑文档时，文档中包含tab往往无法分辨，在vim中可以定义显示特殊字符的方法：

```bash
set list
set listchars=tab:>-
```

此时，如果文档中有tab符号，则会显示成`>---`，便于检查。

## 空格替换为tab

vim提供了一个 `!retab` 命令来替换空格到 `<tab>`

```bash
# first in .vimrc set up
:set expandtab
:set tabstop=4
# or you can do this
:set tabstop=4 shiftwidth=4 expandtab

# then in the py file in command mode, run
:retab!
```

你也可以用命令

```bash
sed -e 's/    /\t/g' test.py > test.new.py
```

## tab替换为空格

vim命令如下：

```bash
:set et|retab
```

你也可以用搜索来替换：

```bash
:%s/\t/    /g
```

## 参考

* [Redefine tab as 4 spaces](http://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces)
* [Displaying tabs as characters](https://vi.stackexchange.com/questions/422/displaying-tabs-as-characters)
* [How to replace tabs with spaces?](https://vi.stackexchange.com/questions/495/how-to-replace-tabs-with-spaces)
* [replace tab by space or replace spaces by tab in linux](https://songhuiming.github.io/pages/2016/07/31/replace-tab-by-space-or-replace-spaces-by-tab-in-linux/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/vim/redefine_tab_as_4_spaces.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
