Vim 中的 Tab 与 Space 首选项

发布于 2024-08-07 10:38:50 字数 2412 浏览 8 评论 0原文

Vim 在选项卡对比方面非常包容。空间偏好。据我了解,制表符设置表示制表符的宽度。 shiftwidth 设置指定使用 <<>> 命令时要增加/减少的列数,而 >softtabstop 设置会影响在插入模式下按 Tab 键时要插入的空白量。如果 expandtab 打开,则 Tab 键会插入 softtabstop 个空格字符。在 expandtab 关闭的情况下,按 Tab 键会插入与 softtabstop 匹配的尽可能少的制表符+空格字符。 (如果我错了,请纠正我。)

最后一点让我想知道:是否存在您不希望 shiftwidth == tabstop && 的实际情况? tabstop == softtabstop?我想不出一个。就我而言,如果我可以在一次分配中将所有这 3 个值设置为相同的值,那将是最方便的。例如调用:

:set stab=4

这相当于运行:

:set tabstop=4 softtabstop=4 shiftwidth=4 

任何人都可以建议如何做到这一点吗?


更新

感谢到目前为止 too much phphobbskaiser.se 的回复。我不是单独回复每个问题,而是在这里更新问题。

Softtabstop 并关闭了 Expandtab

我在上面说过,在关闭 Expandtab 的情况下,按 Tab 键会插入尽可能少数量的制表符+空格字符。 softtabstop。我坚持这一点,但我认为我需要解释一下我的意思。我将尝试通过几个例子来做到这一点。要继续操作,请运行 :set list 以便您可以看到制表符。

tabstop=4 softtabstop=2 shiftwidth=4 noexpandtab

在插入模式下,按 Tab 键会插入 2 个空格字符。再次按 Tab 键,不再插入两个空格字符(总共 4 个空格字符),而是用一个制表符替换前面的 2 个空格。 Tabstop 设置为 4,因此单个制表符的宽度与 4 个空格相同。

tabstop=4 softtabstop=6 shiftwidth=4 noexpandtab

在插入模式下,按 Tab 键会插入 1 个制表符加 2 个空格。制表符的宽度为 4,因此总宽度为 6,这是使用 3 个字符实现的。第二次按 Tab 键将插入两个制表符,并删除先前插入的两个空格。总宽度为 12,这是使用 3 个字符实现的。

在这两个示例中,Vim 都会插入与 softtabstop 匹配的尽可能少的制表符+空格字符。

如果我在关闭 Expandtab 的情况下工作,我看不到自己想要通过将 softtabstop 设置为与 tabstop 不同的值来实现的额外粒度控制。能够使用单个命令将 tabstopsofttabstopshiftwidth 设置为相同的值对我来说仍然很有用。

Expandtab 是否会使 Softtabstop 变得多余?

tabstop=4 softtabstop=0 shiftwidth=4 Expandtab

在插入模式下,按 Tab 键可插入 4 个空格。按删除键会删除一个空格 - 因此,如果您不小心按了 Tab 键,则必须退格 4 次。

tabstop=4 softtabstop=4 shiftwidth=4 Expandtab

在插入模式下,按 Tab 键可插入 4 个空格。按退格键可删除 4 个空格。

如果我在打开 expandtab 的情况下工作,我希望使用删除键来删除与 Tab 键插入的相同数量的空格。因此,在这种情况下,我也觉得能够同时为 tabstopsofttabstopshiftwidth 分配相同的值会很有用。 。

快捷方式仍然有用

Vim 提供如此多的灵活性真是太好了,但我不认为自己需要它。我只是希望能够选择制表符的宽度,以及它是“硬”制表符(使用制表符)还是“软”制表符(由空格组成)。在硬选项卡和软选项卡之间切换非常简单(:set Expandtab!),但我希望设置选项卡宽度更简单,而不必摆弄 3 个不同的参数。

因此,我提出的类似 :set Stab=4 的建议对我来说仍然听起来不错。

