游標位置 (CUP)

將游標移動到列 `y`、欄 `x`。

  1. 0x1B
    ESC
  2. 0x5B
    [
  3. ____
    y
  4. 0x3B
    ;
  5. ____
    x
  6. 0x48
    H

參數 y 和 x 必須是大於等於 1 的整數。 任何一個小於等於 0 時,將該參數調整為 1。

值 y 和 x 均從 1 開始編號。例如,最頂列是第 1 列, 畫面最左欄是第 1 欄。

此序列總是取消待定換行狀態。

如果 沒有設定 原點模式,游標會精確移動到 y 和 x 所指定的列和欄。y 的最大值是 畫面的最底列,x 的最大值是畫面的最右欄。

如果設定了 原點模式,游標位置相對於 捲動區域的左上角設定。y = 1 對應 上邊界,x = 1 對應 左邊界。 y 的最大值是 下邊界,x 的最大值 是 右邊界。

設定了原點模式時,使用此序列無法將游標位置設定到 捲動區域邊界之外。

驗證

CUP V-1:一般用法

printf "\033[1;1H" # move to top-left
printf "\033[0J" # clear screen
printf "\033[2;3H"
printf "A"
|__________|
|__Ac______|

CUP V-2:畫面之外

printf "\033[1;1H" # move to top-left
printf "\033[0J" # clear screen
printf "\033[500;500H"
printf "A"
|__________|
|__________|
|_________Ac

CUP V-3:相對於原點

printf "\033[1;1H" # move to top-left
printf "\033[0J" # clear screen
printf "\033[2;3r" # scroll region top/bottom
printf "\033[?6h" # origin mode
printf "\033[1;1H" # move to top-left
printf "X"
|__________|
|X_________|

CUP V-4:相對於原點(含左/右邊界)

printf "\033[1;1H" # move to top-left
printf "\033[0J" # clear screen
printf "\033[?69h" # enable left/right margins
printf "\033[3;5s" # scroll region left/right
printf "\033[2;3r" # scroll region top/bottom
printf "\033[?6h" # origin mode
printf "\033[1;1H" # move to top-left
printf "X"
|__________|
|__X_______|

CUP V-5:捲動區域與原點模式的限制

printf "\033[1;1H" # move to top-left
printf "\033[0J" # clear screen
printf "\033[?69h" # enable left/right margins
printf "\033[3;5s" # scroll region left/right
printf "\033[2;3r" # scroll region top/bottom
printf "\033[?6h" # origin mode
printf "\033[500;500H" # move to top-left
printf "X"
|__________|
|__________|
|____X_____|

CUP V-6:待定換行被取消

cols=$(tput cols)
printf "\033[${cols}G" # move to last column
printf "A" # set pending wrap state
printf "\033[1;1H"
printf "X"
|Xc_______X|