vim - 设置自动缩进以用空格或制表符填充前导空格

发布于 2024-11-04 22:08:35 字数 480 浏览 5 评论 0原文

看起来如果我们启用 'ai',vim 会用 tabstop 填充前导空格。 我可以用“et”让它只填充空格。我不喜欢混有空格和制表符的 C 文件。

我的 vimrc:

set ts=4 et
set ai
set hlsearch
syntax on
filetype plugin indent on
autocmd FileType make setlocal noexpandtab

但是,在某些情况下,当我点击键盘上的“TAB”时,我确实需要输入制表符,例如,在 makefile 和其他一些文件中。 “autocmd FileType”命令不好:我无法在 vimrc 中添加每种文件类型。

我想要的很简单

  1. 自动缩进以填充前导区域 空间;
  2. 当按键盘上的“TAB”时,制表符停止 输入,而不是空格(所以没有“et”)

怎么办?

It seems if we enable 'ai', vim will fill the the leading space with tabstop.
I can make it fill with just space with 'et'. I don't like a C file mixed with space and tabstop.

My vimrc:

set ts=4 et
set ai
set hlsearch
syntax on
filetype plugin indent on
autocmd FileType make setlocal noexpandtab

However, in some condition I do need to input tabstop when I hit the 'TAB' on keyboard, for example, in makefile and some others.
The 'autocmd FileType' command is not good: I can't add every file type in vimrc.

What I want is simple:

  1. autoindent to fill leading area with
    space;
  2. when hit 'TAB' on keyboard, tabstop
    input, not space (so no 'et')

How to do it?

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

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

发布评论

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

评论(3

香橙ぽ 2024-11-11 22:08:35
inoremap <expr> <tab> ((getline('.')[:col('.')-2]=~'\S')?("\<C-v>\t"):(repeat(' ', &ts-((virtcol('.')-1)%&ts))))

如果我读得正确的话,它与@Lynch 的答案相同。

您还可以使用 :这将插入 而不调用任何映射,并忽略 Expandtab,除非您重新映射 ;出于某种原因。

如果您只想插入选项卡,

inoremap <Tab> <C-v><Tab>

它将忽略 Expandtab 设置。

inoremap <expr> <tab> ((getline('.')[:col('.')-2]=~'\S')?("\<C-v>\t"):(repeat(' ', &ts-((virtcol('.')-1)%&ts))))

It does the same as @Lynch answer if I read it correctly.

You can also use <C-v><Tab>: this will insert <Tab> without invoking any mappings and ignores expandtab unless you remapped <C-v> or <C-v><Tab> for some reason.

If you want to just insert tab do

inoremap <Tab> <C-v><Tab>

It will ignore expandtab setting.

巷子口的你 2024-11-11 22:08:35

我用一个函数做到了。我测试了它,但也许在某些特定情况下你将不得不修复一些错误。尝试将其添加到您的 vimrc 中:

set et

function! Inserttab()
    let insert = ""
    let line = getline('.')
    let pos = getpos('.')[2]
    let before = ""
    let after = line
    if pos != 1
        let before = line[ 0: pos - 1]  
        let after = line[pos : strlen(line) ]
    endif
    if pos != 1 && substitute(before, "[ \t]", "", "g") != "" 
         let insert = "\t"
    else
         let insert = "    "
    endif
    let line = before . insert . after 
    call setline('.', line)
    call cursor(line('.'), strlen(before . insert))
endfunction

inoremap <tab> <esc>:call Inserttab()<CR>a

基本上,它会将可视模式下的键重新映射到函数 Inserttab()。另请注意,如果将 ts 更改为 4 以外的值,它仍会输出 4 个空格而不是两个,因为该值是硬编码的。

我对 vim 脚本也不是很熟悉,但我认为使用的所有变量都是全局的,这是一件坏事。

我忘了提及,要“查看”空格,您可以使用set list。您可以使用 set nolist 禁用此功能。此外,在正常模式下,您可以使用 ga 来查看有关光标所在字符的信息。

编辑
我意识到您可能想在行的开头插入制表符。我的脚本在开头插入空格并在其他位置插入制表符。

如果你真的想要每次点击 tab 键时都有一个制表符,你可以简单地使用这个:

set et

function! Inserttab()
    let insert = ""
    let line = getline('.')
    let pos = getpos('.')[2]
    let before = ""
    let after = line
    if pos != 1
        let before = line[ 0: pos - 1]  
        let after = line[pos : strlen(line) ]
    endif
    let insert = "\t"
    let line = before . insert . after 
    call setline('.', line)
    call cursor(line('.'), strlen(before . insert))
endfunction

inoremap <tab> <esc>:call Inserttab()<CR>a

但我不明白这一点,在这个版本中,你将永远无法从插入模式手动缩进。

I did it using a function. I tested it, but maybe in some particular case you will have to fix some bugs. Try adding this to your vimrc:

set et

function! Inserttab()
    let insert = ""
    let line = getline('.')
    let pos = getpos('.')[2]
    let before = ""
    let after = line
    if pos != 1
        let before = line[ 0: pos - 1]  
        let after = line[pos : strlen(line) ]
    endif
    if pos != 1 && substitute(before, "[ \t]", "", "g") != "" 
         let insert = "\t"
    else
         let insert = "    "
    endif
    let line = before . insert . after 
    call setline('.', line)
    call cursor(line('.'), strlen(before . insert))
endfunction

inoremap <tab> <esc>:call Inserttab()<CR>a

Basicaly it does remap your key in visual mode to the function Inserttab(). Also note that if you change ts for something other than 4 it will still output 4 spaces instead of two because the value is hard coded.

Also im not very familiar with vim scripts, but I think all the variables used will be global which is a bad thing.

I forgot to mention that to "see" white spaces you can use set list. You disable this with set nolist. Also in normal mode you can use ga to see information about the character your cursor is on.

Edit
I realise that you may want to insert tab at the beginin of the line. My script insert space at the begining and tab anywhere else.

If you really want a tab every time you hit tab key you could simply use this:

set et

function! Inserttab()
    let insert = ""
    let line = getline('.')
    let pos = getpos('.')[2]
    let before = ""
    let after = line
    if pos != 1
        let before = line[ 0: pos - 1]  
        let after = line[pos : strlen(line) ]
    endif
    let insert = "\t"
    let line = before . insert . after 
    call setline('.', line)
    call cursor(line('.'), strlen(before . insert))
endfunction

inoremap <tab> <esc>:call Inserttab()<CR>a

But I dont see the point, with this version you will never be able to indent manually from insert mode.

朮生 2024-11-11 22:08:35

一种方法是

  1. :set sw=4 (或任何你想要的)
  2. :set ts=46 (或一些大数字)

那么自动缩进将不会插入制表符,除非你达到 46 个空格,在这种情况下你可以输入更大的数字。

唯一需要拖动的是,如果其他人正在使用选项卡,那么您必须重置 ts 以同意您正在编辑的文件。另一方面,它将使选项卡立即显而易见,这也是可取的。

One way to do it is

  1. :set sw=4 (or whatever you want)
  2. :set ts=46 (or some large number)

Then autoindent will not insert tabs unless you reach 46 spaces, in which case you can put in a higher number.

Only drag about this is if someone else is using tabs, then you have to reset ts to agree with the file you are editing. On the other hand, it will make the tabs immediately obvious, which can be desirable as well.

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