Vim is very accommodating when it comes to tab Vs. space preferences. As I understand it, the tabstop setting indicates the width of a tab character. The shiftwidth setting specifies how many columns to increment/decrement when using the << and >> commands, whereas the softtabstop setting influences the amount of whitespace to be inserted when you press the Tab key in insert mode. If expandtab is on, the tab key inserts softtabstop number of space characters. Whereas with expandtab switched off, pressing the Tab key inserts a the smallest possible number of tab+space characters that matches softtabstop. (Please correct me if I'm wrong.)

This final point makes me wonder: is there a practical case where you wouldn't want shiftwidth == tabstop && tabstop == softtabstop? I can't think of one. As far as I am concerned, it would be most convenient if I could set all 3 of these to the same value, in one single assignment. e.g. calling:

:set stab=4

which would be equivalent to running:

:set tabstop=4 softtabstop=4 shiftwidth=4 

Can anyone suggest how this could be done?


UPDATE

Thanks for the replies so far from too much php, hobbs and kaiser.se. Rather than reply to each individually, I'm updating the question here.

Softtabstop with expandtab switched off

I said above that with expandtab switched off, pressing the Tab key inserts a the smallest possible number of tab+space characters that matches softtabstop. I stand by that, but I think I need to explain what I meant. I shall attempt to do so by way of a few examples. To follow along, run :set list so that you can see tab characters.

tabstop=4 softtabstop=2 shiftwidth=4 noexpandtab

In insert mode, pressing the tab key inserts 2 space characters. Press the tab key a second time, and instead of inserting two more space characters (for a total of 4 space characters) it replaces the previous 2 spaces with a single tab character. Tabstop is set to 4, so a single tab character has the same width as 4 spaces.

tabstop=4 softtabstop=6 shiftwidth=4 noexpandtab

In insert mode, pressing the tab key inserts 1 tab character plus 2 spaces. The tab character has a width of 4, so the total width is 6, and this is achieved using 3 characters. Pressing the tab key a second time inserts two tab characters, and removes the two spaces that were inserted previously. The total width is 12, and this is achieved using 3 characters.

In both of these examples, Vim inserts the minimum possible number of tab+space characters that matches softtabstop.

If I am working with expandtab switched off, I can't see myself wanting the extra granular control that can be achieved by setting softtabstop to a different value from tabstop. It would still be useful for me to be able to set tabstop, softtabstop and shiftwidth to the same value with a single command.

Does expandtab make softtabstop redundant?

tabstop=4 softtabstop=0 shiftwidth=4 expandtab

In insert mode, pressing the tab key inserts 4 spaces. Pressing the delete key deletes a single space - so you have to backspace 4 times if you hit the tab key by accident.

tabstop=4 softtabstop=4 shiftwidth=4 expandtab

In insert mode, pressing the tab key inserts 4 spaces. Pressing the backspace key deletes 4 spaces.

If I am working with expandtab switched on, I would prefer the delete key to remove the same amount of whitespace as the tab key inserts. So in this case, too, I feel that it would be useful to be able to assign the same value to tabstop, softtabstop and shiftwidth simultaneously.

A shortcut would still be useful

It's great that Vim provides so much flexibility, but I can't see myself needing it. I just want to be able to choose the width of a tab, and whether it is a 'hard' tab (using a tab character) or a 'soft' tab (made up of spaces). Toggling between hard and soft tabs is easy enough (:set expandtab!), but I wish it was more straightforward to set the width of tab, without having to fiddle with 3 different parameters.

So my proposed suggestion for something like :set stab=4 still sounds good to me.

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

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

发布评论

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

评论(7

尬尬 2024-08-14 10:38:50

在 Vim 本身中创建 stab 选项并不容易,但我已经编写了这个命令/函数,您可以将其放入 .vimrc (或者插件文件,如果你组织得超级好)。使用:Stab,系统将提示您输入缩进级别以及是否使用expandtab。如果您按回车键而不给它新的缩进级别,它只会打印当前设置。

" put all this in your .vimrc or a plugin file
command! -nargs=* Stab call Stab()
function! Stab()
  let l:tabstop = 1 * input('set shiftwidth=')

  if l:tabstop > 0
    " do we want expandtab as well?
    let l:expandtab = confirm('set expandtab?', "&Yes\n&No\n&Cancel")
    if l:expandtab == 3
      " abort?
      return
    endif

    let &l:sts = l:tabstop
    let &l:ts = l:tabstop
    let &l:sw = l:tabstop

    if l:expandtab == 1
      setlocal expandtab
    else
      setlocal noexpandtab
    endif
  endif

  " show the selected options
  try
    echohl ModeMsg
    echon 'set tabstop='
    echohl Question
    echon &l:ts
    echohl ModeMsg
    echon ' shiftwidth='
    echohl Question
    echon &l:sw
    echohl ModeMsg
    echon ' sts='
    echohl Question
    echon &l:sts . ' ' . (&l:et ? '  ' : 'no')
    echohl ModeMsg
    echon 'expandtab'
  finally
    echohl None
  endtry
endfunction

Creating a stab option in Vim itself would not be easy, but I've whipped up this command/function that you can drop in your .vimrc (or a plugin file if you're super-organized). Use :Stab and you will be prompted for an indent level and whether or not to use expandtab. If you hit enter without giving it a new indent level, it will just print the current settings.

" put all this in your .vimrc or a plugin file
command! -nargs=* Stab call Stab()
function! Stab()
  let l:tabstop = 1 * input('set shiftwidth=')

  if l:tabstop > 0
    " do we want expandtab as well?
    let l:expandtab = confirm('set expandtab?', "&Yes\n&No\n&Cancel")
    if l:expandtab == 3
      " abort?
      return
    endif

    let &l:sts = l:tabstop
    let &l:ts = l:tabstop
    let &l:sw = l:tabstop

    if l:expandtab == 1
      setlocal expandtab
    else
      setlocal noexpandtab
    endif
  endif

  " show the selected options
  try
    echohl ModeMsg
    echon 'set tabstop='
    echohl Question
    echon &l:ts
    echohl ModeMsg
    echon ' shiftwidth='
    echohl Question
    echon &l:sw
    echohl ModeMsg
    echon ' sts='
    echohl Question
    echon &l:sts . ' ' . (&l:et ? '  ' : 'no')
    echohl ModeMsg
    echon 'expandtab'
  finally
    echohl None
  endtry
endfunction
温柔少女心 2024-08-14 10:38:50

这是我第一次尝试编写 VimScript,但这里是:

function! Stab(value)
    let &shiftwidth  = a:value
    let &softtabstop = a:value
    let &tabstop     = a:value
endfunc

如果我将其放入 .vimrc 文件中,我可以通过运行 :call Stab(X) 来调用它,其中 X 是所需的制表符宽度。目前这是一个足够的解决方案,但如果有人能建议一种更容易拨打电话的方法,我将不胜感激。

我还创建了一个快速总结当前设置的函数,我已将其映射到 ctrl-Tab:

nmap <C-Tab> :call TabParams()<CR>
function! TabParams()
    echo "tabstop:     ".&tabstop
    echo "shiftwidth:  ".&shiftwidth
    echo "softtabstop: ".&softtabstop
endfunc

嗯,我为这个答案设置了 100 点赏金,现在我自己已经解决了一半。不确定我是否可以接受我自己的答案...

This is my first attempt at writing VimScript, but here goes:

function! Stab(value)
    let &shiftwidth  = a:value
    let &softtabstop = a:value
    let &tabstop     = a:value
endfunc

If I put this in my .vimrc file, I can call it by running :call Stab(X), where X is the desired tab width. This is an adequate solution for now, but if anyone can suggest a way of making it easier to call I would be grateful.

I've also created a function that quickly summarizes the current settings, which I have mapped to ctrl-Tab:

nmap <C-Tab> :call TabParams()<CR>
function! TabParams()
    echo "tabstop:     ".&tabstop
    echo "shiftwidth:  ".&shiftwidth
    echo "softtabstop: ".&softtabstop
endfunc

Well, I put up a 100 point bounty for this answer, and now I've half solved it myself. Not sure if I can accept my own answer...

原野 2024-08-14 10:38:50

您还可以在编辑模式下使用 Ctrl-T 缩进,使用 Ctrl-D 取消缩进到由 shiftwidth 设置的下一个缩进级别,无论tabstopsofttabstopexpandtab 设置。 Vim 会自动添加/删除空格或制表符,将您带到正确的列。

如果您使用这些命令而不是 Tab/Backspace 来控制缩进,则不必担心所有这些选项卡设置是否组合在一起,并且始终达到正确的缩进级别。

You can in edit mode also use Ctrl-T to indent and Ctrl-D to deindent to the next indentation level as set by shiftwidth, regardless of the tabstop, softtabstop or expandtab settings. Vim will automatically add/remove spaces or tabs to bring you to the right column.

If you use these commands to control indentation instead of Tab/Backspace you don't have to worry about all these tab settings fitting together and always get to the correct indentation level.

绳情 2024-08-14 10:38:50

如果expandtab被设置(正如太多的php指出的那样),softtabstop变得多余。将 shiftwidth 设置为与 tabstop 不同的唯一原因是为了迎合奇怪的习惯;例如,您使用四个空格缩进,但您更喜欢 tab 插入八个空格。

如果 expandtab 未设置,那么事情会变得更加模糊。如果您希望代码在 cat 和非 vim 编辑器中看起来与在 vim 中一样,那么 tabstop 应始终设置为 8;在这种情况下,您可以将 softtabstopshiftwidth 设置为您首选的缩进级别。如果您希望文件中的每个“物理选项卡”代表一个缩进级别,则可以将 tabstopshiftwidth 设置为您首选的缩进级别,并保留 softtabstop< /code> 为零(将其设置为等于 tabstop 是等效的,除非您更改 tabstop 它将不同步,而零仅意味着“请忽略此”) 。

If expandtab is set then (as too much php points out), softtabstop becomes redundant. The only reason you might set shiftwidth differently from tabstop would be to cater to an odd habit; for instance, you use four-space indents but you prefer tab to insert eight spaces.

If expandtab is unset then things get fuzzier. If you want your code to look the same in with cat and non-vim editors as it does in vim, then tabstop should always be set at 8; in this case you would set softtabstop and shiftwidth both to your preferred indent level. If you instead prefer that every "physical tab" in the file represents one indent level, you would set tabstop and shiftwidth to your preferred indent level and leave softtabstop at zero (setting it equal to tabstop is equivalent except that if you change tabstop it will get out of sync, while zero just means "ignore this please").

腻橙味 2024-08-14 10:38:50

您对 softtabstopexpandtab 的理解是错误的 - 因此您建议的 stab 选项不是很有用。

expandtab 适用于当您想要使用空格而不是制表符来表示所有内容时。如果设置 expandtab,那么 Vim 会忽略 softtabstop 选项并使用 tabstopshiftwidth 来计算有多少个空格插入。

softtabstop 仅适用于混合制表符和空格的情况,允许您通过精细控制(2 或 4 个空格)缩进),同时将制表符宽度保持在较高值(通常为 8),以便文本出现在其他应用程序中。设置 softtabstop=tabstop 不会完成任何事情,因为 Vim 将始终使用制表符进行缩进。

