Vim 中的 Tab Vs Space 首选项 [英] Tab Vs Space preferences in Vim

查看:24
本文介绍了Vim 中的 Tab Vs Space 首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

:set stab=4

这相当于运行:

:set tabstop=4 softtabstop=4 shiftwidth=4

谁能建议如何做到这一点?

<小时>

更新

感谢太多phphobbskaiser.se 的回复.我没有单独回复每个人,而是在这里更新问题.

关闭扩展选项卡的软制表符

我在上面说过,在 expandtab 关闭的情况下,按下 Tab 键会插入尽可能少的与 softtabstop 匹配的 tab+space 字符.我支持这一点,但我想我需要解释一下我的意思.我将尝试通过几个例子来做到这一点.要继续,请运行 :set list 以便您可以看到制表符.

tabstop=4 softtabstop=2 shiftwidth=4 noexpandtab

在插入模式下,按 Tab 键会插入 2 个空格字符.再次按下 Tab 键,而不是再插入两个空格字符(总共 4 个空格字符),而是用一个制表符替换前两个空格.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 本身中创建一个 stab 选项并不容易,但我已经创建了这个命令/函数,你可以删除在你的 .vimrc (或者一个插件文件,如果你是超级组织的).使用 :Stab 会提示您输入缩进级别以及是否使用 expandtab.如果你在没有给它一个新的缩进级别的情况下按 Enter,它只会打印当前设置.

<前>" 把所有这些放在你的 .vimrc 或插件文件中命令!-nargs=* 刺调用刺()功能!刺()让 l:tabstop = 1 * input('set shiftwidth=')如果 l:tabstop > 0我们也想要 expandtab 吗?let l:expandtab = confirm('设置 expandtab?', "&Yes\n&No\n&Cancel")如果 l:expandtab == 3中产?返回万一让 &l:sts = l:tabstop让 &l:ts = l:tabstop让 &l:sw = l:tabstop如果 l:expandtab == 1设置本地扩展选项卡别的setlocal noexpandtab万一万一" 显示选中的选项尝试echohl 模式消息回声设置制表位="echohl 问题回声 &l:tsechohl 模式消息回声'移位宽度='echohl 问题回声 &l:swechohl 模式消息回声'sts='echohl 问题回声 &l:sts .' ' .(&l:et ? ' ' : 'no')echohl 模式消息echon '扩展表'最后echohl 无尾端端功能

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.

解决方案

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

这篇关于Vim 中的 Tab Vs Space 首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