Saving commands to a file employing Ctrl-x-e
[ad_1]
A person incredibly appealing trick that you may not know is that you can type a line of text (presumably a command) on the Linux command line and straight away help save it to a file by pressing just 3 keys. The editor that will open up will depend on your $EDITOR placing that you can look at making use of the command shown under:
$ echo $EDITOR nano
If you favor to use a diverse editor, use a command like this prior to typing or transferring again to the command that you want to conserve:
$ export EDITOR=vi
And don’t neglect to conserve this alter to your .bashrc (or other start-up file) if you want to make this change long-lasting. For instance:
$ echo "EDITOR=vi" >> ~/.bashrc
As an example, say you have just composed a especially complicated command, verified that it works as you predicted, and now want to conserve it in a file so that you can use it any time you want with out acquiring to reinvent it. Is that challenging? No, it is really straightforward! Push the up-arrow vital as if you intend to run the command once more, but, as a substitute, keep the Ctrl vital and push both the “X” and “E” keys. This is often referred to as the Ctrl-x-e sequence. When you do this, the command ought to open in the editor.
In the illustration demonstrated down below, the textual content is displayed in nano. The instructions at the bottom of the monitor clearly show nano’s command choices. If you select ^O, you will be prompted to save the command in /tmp with a random file name. Back more than the instructed file identify and you can help save it to your residence or present spot in the file technique. It will request you to ensure the “diverse” file name. Following that, you can exit the editor and your command will be sitting down in a file for your afterwards use. You will, as you possible suspect, have to make the file executable due to the fact Linux will not do this devoid of a chmod command. It will search anything like this when you use nano:
echo This is a command that I want to save in a file ^G Aid ^O Produce Out ^W The place Is ^K Slash ^T Execute ^X Exit ^R Examine File ^ Switch ^U Paste ^J Justify
This is a very simple case in point. Say you utilised a command like the a person revealed down below to screen 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 conserve the command in a file just after managing, press the up arrow important without urgent the enter vital to get again to it.
$ cal | head -2 | tail -1
Future, enter the Ctrl-x-e sequence even though you happen to be however positioned at the conclusion of the line. Carrying out this will open the command in the editor. You can enter ^O to help save it, but if you back again over the suggested file title (e.g., /tmp/bash-fc.9RfMEe”) and kind in one thing like “times_of_week” or “~/days_of_7 days” if you happen to be not in your house listing, you can preserve it to your recent place in its place of to /tmp. Then type ^X to exit.
In truth, you can use this method for any command that is still in your command heritage. Use the up arrow to get again to the command and then deploy the Ctrl-x-e trick.
Wrap-up
There are many approaches to save instructions that you could want to reuse. You can style them into a file, use the echo command to insert them to a file, or develop an alias. The Ctrl-x-e trick tends to make conserving a command to a file approximately effortless.
Copyright © 2023 IDG Communications, Inc.
[ad_2]
Resource connection