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. Credit: Thinkstock / Lance Anderson 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. Related content how-to Compressing files using the zip command on Linux The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file. By Sandra Henry-Stocker May 13, 2024 4 mins Linux opinion NSA, FBI warn of email spoofing threat Email spoofing is acknowledged by experts as a very credible threat. By Sandra Henry-Stocker May 13, 2024 3 mins Linux how-to The logic of && and || on Linux These AND and OR equivalents can be used in scripts to determine next actions. By Sandra Henry-Stocker May 02, 2024 4 mins Linux how-to Using the apropos command on Linux By Sandra Henry-Stocker Apr 24, 2024 3 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe