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 How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe