[英]How to set 4 space tab in bash
VIM里面好像是set tabstop=4
,但是不知道bash里面怎么設置
例如:
echo -e "1234567890\t321\n1\t2\n123\t1"
當前 output:
1234567890 321
1 2
123 1
我想要這樣的 output:
1234567890 321
1 2
123 1
它可以在任何地方顯示,就像cat somefile
或php -r 'echo "\t123";'
如何在 bash 中設置制表符寬度?
這不是您的 shell(或 php 或 cat)的屬性。 管理輸出的是您的終端。
使用tabs
命令更改行為:
$ tabs 4
$ echo -e "a\tb"
a b
$ tabs 12
$ echo -e "a\tb"
a b
( tabs
符在 POSIX 中指定,上面的輸出是“偽造的”:它仍然是兩個字母之間的制表符。)
您可以使用tabs
實用程序設置規則或不規則間隔。 無論您是進行自己的輸出,使用cat
輸出已經包含選項卡的文件還是使用您無法控制的程序的輸出,它都將起作用。
但是,如果您要控制輸出,最好使用printf
而不是echo
和格式字符串而不是制表符。
$ printf '%-12s%8.4f %-8s%6.2f\n' 'Some text' 23.456 'abc def' 11.22
Some text 23.4560 abc def 11.22
$ format='%*s%*.*f %*s%*.*f\n'
$ printf "$format" -12 'Some text' 8 4 23.456 -8 'abc def' 6 2 11.22
Some text 23.4560 abc def 11.22
除非您希望其他人能夠使用tabs
實用程序控制程序的輸出。
您可以使用 setterm 來設置它
setterm -regtabs 4
我把它放在我的 .bash_profile 中,但它不是專門與 bash 相關的
tabs 4
導致以下制表位位置。 這不是你所要求的。
tab stop positions 4,8,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68,72,76,80
1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
* * * * * * * * * * * * * * * * * * * *
你問這個..
tab stop positions 5,9,13,17,21,25,29,33,37,41,45,49,53,57,61,65,69,73,77,80
1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
* * * * * * * * * * * * * * * * * * * *
使用單個數字指定選項卡會創建一個從 0 開始的隱式列表。
創建一個明確的列表,例如您所要求的。 提供逗號或空格分隔的制表位位置列表。
像這樣: tabs 5,9,13,17,21,25,29,33,37,41,45,49,53,57,61,65,69,73,77
有關更多詳細信息,請參閱man tabs
和tabs -v
。
這對我有用。
~/.bash_profile
# Set the tab stops
if [ -f ~/.bash_tab_stops ]; then
. ~/.bash_tab_stops
fi
~/.bash_tab_stops
tab_width=4
terminal_width=$( stty size | awk '{print $2}' )
set_tab_stops() {
local tab_width=$1 terminal_width=$2 tab_stops=''
for (( i=1+$tab_width; $i<$terminal_width; i+=$tab_width )); do
tab_stops+=$i','
done
tabs $tab_stops
}
set_tab_stops $tab_width $terminal_width
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
PuTTY Release 0.73 Build platform: 64-bit x86 Windows
Linux VPS 3.10.0-1127.18.2.el7.centos.plus.x86_64
如果您想要第一個選項卡的偏移量(例如git diff
),您可以使用 shell 擴展來獲取步進數字:
tabs {5..300..4}
在上面,5 是第一個標簽的寬度,第二個數字是您的終端寬度,最后一個是您的常規標簽寬度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.