I want to write a shell script that does what a recorded macro would do and replay key-presses. In particular, to produce ctrl+a.

Is this possible?

I've tried things like echo \0141, but it just says there's no command \0141.

  • Why do you want to reinvent the wheel?
    – Chris S
    Jul 2, 2010 at 13:14
  • 1
    why is this reinventing the wheel?
    – interstar
    Jul 2, 2010 at 13:35
  • 3
    I would think a better explanation of what your overall goal was might get you more information or ideas...
    – peelman
    Jul 2, 2010 at 13:46

4 Answers 4


Have you tried Expect?


In X11, you might be able to make use of xdotool.

xdotool key ctrl+a

When an app is running it often reset terminal characteristics, so that pressing ctrl-a may actually be recorded as ASCII 1. Or a series of keystrokes.

In a console ctrl-v ctrl-a produces the ^A display and you get

 echo ^A | od -x
0000000 010a

where 01 is ctrl-a.

So, the point is:

in order to"script" something you have to capture terminal settings, remember them, then reset them during replay. In a shell script this means parsing the output of

stty -a

and saving it in a format that lets you send it back to stty during replay, then send your data stream to the terminal.

This is greatly complicated by terminal drivers, graphical interfaces and so on.

To get a "pure" ctrl-a use ctrl-v ctrl-a - only in console, not necessarily inside an editor.


From a documentation page I made a short while ago

Serge got pretty tired of the standard steps of web development:

  1. Save File
  2. Switch to browser (whatever shortcut.. usually alt+tab)
  3. Refresh page
  4. Look of if page is ok
  5. Come back to editor

So he decided to automate things a bit ;)

How he improved the situation:

  1. Install xdotool

$ sudo apt-get install xdotool

  1. Create xdotool script
$ mkdir Documents/scripts
$ vim Documents/scripts/vim-browser-vim.sh

# Seach for a window having "Coding" in its title and send keys to save current document in vim
xdotool search "Coding" key "Escape" "colon" "w" "Return"

# Search for a window having "Studyladder" in its title, activate it and refresh (In chrome it's Ctrl+r) 
xdotool search "Studyladder" windowactivate key "Ctrl+r"

# Seach for a window having "Coding" in its title and activate it
xdotool search "Coding" windowactivate
chmod u+x  Documents/scripts/vim-browser-vim.sh
  1. Connect script to shortcut

In GNOME: Taskbar > System > Preferences > Keyboard Shortcuts > Add

Name: vim-browser-vim Command: /home/srivest/Documents/scripts/vim-browser-vim.sh

Then Apply.

Next assign the shortcut key of your choice. I used Ctrl+2, I just feel that finger is lazy and needs a workout ;)

Click Close.

  1. Change the name of your console to "Coding" so the xdotool script can find it.

Now you can execute the script by pressing the shortcut!

Life changing! hehe..



If the script is not working, the things you have to look for:

  1. Is xdotool installed
  2. Is the script executable
  3. Are all the windows present, are their title bar matching the search statement in the script. For instance, in the example, I search for studyadder. If the browser tab is not opened on a studyladder page, the title at the top won't say studyladder and the window will not be found.
  4. Many windows with the same name... to avoid. You can look into xdotool stacks if you're so eager.
  5. The script path is correct in the GNOME shortcut.

PS: DO NOT TRY TO GENERATE ALT+TAB AND OTHER DESKTOP COMBO KEYS. This will actually work in the console but not when launched by the gnome shortcut system. I am assuming here that this shortcut code launches the script into its own sandbox and generating things like Alt+Tab to switch application won't work as there are no applications in that sandbox. Hours where wasted trying to make that work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .