V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
wniming
V2EX  ›  Linux

less, diff 等命令把结果直接输出到终端时会把 tab 转换为空格,有办法阻止这些命令做转换吗?

  •  
  •   wniming · 202 天前 · 1353 次点击
    这是一个创建于 202 天前的主题,其中的信息可能已经有所发展或是发生改变。

    不仅这里命令,用 vim 打开文本,如果我想直接在终端软件(比如 gnome-terminal)里复制代码,tab 也会变为空格。

    我非常不理解这些这些程序为什么要怎么做,cat 命令就不会做转换。

    第 1 条附言  ·  202 天前
    更正一下,diff 不会把 tab 转换为空格,git diff 会
    第 2 条附言  ·  202 天前
    git 如果加上--no-pager 就不会把 tab 输出为空格,比如:
    git --no-pager diff
    21 条回复    2023-10-09 09:58:18 +08:00
    kneo
        1
    kneo  
       202 天前 via Android
    有没有可能是你终端的问题?
    rrfeng
        2
    rrfeng  
       202 天前
    因为 tab 是 control character
    cy18
        3
    cy18  
       202 天前
    用>重定向输出没问题,还是 tab 。
    终端内部处理应该也还是 tab ,但是输出到 GUI 渲染出来应该就是空格了。
    wniming
        5
    wniming  
    OP
       202 天前
    @rrfeng 具体一些
    wniming
        6
    wniming  
    OP
       202 天前
    @cy18 为啥 gui 要把 tab 渲染空格? cat 输出的 tab 就不会渲染成空格。
    cy18
        7
    cy18  
       202 天前
    @wniming #6 什么叫 cat 输出的 tab 就不会渲染成空格?
    在 1.txt 里面输几个 tab 。
    cat 1.txt ,终端上显示的就是空格,cat 1.txt> 2.txt ,2.txt 里面就是 tab 。
    less 跟 diff 也一样,less 1.txt > 3.txt ,3.txt 里面还是 tab 没变
    cy18
        8
    cy18  
       202 天前
    @wniming #6 你用的什么终端软件?可能我的终端不支持。
    wniming
        9
    wniming  
    OP
       202 天前
    @cy18 你不信的话看一下我 4 楼的回复里给出的第二个链接,人家问的问题跟我一样

    你用鼠标双击选中 cat 和 less 输出的空白区域就知道啥意思了。
    wniming
        10
    wniming  
    OP
       202 天前
    @cy18 gnome-terminal, xfce4-terminal, xterm 表现都完全一样,你用的是 linux 系统么?
    cy18
        11
    cy18  
       202 天前   ❤️ 1
    @wniming #10 不是,抱歉,打扰了,告辞
    rrfeng
        12
    rrfeng  
       202 天前
    man less 找到 character set 一节就可以看到。

    因为 less 是可交互的,tab 有特殊意义的。

    more 也不会,你把 git pager 设置成 more 就行了。

    为什么非要用 less 呢?
    wniming
        13
    wniming  
    OP
       202 天前
    @rrfeng less 是很常用的命令吧,比如用 less 在 terminal 里看代码,看到某一段想用鼠标直接拷贝出来粘贴到别处,vim 也一样。
    rrfeng
        14
    rrfeng  
       202 天前   ❤️ 1
    是很常用,但是它设计上就是会用 space 代替 tab ,接受这个设定就行了。
    libook
        15
    libook  
       202 天前
    https://man7.org/linux/man-pages/man1/less.1.html#NATIONAL_CHARACTER_SETS
    手册上说 TAB 属于 control characters ,不能被直接显示。

    因为 less 指令的输出最终还是从终端上显示出来的,而 control characters 在会被终端进行特殊处理,可能会导致最终显示的结果部分不受 less 指令的控制。

    类似的还有 ANSI 颜色,负责上色的并不是输出文本的指令,而是指令将颜色符号原样传给终端,终端在进行上色处理,所以你调整颜色方案是在终端上调,而不是在 shell 或者指令上面调。
    hugulas
        16
    hugulas  
       202 天前
    gpt 答案: 我验证了 less -R, 你试试

    -R or --RAW-CONTROL-CHARS
    Like -r, but only ANSI "color" escape sequences and OSC 8 hyperlink sequences are output in "raw" form. Unlike -r, the screen appearance is maintained correctly, provided that there are no escape sequences in the file other than
    these types of escape sequences. Color escape sequences are only supported when the color is changed within one line, not across lines. In other words, the beginning of each line is assumed to be normal (non-colored),
    regardless of any escape sequences in previous lines. For the purpose of keeping track of screen appearance, these escape sequences are assumed to not move the cursor.

    OSC 8 hyperlinks are sequences of the form:

    ESC ] 8 ; ... \7

    The terminating sequence may be either a BEL character (\7) or the two-character sequence "ESC \".

    ANSI color escape sequences are sequences of the form:

    ESC [ ... m

    where the "..." is zero or more color specification characters. You can make less think that characters other than "m" can end ANSI color escape sequences by setting the environment variable LESSANSIENDCHARS to the list of
    characters which can end a color escape sequence. And you can make less think that characters other than the standard ones may appear between the ESC and the m by setting the environment variable LESSANSIMIDCHARS to the list of
    characters which can appear.


    这个问题可能是由终端软件的显示设置引起的,而不是由具体的命令本身引起的。许多终端软件默认将制表符( Tab )转换为空格以实现对齐的效果。

    不过,你可以尝试按照下面的方法进行调整,使终端软件不对制表符进行转换:

    对于 less 命令,你可以尝试在终端中使用 -R 或 --RAW-CONTROL-CHARS 选项来显示控制字符,包括制表符。示例命令如下:
    less -R <file>
    对于 vim ,你可以编辑其配置文件 ~/.vimrc (如果不存在,可以创建一个),将下面的内容添加到文件中:
    set paste
    然后保存并退出,并重新启动 vim ,它将开启“粘贴模式”,在此模式下粘贴的文本将保留制表符。

    无论是 less 还是 vim ,它们的行为都受到终端软件的限制。所以,你还可以尝试以下方法来修改终端软件的显示设置:

    对于 GNOME Terminal:打开终端后,点击菜单栏的“编辑”选项,然后选择“首选项”。在“首选项”对话框中,导航到“选项”选项卡,取消勾选“将制表符转换为空格”选项。然后点击“关闭”按钮保存更改。

    对于其他终端软件(例如 Konsole 、Terminator 等):请参考其各自的文档或设置菜单,查找类似的选项以禁止制表符转换为空格。

    希望这些方法能够帮助你在终端中保留制表符的原始格式。
    wniming
        17
    wniming  
    OP
       202 天前
    @hugulas 都不管用
    comingnine
        18
    comingnine  
       202 天前 via Android
    输出到终端时和输出到文件时好像是设计为不一样啊
    comingnine
        19
    comingnine  
       202 天前 via Android
    制表符跳不少呢,原样输出的话,屏幕会放不开么,会因为这个而不原样输出么
    zhuisui
        20
    zhuisui  
       201 天前
    cat 也会渲染成空格呀,用 --show-tabs 会改为渲染成 ^I
    zhuisui
        21
    zhuisui  
       201 天前
    less 的话要用 --UNDERLINE-SPECIAL 选项
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1225 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:50 · PVG 01:50 · LAX 10:50 · JFK 13:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.