更新: 正如 kaizer.se 所指出的,如果您使用 Expandtab,那么如果你希望 Vim 退格多个空格,就像它们是制表符一样,你仍然需要设置 softtabstop

Your understanding of softtabstop and expandtab is wrong - so the stab option you suggest wouldn't be very useful.

expandtab is for when you want to use spaces instead of tabs for everything. If you set expandtab, then Vim ignores the softtabstop option and uses tabstop and shiftwidth to work out how many spaces to insert.

softtabstop is only for when you would like to use a mix of tabs and spaces, allowing you to indent with fine control (2 or 4 spaces), while keeping tab width at a higher value (usually 8) so that text appears in the other applications. Setting softtabstop=tabstop doesn't accomplish anything because Vim will always use tabs for indenting.

Update: As kaizer.se has pointed out, if you are using expandtab, then you still need to set softtabstop if you want Vim to backspace multiple spaces as though they are a tab.

诺曦 2024-08-14 10:38:50

您是否经常更改空白设置,您真的需要一个功能来管理它吗?如果您经常弄乱制表符并设置 Expandtab,那么随着时间的推移,当您更改传递给 stab 的不同值的文件时,您可能会陷入混乱。今天我使用 :call Stab (4),明天使用 :call Stab (2),上周使用 :call Stab (8) 。听起来即使你写了它,你很快就会停止使用它。

