Creating Keyboard Shortcuts

A number of keyboard shortcuts are at your disposal. You learned about several of them in Useful Shortcuts. For example you learned that Ctrl+l clears the screen, and Ctrl+a jumps to the beginning of the line. Use the bind -p command to see a list of all the defined commands available. It’s a long list, so use less:

 $ ​​bind​​ ​​-p​​ ​​|​​ ​​less
 "C-g": abort
 "C-xC-g": abort
 "eC-g": abort
 "C-j": accept-line
 "C-m": accept-line
 ...

In addition to the ones already defined, you can make your own. Try it out by using the bind command to map Ctrl+t to execute the pwd command:

 $ ​​bind​​ ​​'"C-t": "pwd "'

Note a couple things about this. First, you need single quotes around the keybinding definition. This prevents any shell expansion or evaluation. Second, you need quotes around the keybinding and the printed value. Single quotes around the pair, double quotes around each part, with a colon separating the pair.

Press Ctrl+t and you’ll see the current working directory printed to the screen.

The bind command takes the key combination and maps it to a string of characters. In this case, we used pwd because you want the line break included so the command executes. Without the , it would just put pwd on the same line. That’s useful though. Remember that to redirect the standard error stream to the standard output stream, you use the sequence 2>&1. You can make a keybinding to add this quickly. Let’s make Ctrl+t insert those characters instead. The command is similar; you just don’t include the at the end:

 $ ​​bind​​ ​​'"C-t": " 2>&1"'

Note the space that is in front of the 2>&1 bit. It is a literal space, so when you trigger this keyboard shortcut, it will put that space in. This way you can just type a command, press Ctrl+t, and it will insert all of the characters, including the space.

You do have to be careful you don’t override built-in shortcuts you rely on, though. For example, you know that Ctrl+l clears the screen. But if you bind another keystroke to that key combo, your new one takes over. In addition to the Ctrl key, you have a few other options for defining shortcuts.

Let’s create a keybinding which sets the F1 key to insert the 2>&1 sequence. To do that, you need the character sequence for the F1 key. Terminal software doesn’t send keys to the shell; it sends character data, and each keystroke sends different data depending on the configuration of your terminal. Some keys, like F1, send a sequence of characters, starting with an escape character.

At the prompt, press Ctrl+v, followed by the F1 key to view the character sequence for F1. The characters ^[OP appear on the screen. The first two characters, ^[ represent the escape character. The rest of the sequence is the value for F1.

For the keybinding, replace ^[ with e. Define the new keystroke like this:

 $ ​​bind​​ ​​'"eOP": " 2>&1"'

Now press F1 and you’ll see the results on the screen.

Use Ctrl+v to view the character sequence for any special key so you can create your own bindings. Some keys, like F12, show results like ^[[24~. Note the two opening square braces in a row. In this case, ^[ is the escape character and [24~ is the value.

In addition to using Ctrl and other special keys, you can create a shortcut that starts with one of these options:

  • \ for a backslash.
  • " for a double quote.
  • for a single quote.

Instead of using a keybinding for 2>&1, you’ll create a shortcut that looks for the characters eout and converts them to 2>&1. It’s the same number of characters, but easier to remember. Here’s how you do it:

 $ ​​bind​​ ​​'"\eout": " 2>&1"'

Test it out. Type eout and the text appears. When you type the backslash, you won’t see the rest of the characters. You also have a limited amount of time to type the characters following the backslash before the shell just assumes you want a literal backslash.

One downside to these prefixes is that it does cause a slight delay when typing the prefix character. For that reason, I don’t recommend using double or single quotes, since you’ll want to use those often.

All the commands you’ve entered so far will go away as soon as you close your session. To persist them, add them to your .bashrc file like you would with aliases or environment settings.

Now, let’s add a keybinding to the environment permanently. Open your ~/.bashrc file:

 $ ​​nano​​ ​​~/.bashrc

In the file, add this keybinding that inserts the text >> output.txt when you press Ctrl+x Ctrl+o.

 bind ​'"C-xC-o": " >> output.txt'

This keybinding requires a little more work to use, but it demonstrates that you can create more complex keybindings.

Finally, let’s add one more keybinding you might find useful. If you’re in the middle of typing a command, and you realize you need to prefix it with sudo, you could press Ctrl+a to jump to the beginning of the line, type sudo, a space, and then press Ctrl+e to jump to the end.

Or you can create a keybinding to do that for you when you press Alt+s. Add this to your .bashrc file:

 bind ​'"es": "C-asudo C-e"'

This keybinding illustrates that it’s possible to have one binding activate other keybindings, which means you can compose more complex macros from smaller ones.

Save the file, exit the editor, and source the ~/.bashrc file to apply the changes to your current session. Then try it out. Type the ls command, and then press Alt+s or Esc+s and the command will be prefixed with sudo.

You can customize your environment in another place, and it will affect more than just your shell.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.111.195