Membuat Shortcut Keyboard untuk Menjalankan Bash Script

calendar_today
schedule 2 min read

Membuat Shortcut Keyboard untuk Menjalankan Bash Script

Yo, peeps! Pernah nggak sih kalian capek ngetik perintah yang sama terus-terusan di terminal? Atau mungkin pengen ngejalanin script bash dengan sekali pencet tombol? Nah, shortcut keyboard bisa jadi solusinye! Dengan ngeset shortcut, bisa deh ngejalanin script bash tanpa ngetik command panjang-panjang. Bikin hidup lebih gampang kapok!

Step 1: Bikin Bash Script Dulu

Pertama-tama, pastiin dulu punya file bash script yang pengen dijalanin. Misal, kita bikin file hello.sh yang isinya:

code
#!/bin/bash
echo "Hello, World!"

Jangan lupa bikin filenya bisa dijalanin pake chmod:

code
chmod +x hello.sh

Step 2: Bikin Alias di .bashrc atau .bash_aliases

Alias tuh kayak shortcut buat command. Buka file .bashrc atau .bash_aliases (kalo ada) di home direktori.

code
nano ~/.bashrc

Terus, tambahin alias buat ngejalanin scriptnya. Misal:

code
alias hello="~/path/to/hello.sh"

Jangan lupa source file .bashrc biar aliasnya aktif:

code
source ~/.bashrc

Sekarang, bisa panggil script pake hello aja di terminal.

Step 3: Bikin Shortcut Keyboard dengan xbindkeys

Nah, kalo pengen ngejalanin script pake shortcut keyboard, kita bisa pake xbindkeys. Pertama, install dulu xbindkeys sama xdotool:

code
sudo apt install xbindkeys xdotool

Terus, bikin config file buat xbindkeys:

code
xbindkeys --defaults > ~/.xbindkeysrc

Buka file .xbindkeysrc:

code
nano ~/.xbindkeysrc

Terus, tambahin shortcut. Misal, pake Ctrl + Alt + H buat ngejalanin hello.sh:

code
"xdotool key --clearmodifiers Escape && xdotool key --clearmodifiers Return && xdotool type 'hello' && xdotool key Return"
  Control+Mod2 + h

Keterangan:

  • xdotool key --clearmodifiers Escape untuk pastiin nggak ada modifier key yang aktif.
  • xdotool key --clearmodifiers Return untuk simulasi ngetik Enter.
  • xdotool type 'hello' untuk ngetik hello (alias dari script).
  • xdotool key Return untuk nge-enter lagi biar script jalan.

Simpen file, dan restart xbindkeys:

code
pkill xbindkeys
xbindkeys

Sekarang, coba pencet Ctrl + Alt + H di keyboard. Kalo bener, hello.sh bakal jalan dan nampilin “Hello, World!” di terminal.

Step 4: Biar Shortcutnya Auto Start

Biar shortcutnya jalan tiap kali komputer nyala, tambahin xbindkeys ke startup applications. Caranya:

  1. Buka “Startup Applications” (biasanya ada di menu system).
  2. Klik “Add”.
  3. Isi:
    • Name: xbindkeys
    • Command: xbindkeys
    • Comment: Keyboard Shortcuts
  4. Klik “Save”.

Sekarang, setiap kali login, xbindkeys bakal jalan otomatis.

A

Written by Ariful

Full-stack engineer obsessed with web performance.