简体   繁体   English

如何在命令行中输入tab char?

[英]How to enter a tab char on command line?

In an interactive bash terminal how do I enter a tab character? 在交互式bash终端中,如何输入制表符? For example, if I wanted to use sed to replace "_" with tabs I'd like to use: 例如,如果我想使用sed将“_”替换为我想使用的标签:

echo $string | sed 's/[_]/TAB/g'

Where TAB means the tab key. 其中TAB表示Tab键。 This works in a shell script not interactively where when I hit the tab key I get no character and a clank noise sounds. 这不是交互式的shell脚本,当我按Tab键时,我没有任何字符和铮铮声。 I've also tried \\t but it only places t's in the string and not tabs. 我也试过\\t但它只将t放在字符串而不是标签中。

Note this is mac osx. 注意这是mac osx。

Control + V取代它,然后用Tab取消通常的扩展行为。

Since this question is tagged "bash"... using the "ANSI-C quoting" feature of the Bash shell is probably preferable. 由于这个问题被标记为“bash”...使用Bash shell的“ANSI-C引用”功能可能更可取。 It expands ANSI C-style escape sequences such as \\t and \\n and returns the result as a single-quoted string. 它扩展了ANSI C样式的转义序列,例如\\t\\n ,并将结果作为单引号字符串返回。

echo $string | sed $'s/_/\t/g'

This does not rely on your terminal understanding the Control + V (insert next character literally) key binding—some may not. 这不依赖于您的终端理解Control + V (字面插入下一个字符)键绑定 - 有些可能不会。 Also, because all the "invisible" characters can be represented literally, your solution can be copy-pasted without loss of information, and will be much more obvious/durable if you're using including this sed invocation in a script that other people might end up reading. 此外,因为所有“隐形”字符都可以按字面表示,所以您的解决方案可以在不丢失信息的情况下进行复制粘贴,并且如果您在脚本中使用此sed调用,则其他人可能会更加明显/持久最终阅读。

Also note that macOS's version of sed is the BSD version. 另请注意,macOS的sed版本是BSD版本。 GNU sed will interpret character escapes like \\t in the replacement pattern just fine, and you wouldn't even need above workaround. GNU sed会在替换模式中解释像\\t这样的字符转义就好了,你甚至不需要上面的解决方法。

You can install GNU sed with MacPorts , and it should be made available as gsed , but Homebrew might supersede your system's sed depending on how you have your $PATH variable arranged. 您可以使用MacPorts安装GNU sed,它应该可以作为gsed ,但Homebrew可能会取代您的系统的sed具体取决于您如何安排$PATH变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Shell命令行上输入退格字符? - How to enter a backspace character on a shell command line? OSX终端从命令行关闭当前选项卡没有提示? - OSX Terminal close current tab from command line without prompt? 从命令行打开新的终端选项卡 (Mac OS X) - Open new Terminal Tab from command line (Mac OS X) 如何将命令发送到给定的iTerm 2选项卡? - How to send a command to a given iTerm 2 tab? JupyterLab - 如何添加 Command(⌘) + Enter 以在 macOS 中运行单元格? (⌘ 是怎么称呼的?) - JupyterLab - How to add Command(⌘) + Enter for running a cell in macOS ? (How is ⌘ called?) 如何输入从Java代码运行的终端命令的输入 - How to enter input to terminal command which is run from java code 如何在python中打开CLI程序并输入命令(Mac OSX)? - How to open a CLI program in python and enter a command(Mac OSX)? 通过lein,命令行或emacs从命令选项卡应用程序切换器隐藏Clojure REPL - Hide Clojure REPL from command-tab application switcher via lein, command line, or emacs 如何构建命令行实用程序 - How to build command line utility 有什么方法可以重启通过终端(Mac终端)连接到USB端口的iOS设备并通过命令行输入小程序? - Any way to reboot an iOS Device that is connected to a USB port via terminal (Mac terminal) and enter the appleid through command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM