AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1647887
Accepted
xpt
xpt
Asked: 2021-05-10 07:53:20 +0800 CST2021-05-10 07:53:20 +0800 CST 2021-05-10 07:53:20 +0800 CST

在终端处理 NBSP

  • 772

如何在终端中处理 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

对不起卡米尔,我应该更加小心你的建议。

linux command-line
  • 1 1 个回答
  • 340 Views

1 个回答

  • Voted
  1. Best Answer
    Kamil Maciorowski
    2021-05-10T11:34:32+08:002021-05-10T11:34:32+08:00

    初步说明

    • 此答案适用于编码为\302\240(以八进制表示)的 NBSP。\302\240用于 Unicode。在 iso8859-1 中,它是唯一的\240(\xa0参见这个答案)。如果需要,调整解决方案。
    • 我的解决方案会影响涉及 Readline 的交互式使用。他们不会在脚本中修复不需要的 NBSP。如果您需要修复脚本,请使用sed或实际修复脚本中的代码。
    • 解决方案应该适用于键入的命令。对于粘贴的命令,如果enable-bracketed-paste是off(检查bind -v | grep bracketed),它们将起作用。覆盖的解决方案Enter将在键入时起作用Enter,即使enable-bracketed-paste是on并且命令已粘贴。

    全球解决方案

    从Ask Ubuntu 上的这个答案:

    将以下内容放入.inputrc[…]:

    […]

    # map NBSP to regular space (left part has NBSP in quotes, right part has space)
    " ":" "
    

    如果降价搞砸了上述

    确实如此!

    如果降价搞砸了上述内容,您必须按照评论中的说明将 NBSP 放在冒号左侧的引号中。这将使用常规空间映射输入流上任何出现的 NBSP。

    Readline 应该明白\302\240。以下版本不会被markdown破解,直接复制即可:

    "\302\240":" "
    

    请注意,此解决方案可能会影响任何使用 Readline 的程序,并且可能没有简单的方法可以根据程序的需要禁用该解决方案。在 Bash 中,您可以通过运行以下命令禁用它:

    bind -r '\302\240'
    

    并通过以下方式重新启用:

    bind '"\302\240":" "'
    

    单独的 Bash 实例可以单独禁用绑定。


    仅适用于 Bash

    如果你想影响 Bash 而不是其他使用 Readline 的程序,那么把它放在你的~/.bashrc:

    bind '"\302\240":" "'
    

    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:

    _fix_nbsp () { READLINE_LINE="${READLINE_LINE//$'\302\240'/ }"; }
    

    Bind it to some unused combination, e.g. to Ctrl+x,j:

    bind -x '"\C-xj": _fix_nbsp'
    

    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:

    bind '"\C-m": "\C-xj\C-j"'
    

    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 like sed, 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。

    • 3

相关问题

  • Python 的“pass”参数的批处理等价物是什么?

  • 禁用后无法启用 Microsoft Print to PDF

  • 以 root 身份运行 docker 容器

  • 如何在域和 Linux 活动目录中启用指纹传感器

  • 如何在CentOS 7 中将Ctrl+C 永久更改为Ctrl+K?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve