问题描述
我查看了 Stack Overflow 上的几个问题,了解如何将空格转换为制表符而没有找到我需要的内容.关于如何将制表符转换为空格似乎还有更多问题,但我正试图做相反的事情.
I've looked over several questions on Stack Overflow for how to convert spaces to tabs without finding what I need. There seem to be more questions about how to convert tabs to spaces, but I'm trying to do the opposite.
在 Vim
中,我尝试了 :retab
和 :retab!
没有运气,但我相信这些实际上是为了从标签到无论如何都是空格.
In Vim
I've tried :retab
and :retab!
without luck, but I believe those are actually for going from tabs to spaces anyways.
我在命令提示符下尝试了 expand
和 unexpand
都没有成功.
I tried both expand
and unexpand
at the command prompt without any luck.
这是有问题的文件:
如何使用 Vim
或 shell 将前导空格转换为制表符?
How can I convert leading spaces to tabs using either Vim
or the shell?
推荐答案
使用 Vim 扩展所有 领先 空间(比 'tabstop'
),您使用 retab
但首先确保 'expandtab'
被重置(:verbose set ts? et?
是你的朋友).retab
需要一个 range,所以我通常指定 %
表示整个文件".
Using Vim to expand all leading spaces (wider than 'tabstop'
), you were right to use retab
but first ensure 'expandtab'
is reset (:verbose set ts? et?
is your friend). retab
takes a range, so I usually specify %
to mean "the whole file".
:set tabstop=2 " To match the sample file
:set noexpandtab " Use tabs, not spaces
:%retab! " Retabulate the whole file
在做这样的事情之前(尤其是 Python 文件!),我通常设置 'list'
,这样我就可以看到空格并进行更改.
Before doing anything like this (particularly with Python files!), I usually set 'list'
, so that I can see the whitespace and change.
我在 我的 .vimrc
为此:
I have the following mapping in my .vimrc
for this:
nnoremap <F2> :<C-U>setlocal lcs=tab:>-,trail:-,eol:$ list! list? <CR>
这篇关于如何在 Vim 或 Linux 中将空格转换为制表符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!