如何在 vim 中仅使用制表符(而不是空格)

发布于 2024-09-18 16:57:48 字数 613 浏览 5 评论 0原文

我更喜欢使用 tab 而不是 空格(可能与大多数其他人有点不同)

但是我发现,当我在行尾,它会添加一些空格,但不会添加制表符。所以,我必须删除它们并按tab

我想知道如何将vim设置为:

  1. 仅使用tab来缩进一行制表
  2. 符看起来像4个空格,但实际上
  3. 在最后按enter时 是一个制表符一行中,新行仅以选项卡开始,

我已经在谷歌上搜索了一段时间,但没有找到一个好的答案。预先感谢您


更新

@Alok 提供的答案在大多数情况下效果很好。但我发现,有时,这取决于文件类型。例如,如果您正在编辑 haml 文件,并且 vimfiles/indent/ 中有一个 haml.vim,则所有选项卡将被转换为空格。所以如果你希望它只是tab,你应该修改(或删除)相应的缩进文件。

I prefer to use tab than white space(may be a little different from most of others)

But I found, when I hit Enter at the end of line, it will add some white spaces, but not tab. So, I have to delete them and press tab.

I want to know how to set vim as:

  1. use only tab to indent the lines
  2. a tab looks like 4-spaces, but actually is a tab
  3. when hit enter at the end of a line, the new line is started with only tabs

I've googled for this for a while, but not found a good answer. Thank you in advance


UPDATE

The answer @Alok has provided works well in most of cases. But I just found, sometimes, it depends on the file type. For example, if you are editing a haml file, and there is a haml.vim in your vimfiles/indent/, then all the tabs will be converted to space. So if you want it to be tab only, you should modify(or delete) the corresponding indent file.

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

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

发布评论

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

评论(3

舟遥客 2024-09-25 16:57:48

您正在寻找的设置是:

set autoindent
set noexpandtab
set tabstop=4
set shiftwidth=4

单行:

set autoindent noexpandtab tabstop=4 shiftwidth=4

autoindent 可以替换为 smartindentcindent,具体取决于您的喜好。另请参阅文件类型插件缩进

http://vim.wikia.com/wiki/Indenting_source_code

The settings you are looking for are:

set autoindent
set noexpandtab
set tabstop=4
set shiftwidth=4

As single line:

set autoindent noexpandtab tabstop=4 shiftwidth=4

autoindent can be replaced with smartindent or cindent, depending upon your tastes. Also look at filetype plugin indent on.

http://vim.wikia.com/wiki/Indenting_source_code

你的他你的她 2024-09-25 16:57:48

讨论迟到了,仅适用于那些努力强制使用选项卡的人。使用当前版本的 vim (>8) 需要设置:

:set noet ci pi sts=0 sw=4 ts=4

扩展为:

:set noexpandtab
:set copyindent
:set preserveindent
:set softtabstop=0
:set shiftwidth=4
:set tabstop=4

我们还可以仅对特定文件类型使用此设置,利用setlocal

autocmd FileType c,cpp,java,python setlocal noet ci pi sts=0 sw=4 ts=4

如需进一步参考,请参阅:https://vim.fandom。 com/wiki/Indent_with_tabs,_align_with_spaces

Late to the discussion, just for anyone who struggled to force the use of tabs. With the current version of vim (>8) it was required to set:

:set noet ci pi sts=0 sw=4 ts=4

which expands to:

:set noexpandtab
:set copyindent
:set preserveindent
:set softtabstop=0
:set shiftwidth=4
:set tabstop=4

We can also use this setup with only specific file types, taking advantage of setlocal:

autocmd FileType c,cpp,java,python setlocal noet ci pi sts=0 sw=4 ts=4

For further reference, see: https://vim.fandom.com/wiki/Indent_with_tabs,_align_with_spaces

陌生 2024-09-25 16:57:48

就我而言,set indentexpr= 成功了。

我发现配置文件使用 fd python.vim / 设置了烦人的行为。在 /usr/share/vim/vim90/indent/python.vim 中有一行 setlocal indentexpr=python#GetIndent(v:lnum) 。函数 python#GetIndent 定义在 /usr/share/vim/vim90/autoload/python.vim 中。该函数返回一个奇怪的值,该值不是 shiftwidthtabstop 的倍数。因此 vim 插入了一个制表符,后跟一些空格。

现在,我已将 indentexpr 设置为空值,我认为 vim 会退回到 autoindentsmartindentcindent 已关闭。 (https://vimhelp.org/indent.txt.html)

当然 还需要设置 noexpandtab 。我不确定这里是否有任何其他相关设置。


编辑: autoindent 并不完美,它只不过将新行缩进与前一行一样多。

set smartindent
set cinwords=if,elif,else,try,except,finally,with,for,while,class,def

无法按预期工作,并且 cindent 似乎不适合 python,因此使用 indentexpr 毕竟是正确的方法。

幸运的是,可以配置缩进功能,请参阅 help ft-python-indent 或查看代码 - 这提供了有关disable_parentheses_indenting 功能的更多信息。看来这对我来说是解决方案:(

if !exists('g:python_indent')
    let g:python_indent = {}
endif
let g:python_indent.disable_parentheses_indenting = 1

https://vi.stackexchange.com/a/38824/43863)

In my case set indentexpr= did the trick.

I have found the config files which set the annoying behavior with fd python.vim /. In /usr/share/vim/vim90/indent/python.vim there is the line setlocal indentexpr=python#GetIndent(v:lnum). The function python#GetIndent is defined in /usr/share/vim/vim90/autoload/python.vim. This function returned a weird value which was not a multiple of shiftwidth and tabstop. Therefore vim inserted a tab followed by some spaces.

Now, that I have set indentexpr to an empty value, I think vim falls back to autoindent. smartindent and cindent are turned off. (https://vimhelp.org/indent.txt.html)

Of course set noexpandtab is required, too. I am not sure if any other settings are relevant here.


EDIT: autoindent is not perfect, it does nothing more than indent the new line as much as the previous line.

set smartindent
set cinwords=if,elif,else,try,except,finally,with,for,while,class,def

does not work as expected and cindent does not seem suitable for python, so using indentexpr is the way to go after all.

Luckily the indentation function can be configured, see help ft-python-indent or look at the code – that was more informative regarding the disable_parentheses_indenting feature. It seems this is the solution for me:

if !exists('g:python_indent')
    let g:python_indent = {}
endif
let g:python_indent.disable_parentheses_indenting = 1

(https://vi.stackexchange.com/a/38824/43863)

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