Vim 中 Tab 键 == 4 个空格并在大括号后自动缩进

发布于 2024-07-08 01:43:20 字数 371 浏览 7 评论 0原文

如何制作 vi-Vim 从不使用制表符(将空格转换为制表符,不好!),使制表符键 == 4 个空格,并在大括号块后自动缩进代码,例如Emacs 可以吗?

另外,如何保存这些设置,这样我就不必再次输入它们?

我见过与此相关的其他问题,但它似乎总是与我想要的有点偏离。

How do I make vi-Vim never use tabs (converting spaces to tabs, bad!), makes the tab key == 4 spaces, and automatically indent code after curly brace blocks like Emacs does?

Also, how do I save these settings so I never have to input them again?

I've seen other questions related to this, but it always seems to be a little off from what I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

偏闹i 2024-07-15 01:43:20

正如其他几个答案中所指出的,现在首选的方法不是使用 smartindent,而是使用以下内容(在您的 .vimrc):

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab


In your [.vimrc:][1] file:

set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

帮助文件需要一些时间来适应,但是你读得越多,Vim 就越好:

:help smartindent

甚至更好,您可以将这些设置嵌入到源代码中以实现可移植性:

:help auto-setting

要查看当前设置:

:set all

正如 graywh 在评论中指出的那样, smartindent已被 cindent 取代,它“工作得更巧妙”,尽管仍然主要用于具有类似 C 语法的语言:

:help C-indenting

As has been pointed out in a couple of other answers, the preferred method now is NOT to use smartindent, but instead use the following (in your .vimrc):

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab


In your [.vimrc:][1] file:

set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

The help files take a bit of time to get used to, but the more you read, the better Vim gets:

:help smartindent

Even better, you can embed these settings in your source for portability:

:help auto-setting

To see your current settings:

:set all

As graywh points out in the comments, smartindent has been replaced by cindent which "Works more cleverly", although still mainly for languages with C-like syntax:

:help C-indenting

反目相谮 2024-07-15 01:43:20

相关,如果您打开一个同时使用制表符和空格的文件,假设您已经将

set expandtab ts=4 sw=4 ai

整个文件中的所有制表符和空格替换为

:%retab

Related, if you open a file that uses both tabs and spaces, assuming you've got

set expandtab ts=4 sw=4 ai

You can replace all the tabs with spaces in the entire file with

:%retab
梦断已成空 2024-07-15 01:43:20

获得特定于文件类型的缩进的最佳方法是在 vimrc 中使用 filetype plugin indent on 。 然后,您可以在 .vim/ftplugin/c.vim 中指定诸如 set sw=4 sts=4 et 之类的内容,而不必为所有正在编辑的文件和其他非 C 文件设置全局变量类型语法也将正确缩进(甚至 lisp)。

The best way to get filetype-specific indentation is to use filetype plugin indent on in your vimrc. Then you can specify things like set sw=4 sts=4 et in .vim/ftplugin/c.vim, for example, without having to make those global for all files being edited and other non-C type syntaxes will get indented correctly, too (even lisps).

神也荒唐 2024-07-15 01:43:20

要在大多数文件中使用 4 个空格制表符,在 Makefile 中使用真正的 8 宽制表符,以及在包括 C/C++ 在内的各种文件中自动缩进,请将其放入您的 ~/.vimrc 文件中:

" Only do this part when compiled with support for autocommands.
if has("autocmd")
    " Use filetype detection and file-based automatic indenting.
    filetype plugin indent on

    " Use actual tab chars in Makefiles.
    autocmd FileType make set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
endif

" For everything else, use a tab width of 4 space chars.
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.

To have 4-space tabs in most files, real 8-wide tab char in Makefiles, and automatic indenting in various files including C/C++, put this in your ~/.vimrc file:

" Only do this part when compiled with support for autocommands.
if has("autocmd")
    " Use filetype detection and file-based automatic indenting.
    filetype plugin indent on

    " Use actual tab chars in Makefiles.
    autocmd FileType make set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
endif

" For everything else, use a tab width of 4 space chars.
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.
那些过往 2024-07-15 01:43:20

