Shell自定义快捷键方法详解

首先,我们要学会查看 Shell 中已有快捷键的方法,如下所示:

[root@localhost ~]# stty -a
#查询所有的快捷键
speed 38400 baud; rows 21; columns 1.04; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>;
start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time =0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -cdtrdsr
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc —ixany -imaxbel
-iutf8
opost -Olcuc -ocrnl onlcr -onocr -Onlret -ofill -ofdel nlO crO tabO bsO vtO ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke


"-a"选项用于査询系统中所有可用的快捷键,可以看到,"Ctrl+C"用于强制中止,"Ctrl+D"用于中止输入。那么,这些快捷键可以更改吗?

当然可以,只需执行以下命令即可:

[root@localhost ~] # stty 关键字 快捷键
例如:
[root01ocalhost ~]# stty intr ^p
#定义Ctrl +P快提键为强制中止,"^"字符只需手工输入即可
[root@localhost ~】# stty -a
speed 38400 baud; rows 21; columns 104; line = 0;
intr = ^P; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>;
start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
#强制中止的快提键变成了Ctrl+P