bash 如何搜索不可见的控制字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23103466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:13:14  来源:igfitidea点击:

How to search for invisible control characters

linuxbashvimsublimetext2

提问by rodrigo

I know there are some threads about this on stackoverflow but when i write ":set list" in the editor, it seems to display hidden characters but it doesnt display the hidden characters in the code we are having problems with.

我知道在 stackoverflow 上有一些关于此的线程,但是当我在编辑器中编写“:set list”时,它似乎显示隐藏字符,但它没有显示我们遇到问题的代码中的隐藏字符。

Some times now we have had some invisible symbols in our code making if loops break, i dont know how the symbols get there except from that some wierd keyboard combination much have been accidentally typed in. The code itself looks correct but the invisible symbol breaks it.

有时现在我们的代码中有一些不可见的符号,如果循环中断,我不知道这些符号是如何到达那里的,除了意外输入了一些奇怪的键盘组合。代码本身看起来是正确的,但不可见的符号会破坏它.

I have searched online about this but all i can find seems to be the ":set list" command in vim in addition to have to change the color of the hidden characters, but while this seems to display some hidden characters it doesnt display the problematic ones. We are getting two symbols which looks like a cross and one looks like a pistol. We have also tried to add the "draw_white_space" setting in sublime text but this only seems to display, well, whitspace like it says but the result was shown on google for showing hiden characters so i gave it a try.

我已经在网上搜索过这个,但我能找到的似乎是 vim 中的“:set list”命令,此外还必须更改隐藏字符的颜色,但是虽然这似乎显示了一些隐藏字符,但它并没有显示有问题的那些。我们得到了两个看起来像十字架的符号,一个看起来像手枪。我们还尝试在 sublime 文本中添加“draw_white_space”设置,但这似乎只显示,好吧,就像它所说的那样,但结果显示在谷歌上以显示隐藏字符,所以我试了一下。

The only way we have been able to see where the symbols are is with the DiffMerge tool, we have not been able to see these symbols in any other editor but we have actually been able to copy the sign to its own file and grep through all the files with the -f grep option which works, but it would be easier to display the characters in vim but using a keybinding.

我们能够看到符号在哪里的唯一方法是使用 DiffMerge 工具,我们无法在任何其他编辑器中看到这些符号,但实际上我们已经能够将符号复制到它自己的文件中并通过所有的 grep带有 -f grep 选项的文件可以工作,但在 vim 中显示字符会更容易,但使用键绑定。

Does someone have any suggestions? This is causing us to use a lot more time debugging the code when the problems is an invisible symbol.

有人有什么建议吗?当问题是一个不可见的符号时,这导致我们使用更多的时间来调试代码。

回答by rodrigo

Try the following search command:

尝试以下搜索命令:

/[^ -~<09>]

(you get the <09>by pressing the tab key). Or if you want to get rid of those nasty tabs, just:

(您可以<09>通过按 Tab 键获得)。或者,如果您想摆脱那些讨厌的标签,只需:

/[^ -~]

That will find and highlight any non-ASCII or control-ASCII character.

这将找到并突出显示任何非 ASCII 或控制 ASCII 字符。

If you still have hidden characters out there, you can try this command before the search:

如果您仍然有隐藏字符,您可以在搜索之前尝试使用以下命令:

:set enc=latin1

That will prevent any weird Unicode character to show up in your code.

这将防止任何奇怪的 Unicode 字符出现在您的代码中。