How to Add Keyboard Shortcuts for Command

To set up keyboard shortcuts for commands in the command line, you can follow these general steps:

  1. Determine your shell: Identify the shell you are using, such as Bash, PowerShell, or zsh. Different shells have different methods for defining keyboard shortcuts, so it’s important to know which shell you’re using.
  2. Locate the configuration file: Find and open the configuration file specific to your shell. The file names and locations can vary based on the operating system and shell you’re using. Here are some common ones:
  • Bash: ~/.bashrc or ~/.bash_profile
  • PowerShell: $PROFILE
  • zsh: ~/.zshrc

For Bash or zsh:

  1. Using a function:
   function hello() {
       echo "Hello, world!"
   }
  • Open your Bash configuration file, such as ~/.bashrc or ~/.bash_profile, using a text editor.If using zsh , open your zsh configuration file, such as ~/.zshrc, using a text editor.
  • Add the following code to define a function:
    function shortcut_name() { command } , in bash you can omit the function keyword and write it simply as shortcut_name( ){ command }
    Replace shortcut_name with the desired name for the keyboard shortcut, and command with the actual command you want to associate with the shortcut.
  • Save and close the configuration file.
  • Execute the command source .bashrc to reload the Bash configuration file (~/.bashrc) in the current shell session. It applies any changes made to the file immediately, allowing you to use the newly defined keyboard shortcuts or aliases without needing to open a new terminal window. Note that the source command is specific to Bash and may not work in other shells like PowerShell. In zsh, you can use source ~/.zshrc to reload the configuration file (~/.zshrc). In PowerShell, the equivalent command is . $PROFILE.
  • Test the keyboard shortcut by typing shortcut_name. Example: write hello and hit enter.
  1. Using an alias:
   alias hello='echo "Hello, world!"'
  • Open your Bash or zsh configuration file.
  • Add the following code to create an alias:
    alias shortcut_name='command'
    Replace shortcut_name with the desired name for the keyboard shortcut, and command with the actual command you want to associate with the shortcut.
  • Save and close the configuration file.
  • Reload the bash or zsh configuration file.
  • Test the keyboard shortcut by typing shortcut_name.

For PowerShell:

  1. Using a function:
   function hello {
       Write-Host "Hello, world!"
   }
  1. Using an alias:
   Set-Alias -Name hello -Value { Write-Host "Hello, world!" }
  • Open your PowerShell profile file.
  • Add the following code to create an alias:
    Set-Alias -Name shortcut_name -Value command
    Replace shortcut_name with the desired name for the keyboard shortcut, and command with the actual command you want to associate with the shortcut.
  • Save and close the profile file.
  • Reload the PowerShell session to apply the changes.
  • Test the keyboard shortcut by typing shortcut_name. Example:Type hello and hit enter.

Remember to adjust the command part in each example with the specific command you want to associate with the keyboard shortcut.

Also read some important command line commands to use

Don't Miss Out! Subscribe to Read Our Latest Blogs.

If you found this blog helpful, share it on social media.

Subscription form (#5)

Leave a Comment

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Scroll to Top