在许多 Linux 系统(例如 Ubuntu)上,默认情况下 .vimrc 文件并不存在,因此建议您先创建它。

不要使用主目录中存在的 .viminfo 文件。 它用于不同的目的。

步骤 1:转到主目录

cd ~

步骤 2:创建文件

vim .vimrc

步骤 3:添加上述配置

filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab

步骤 3:保存文件,按 < kbd>Shift + ZZ

On many Linux systems, like Ubuntu, the .vimrc file doesn't exist by default, so it is recommended that you create it first.

Don't use the .viminfo file that exist in the home directory. It is used for a different purpose.

Step 1: Go to your home directory

cd ~

Step 2: Create the file

vim .vimrc

Step 3: Add the configuration stated above

filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab

Step 3: Save file, by pressing Shift + ZZ.

勿忘初心 2024-07-15 01:43:20

推荐的方法是使用基于文件类型的缩进,并且仅在不够时才使用 smartindent 和 cindent。

将以下内容添加到您的 .vimrc

set expandtab
set shiftwidth=2
set softtabstop=2
filetype plugin indent on

希望它作为不同的答案有所帮助。

The recommended way is to use filetype based indentation and only use smartindent and cindent if that doesn't suffice.

Add the following to your .vimrc

set expandtab
set shiftwidth=2
set softtabstop=2
filetype plugin indent on

Hope it helps as being a different answer.

世界如花海般美丽 2024-07-15 01:43:20

编辑 ~/.vimrc

$ vim ~/.vimrc

添加以下行:

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab

edit your ~/.vimrc

$ vim ~/.vimrc

add following lines :

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
疯到世界奔溃 2024-07-15 01:43:20

来自 VIM wiki

:set tabstop=4
:set shiftwidth=4
:set expandtab

From the VIM wiki:

:set tabstop=4
:set shiftwidth=4
:set expandtab
浮世清欢 2024-07-15 01:43:20

