[英](g)Vim -> show 4 spaces, but save 2 spaces (tabs)
Is it possible to open files that are indented with 2 spaces, but show me 4 space indentation, and when I make 4 spaces, it saves in a 2 space format? 是否可以打开以2个空格缩进的文件,但显示4个空格缩进,当我制作4个空格时,它以2个空格的格式保存吗?
Edit 编辑
It turns out I also need to be able to ensure that it works if the file has a mix of tabs, 2 spaces, and 4 spaces. 事实证明,如果文件包含制表符,2个空格和4个空格的混合文件,我还需要确保它能正常工作。
Edit 2 编辑2
So, here is my current solution. 因此,这是我当前的解决方案。 I'm having to remap my (originally mapped to :w) so that I can place my cursor back where it was (and give me one "history back" as far as cursor positions when I do a save. Is there a way to do this without affecting cursor position (and not adding the substitution to the history, either)? 我必须重新映射我的(最初映射到:w),以便我可以将光标放回原处(并在保存时给我一个“历史记录”,直到光标位置为止。)这样做不会影响光标位置(也不会在历史记录中添加替代项)?
function! s:ShimSpaces()
nunmap <C-S>
nmap <C-S> ms``mt:w<Cr>`t`s
augroup SeoTabs
autocmd!
autocmd BufReadPost,BufWritePost * set tabstop=4
autocmd BufReadPost,BufWritePost * %substitute/^ \+/&&/e
autocmd BufReadPost * %substitute/ \+$//e
autocmd BufWritePre * %substitute/^\( \+\)\1/\1/e
autocmd BufWritePre * set tabstop=2
autocmd BufWritePre * retab
augroup END
endfunction
command! -n=0 -bar ShimSpaces :call s:ShimSpaces()
This is the opposite of what was asked here . 这与这里的要求相反。
The help has an example for a similar use case of different tab widths, see :help retab-example
. 帮助中有一个使用不同制表符宽度的类似用例的示例,请参见:help retab-example
。
Adapting that to doubling / halving spaces: 使其适应于加倍/减半的空间:
:augroup AdaptIndent
:autocmd!
:autocmd BufReadPost,BufWritePost * %substitute/^ \+/&&/e
:autocmd BufWritePre * %substitute/^\( \+\)\1/\1/e
:augroup END
With *
, this will affect all opened files. 使用*
,这将影响所有打开的文件。 To restrict this to certain files, see :help autocmd-patterns
. 要将其限制为某些文件,请参见:help autocmd-patterns
。
Edit: With the :augroup
wrapping, this can be turned off again via :autocmd! AdaptIndent
编辑:用:augroup
包装,可以通过:autocmd! AdaptIndent
重新关闭:autocmd! AdaptIndent
:autocmd! AdaptIndent
. :autocmd! AdaptIndent
。 This way, you can easily toggle this on / off. 这样,您可以轻松地将其打开/关闭。 For ease of use, I'd put this in a function and define a custom command calling it. 为了易于使用,我将其放在函数中并定义了一个自定义命令来调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.