Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to hit F1 and have a string with return key to be typed automatically into the terminal I am in. I do not want to do it with bash_profile because I want it to be configured locally in once place not on every server I ssh in to.

How do I do that?

share|improve this question
    
I suggest you use xmacro, as seen in this reply: askubuntu.com/questions/150122/… –  roadmr Sep 13 '12 at 16:37
    
Better yet, tell us what you are trying to do. There are better ways to automatize things than using xmacro. –  January Sep 14 '12 at 5:22

1 Answer 1

OK, the answer that I have and which works almost exactly as requested is actually surprisingly complex. You will use xmacro to record and play back key strokes.

  1. Install xmacro

    sudo apt-get install xmacro
    
  2. Record a macro

    xmacrorec2 > test.macro
    

    Now press ESC (to record the "abort" key for recording), type whatever text you want to assign and press ESC again to stop the recording. The recorded macro is now in the file test.macro; you can edit it with a text editor.

  3. Test that macro works

    cat test.macro | xmacroplay -d 0 $DISPLAY
    
  4. (optional, recommended) Create a script with the above commands. Save it as macro.sh:

    #!/bin/bash
    
    cat test.macro | xmacroplay -d 0 $DISPLAY > /dev/null 2>/dev/null
    

    Make it executable:

    chmod a+x macro.sh
    
  5. A little problem remains. xmacro plays back the exact keystrokes, but if you define a shortcut, and run the script from the shortcut, the keys will be played before you release the shortcut key, as if you started typing without releasing the key. You need to prepend the following line to the test.macro file:

    KeyStrRelease F1
    

    which will simulate the release of the F1 key.

  6. Using keyboard preferences (you can access them through settings or by typing `gnome-control-center keyboard), create a new shortcut under "Custom shortcuts" and assign to it F1.

OK, but there is one more caveat: the F1 shortcut will now work in every application, not only the terminal. I don't know how to solve it; possibly, in the macro.sh one would have to test what window has the focus first and check whether the identifier of this window is terminal.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.