如何在Vim或Linux中将空格转换为制表符? [英] How can I convert spaces to tabs in Vim or Linux?
问题描述
我已经查看了有关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扩展所有 leading 空间(比 retab
,但首先确保 %
表示整个文件".
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中将空格转换为制表符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!