自动缩进基于当前语法模式。 我知道,如果您正在编辑 Foo.java,则输入 { 并按 Enter 会缩进以下行。

至于选项卡,有两种设置。 在 Vim 中,输入一个冒号,然后输入“set tabstop=4”,这会将制表符设置为显示为四个空格。 再次点击冒号并输入“set Expandtab”,这将为制表符插入空格。

您可以将这些设置放在主目录的 .vimrc (或 Windows 上的 _vimrc)中,这样您只需输入一次。

The auto-indent is based on the current syntax mode. I know that if you are editing Foo.java, then entering a { and hitting Enter indents the following line.

As for tabs, there are two settings. Within Vim, type a colon and then "set tabstop=4" which will set the tabs to display as four spaces. Hit colon again and type "set expandtab" which will insert spaces for tabs.

You can put these settings in a .vimrc (or _vimrc on Windows) in your home directory, so you only have to type them once.

倦话 2024-07-15 01:43:20

首先,不要在 Vim 中使用 Tab 键进行手动缩进。 Vim 在插入模式下有一对命令用于手动增加或减少缩进量。 这些命令是 Ctrl-TCtrl-D。 这些命令观察 tabstopshiftwidthexpandtab 的值,并保持空格和制表符的正确混合(制表符的最大数量,后跟任何制表符)必要的空格数)。

其次,如果您使用自动缩进,这些手动缩进键就不必经常使用。

如果 Ctrl-T 而不是 Tab 困扰您,您可以重新映射它:

:imap <Tab> ^T

您还可以重新映射 Shift-Tab 来执行 Ctrl -D deindent:

:imap <S-Tab> ^D

这里 ^T 和 ^D 是文字控制字符,可以作为 Ctrl-VCtrl-T 插入。

完成此映射后,您仍然可以使用 Ctrl-VTab 在缓冲区中键入文字 Tab。 请注意,如果您这样做,即使 :set Expandtab 打开,您也会得到一个未展开的制表符。

使用 :set smarttab 可以实现与 映射类似的效果,这也会导致行前面的退格键表现得智能。

smarttab模式下,当Tab不在行首使用时,它没有特殊含义。 这与我上面将 Tab 映射到 Ctrl-T 不同,因为在行中的任何位置(在插入模式下)使用 Ctrl-T 都会增加该行的缩进。

其他有用的映射可能是:

:map <Tab> >
:map <S-Tab> <

现在我们可以做一些事情,比如选择一些行,然后点击 Tab 来缩进它们。 或者在一行上按两次 Tab (在命令模式下)以增加缩进。

如果您使用正确的缩进管理命令,那么一切都由三个参数控制:shiftwidthtabstopexpandtab

shiftwidth 参数控制缩进大小; 如果您需要四个空格缩进,请使用 :set shiftwidth=4 或缩写 :set sw=4

如果仅这样做,则将使用空格和制表符的混合来创建缩进,因为 noexpandtab 是默认值。 使用:set Expandtab。 这会导致您在缓冲区中键入的制表符扩展为空格,并且 Vim 管理的缩进仅使用空格。

expandtab 打开时,如果您通过所有正确的 Vim 机制管理缩进,则 tabstop 的值变得无关紧要。 它控制选项卡在文件中出现时的显示方式。 如果您设置 tabstop=8 Expandtab,然后使用 Ctrl-VTab 将硬制表符潜入文件中,它将产生与像往常一样,下一个基于 8 列的选项卡位置。

Firstly, do not use the Tab key in Vim for manual indentation. Vim has a pair of commands in insert mode for manually increasing or decreasing the indentation amount. Those commands are Ctrl-T and Ctrl-D. These commands observe the values of tabstop, shiftwidth and expandtab, and maintain the correct mixture of spaces and tabs (maximum number of tabs followed by any necessary number of spaces).

Secondly, these manual indenting keys don't have to be used very much anyway if you use automatic indentation.

If Ctrl-T instead of Tab bothers you, you can remap it:

:imap <Tab> ^T

You can also remap Shift-Tab to do the Ctrl-D deindent:

:imap <S-Tab> ^D

Here ^T and ^D are literal control characters that can be inserted as Ctrl-VCtrl-T.

With this mapping in place, you can still type literal Tab into the buffer using Ctrl-VTab. Note that if you do this, even if :set expandtab is on, you get an unexpanded tab character.

A similar effect to the <Tab> map is achieved using :set smarttab, which also causes backspace at the front of a line to behave smart.

In smarttab mode, when Tab is used not at the start of a line, it has no special meaning. That's different from my above mapping of Tab to Ctrl-T, because a Ctrl-T used anywhere in a line (in insert mode) will increase that line's indentation.

Other useful mappings may be:

:map <Tab> >
:map <S-Tab> <

Now we can do things like select some lines, and hit Tab to indent them over. Or hit Tab twice on a line (in command mode) to increase its indentation.

If you use the proper indentation management commands, then everything is controlled by the three parameters: shiftwidth, tabstop and expandtab.

The shiftwidth parameter controls your indentation size; if you want four space indents, use :set shiftwidth=4, or the abbreviation :set sw=4.

If only this is done, then indentation will be created using a mixture of spaces and tabs, because noexpandtab is the default. Use :set expandtab. This causes tab characters which you type into the buffer to expand into spaces, and for Vim-managed indentation to use only spaces.

When expandtab is on, and if you manage your indentation through all the proper Vim mechanisms, the value of tabstop becomes irrelevant. It controls how tabs appear if they happen to occur in the file. If you have set tabstop=8 expandtab and then sneak a hard tab into the file using Ctrl-VTab, it will produce an alignment to the next 8-column-based tab position, as usual.

关于从前 2024-07-15 01:43:20

毕竟,您可以编辑 .vimrc,然后添加 conf

set tabstop=4

或执行命令

Afterall, you could edit the .vimrc,then add the conf

set tabstop=4

Or exec the command

只是一片海 2024-07-15 01:43:20

最简单的一个是在 vim 文件中或者简单地编辑 .vimrc

set tabstop=4

Simplest one will be in vim file or simply edit the .vimrc

set tabstop=4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文