如何在Vim或Linux中将空格转换为制表符? [英] How can I convert spaces to tabs in Vim or Linux?

查看:602
本文介绍了如何在Vim或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.

我在命令提示符下尝试了expandunexpand,但没有任何运气.

I tried both expand and unexpand at the command prompt without any luck.

这是相关文件:

http://gdata-python-client.googlecode.com /hg-history/a9ed9edefd61a0ba0e18c43e448472051821003a/samples/docs/docs/docs_v3_example.py

如何使用Vim或Shell将前导空格转换为制表符?

How can I convert leading spaces to tabs using either Vim or the shell?

推荐答案

使用Vim扩展所有 leading 空间(比 % 表示整个文件".

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屋!

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