Ron
Ron

Reputation: 8176

How to make Gedit run a custom command when I save a certain type of file?

For example, for source code files, I want gedit to lint it when I save it.

The "run lint" part is done, via the External Tools plugin. Now can I make it run automatically everytime I save a source code file?

Upvotes: 1

Views: 1609

Answers (2)

jep
jep

Reputation: 387

You could save your file via your external tool running lint. There's a field Save with the following options: Nothing, Current document and All documents. Saving is done before the tool is run.

Here's a screenshot:

External Tools Manager (gedit)
(source: gnome.org)

Upvotes: 3

Amanda
Amanda

Reputation: 12747

I've been playing with a variation on this -- I want one keyboard shortcut to run a tool that will tidy Python, CSS or HTML, depending on the file type. But if you map ctrls to an external tool that runs lint if the file type matches, doesn't if it doesn't, and saves at the end no matter what. Something like this:

if [ $GEDIT_CURRENT_DOCUMENT_TYPE = "text/x-python" ]; then
    # Run lint
elif [ $GEDIT_CURRENT_DOCUMENT_TYPE = "text/html" ]; then  
    # Run tidy
elif [ $GEDIT_CURRENT_DOCUMENT_TYPE = "text/css" ]; then
    # run csstidy
else 
    cat -
    echo "Type is:" $GEDIT_CURRENT_DOCUMENT_TYPE > /dev/stderr
    echo "so I'm not doing a thing." > /dev/stderr
fi

NB. If your external tool is set to replace your document when it runs, you want to add cat - to read the original back into gedit so you don't just clobber it. If your output is all going to the Shell Output pane, you won't need that.

Upvotes: 1

Related Questions