You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除文件中仅在vi编辑器可见的^[[0m控制字符?

Fixing ANSI Control Characters (^[[0m) in Your File

Hey there! Let's sort out that pesky ^[[0m control character showing up in vi—those are ANSI escape codes used for text formatting, which cat automatically handles (so they don't show up) but vi displays as raw characters. Here are a few simple, reliable ways to remove them:

  • Use sed to clean the file in one go
    This command will globally remove all ANSI formatting escape codes from your file (not just ^[[0m, but any similar ones like bold/color codes):

    sed -i 's/\x1b\[[0-9;]*m//g' test.txt
    

    Breakdown: \x1b represents the ESC character (the start of all ANSI escape codes), \[[0-9;]*m matches the rest of the format sequence, and //g replaces every occurrence with nothing.

  • Clean it directly in vi
    If you prefer doing it right in the editor, open test.txt with vi then run this substitution command:

    :%s/\x1b\[[0-9;]*m//g
    

    Hit Enter, then save with :w—your file will be free of those control characters immediately.

  • Alternative: Use tr for quick removal (limited to ESC-based codes)
    If you only need to strip the ESC character and its following sequence, you can use tr to delete all ESC characters:

    tr -d '\033' < test.txt > temp.txt && mv temp.txt test.txt
    

    Note: This is less thorough than sed since it only removes the ESC character, but it works if your only issue is the ^[[0m sequence.

After running any of these, open the file with vi again—those annoying ^[[0m strings should be gone, and cat will still display the content as expected!

内容的提问来源于stack exchange,提问作者Sanjay Shitole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:38:58