Americas

  • United States
sandra_henrystocker
Unix Dweeb

Saving commands to a file using Ctrl-x-e

How-To
Mar 09, 20234 mins
Linux

The Ctrl-x-e key sequence provides a quick and easy way to save commands you've recently used on the command line into files.

One very interesting trick that you may not know is that you can type a line of text (presumably a command) on the Linux command line and immediately save it to a file by pressing just three keys. The editor that will open up will depend on your $EDITOR setting that you can view using the command shown below:

$ echo $EDITOR
nano

If you prefer to use a different editor, use a command like this before typing or moving back to the command that you want to save:

$ export EDITOR=vi

And don’t forget to save this change to your .bashrc (or other start-up file) if you want to make this change permanent. For example:

$ echo "EDITOR=vi" >> ~/.bashrc

As an example, say you’ve just composed a particularly complex command, verified that it works as you expected, and now want to save it in a file so that you can use it any time you want without having to reinvent it. Is that hard? No, it’s easy! Press the up-arrow key as if you intend to run the command again, but, instead, hold the Ctrl key and press both the “X” and “E” keys. This is often referred to as the Ctrl-x-e sequence. When you do this, the command should open in the editor.

In the example shown below, the text is displayed in nano. The commands at the bottom of the screen show nano’s command options. If you select ^O, you will be prompted to save the command in /tmp with a random file name. Back over the suggested file name and you can save it to your home or current location in the file system. It will ask you to confirm the “different” file name. After that, you can exit the editor and your command will be sitting in a file for your later use. You will, as you likely suspect, have to make the file executable since Linux doesn’t do this without a chmod command. It will look something like this when you use nano:

echo This is a command that I want to save in a file


^G Help        ^O Write Out   ^W Where Is    ^K Cut         ^T Execute
^X Exit        ^R Read File   ^ Replace     ^U Paste       ^J Justify

Here’s a simple example. Say you used a command like the one shown below to display the days of the week as they are output by the cal command:

$ cal | head -2 | tail -1
Su Mo Tu We Th Fr Sa

To save the command in a file after running, press the up arrow key without pressing the enter key to get back to it.

$ cal | head -2 | tail -1

Next, enter the Ctrl-x-e sequence while you’re still positioned at the end of the line. Doing this will open the command in the editor. You can enter ^O to save it, but if you back over the suggested file name (e.g., /tmp/bash-fc.9RfMEe”) and type in something like “days_of_week” or “~/days_of_week” if you’re not in your home directory, you can save it to your current location instead of to /tmp. Then type ^X to exit.

In fact, you can use this technique for any command that is still in your command history. Use the up arrow to get back to the command and then deploy the Ctrl-x-e trick.

Wrap-up

There are many ways to save commands that you might want to reuse. You can type them into a file, use the echo command to add them to a file, or create an alias. The Ctrl-x-e trick makes saving a command to a file nearly effortless.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author