Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need assistance in creating a script that does the following:

  1. Open Gedit
  2. Open a document with Gedit
  3. Save the document twice
  4. Close Gedit

I was wondering how can I make it happen with Gedit and xdotool, as xdotool can simulate the key combination to save a document Ctrl+S

share|improve this question
up vote 6 down vote accepted

The window must be focused because otherwise gedit will ignore X events sent by xdotool.

Here's what your script should be:


gedit -s /path/to/document
# -s is standalone, meaning a new window will be opened instead of a tab in any existing gedit

sleep 5 # wait for gedit to open, adjust if necessary

GEDPID=$!  # save PID of gedit process

sleep 2 # wait for gedit to actually open document, adjust if necessary

GEDWINID=`xdotool search --pid $GEDPID | tail -1`
# gets window ID of gedit, the third "child" is the actual window

xdotool windowactivate --sync $GEDWINID key --clearmodifiers --delay 100 ctrl+s ctrl+s alt+F4
# focus gedit window, wait until focused, then save twice and exit
share|improve this answer

Your Answer


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.