如何在终端中处理 NBSP?
以下是我将完全相同的字符串(由前导 NBSP 组成)粘贴到终端的行为:
$ echo abc
bash: echo: command not found
$ echo abc
abc
$ echo abc
-bash: $'\240echo': command not found
$ ls ~/.inputrc
ls: cannot access '/root/.inputrc': No such file or directory
$ ls ~/.bash*
/root/.bash_history /root/.bashrc
$ cat ~/.bashrc
. . . basically nothing there . . .
$ bind -v | grep bracketed
set enable-bracketed-paste on
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux bullseye/sid
Release: testing
Codename: bullseye
$ uname -rm
5.10.0-6-amd64 x86_64
NBSP 来自我正在使用的文档,无论是 LibreOffice Writer 还是 google doc 等。所以我非常需要让这个粘贴 NBSP 工作(作为我的第二种情况),因为那是我记录我的命令的地方,即我不能改变他们的行为。
所以,
- 对于这三种情况,我没有触及任何系统默认设置或我的
- 在我的第二种情况下,工作中,我有
LANG=C
. 其他两个都基于 utf8LANG
- 但是,即使我更改为
LANG=C
其他两个,我仍然会遇到同样的错误
此答案中的“方法1”对我不起作用:
并且该bind '"\302\240":" "'
方法对我也不起作用(这是一台root
目前只有用户的新机器,因此提示可能看起来很奇怪):
我猜bind '"\302\240"
是与在终端中按下那个键相关联,而我只是在粘贴。只是猜测。
$ echo $'\240echo abc' | grep -a $'\240'`
echo abc
nbsp=`echo $'\240'`
$ echo "${nbsp}echo abc${nbsp}${nbsp}${nbsp}def" | grep -a $'\240'
echo abc def
bind "${nbsp}":" "
# then paste in the above echo output,
$ od -c -
echo abc def
0000000 240 e c h o a b c 240 240 240 d e f \n
# paste again to bash, which will become:
$ echo abcdef
abcdef
# i.e., all the spaces have been eaten, instead of mapped as " "
$ tail -5 /etc/inputrc
# map NBSP to regular space (left part has NBSP in quotes, right part has space)
"\240":" "
"\302\240":" "
"Â ":" "
$ tail -4 /etc/inputrc | od -c -h
0000000 " \ 2 4 0 " : " " \n " \ 3 0 2
0000020 \ 2 4 0 " : " " \n " 302 240 " : "
0000040 " \n " 342 220 243 " : " " \n
所有上述命令都在 中完成xterm
,除了黑屏命令,它来自urxvt
(提供302 240
od -c -
输出)。
结论:
非常感谢卡米尔,他一直以来的帮助。“现在是我住的地方的午夜”,他说,但接着说“我会在几个小时后检查这个问题”。这是确切的绑定命令,bind 'set enable-bracketed-paste off'
这是我需要的最后一英里。
bind -r '\240'
bind '"\240":" "'
bind 'set enable-bracketed-paste on'
$ echo abc def
-bash: $'\240echo': command not found
bind 'set enable-bracketed-paste off'
$ echo abc def
abc def
对不起卡米尔,我应该更加小心你的建议。
初步说明
\302\240
(以八进制表示)的 NBSP。\302\240
用于 Unicode。在 iso8859-1 中,它是唯一的\240
(\xa0
参见这个答案)。如果需要,调整解决方案。sed
或实际修复脚本中的代码。enable-bracketed-paste
是off
(检查bind -v | grep bracketed
),它们将起作用。覆盖的解决方案Enter将在键入时起作用Enter,即使enable-bracketed-paste
是on
并且命令已粘贴。全球解决方案
从Ask Ubuntu 上的这个答案:
确实如此!
Readline 应该明白
\302\240
。以下版本不会被markdown破解,直接复制即可:请注意,此解决方案可能会影响任何使用 Readline 的程序,并且可能没有简单的方法可以根据程序的需要禁用该解决方案。在 Bash 中,您可以通过运行以下命令禁用它:
并通过以下方式重新启用:
单独的 Bash 实例可以单独禁用绑定。
仅适用于 Bash
如果你想影响 Bash 而不是其他使用 Readline 的程序,那么把它放在你的
~/.bashrc
:Commands to disable and re-enable are exactly like in the previous solution.
Dynamically
This function will turn all NBSPs in the command line into regular spaces:
Bind it to some unused combination, e.g. to Ctrl+x,j:
Now you can sanitize the command line by typing Ctrl+x,j.
It may be useful to make Bash do this automatically upon Enter. Normally Enter sends Ctrl+m (verify by Ctrl+v,Enter, you should see
^M
), but Ctrl+j should work as well. So let's make Enter send Ctrl+x,j to trigger our function and then Ctrl+j to execute the result:Ctrl+j typed manually will still allow you to execute the command line without calling the function. Remember the function turns all NBSPs into regular spaces. If your code contains NBSPs that should survive then you need to fix other (i.e. unwanted) NBSPs manually and execute with Ctrl+j.
Notes:
To make the solution permanent, add the function and the
bind …
command(s) to your~/.bashrc
.If you choose to override Enter then the function will be run even if Enter is going to insert a newline after
\
or inside a quoted string (so without execution yet). This shouldn't break things as long as you want NBSPs to be converted. Still running the function multiple times when the final run alone would do the whole job is inelegant. On the other hand the function does not call external utilities likesed
, it does its job totally inside Bash, so even calling it multiple times in vain from time to time is not that bad.但是,如果您在 NBSP 应该存在的位置粘贴多行命令(因此您将按照上面的建议使用Ctrl+ j),那么粘贴的换行符将触发已粘贴片段的功能。要解决此问题,请调查括号中的 paste。