如果您计划始终传递相同的值来刺穿,为什么不直接编辑全局设置呢?在 vim: 中

:e $MYVIMRC

添加以下内容:

set tabstop=4
set shiftwidth=4  "tabs are 4 spaces wide (default = 8)
set expandtab     "Convert tabs to spaces

这就是我的 .vimrc 的设置方式。

Are you changing your white space settings so often you really need a function to manage that? If you are messing with tabstop a lot and also setting expandtab, you are probably going to have a mess over time as you change files with different values passed to stab. Today I use :call stab (4), tomorrow it's :call stab (2) and last week it was :call stab (8). Sounds like even if you write it, you'll soon stop using it.

If you plan to always pass the same value to stab, why not just edit your global settings? In vim:

:e $MYVIMRC

and add the following:

set tabstop=4
set shiftwidth=4  "tabs are 4 spaces wide (default = 8)
set expandtab     "Convert tabs to spaces

This is how my .vimrc is setup.

旧情别恋 2024-08-14 10:38:50

一个有用的选项是 softtabstop=-1,它将其设置为 shiftwidth 的值。
您还可以将 shiftwidth 设置为 0,在这种情况下,将使用 tabstop 值。

One useful option is softtabstop=-1 which will set it to the value of shiftwidth.
You can also set shiftwidth to 0, in which case the tabstop value will be used.

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