Friday, March 10, 2023
HomeNetworkingSaving instructions to a file utilizing Ctrl-x-e

Saving instructions to a file utilizing Ctrl-x-e


One very attention-grabbing trick that you could be not know is which you can sort a line of textual content (presumably a command) on the Linux command line and instantly reserve it to a file by urgent simply three keys. The editor that may open up will rely in your $EDITOR setting which you can view utilizing the command proven under:

$ echo $EDITOR
nano

When you choose to make use of a unique editor, use a command like this earlier than typing or shifting again to the command that you just need to save:

$ export EDITOR=vi

And remember to save lots of this modification to your .bashrc (or different start-up file) if you wish to make this modification everlasting. For instance:

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

For example, say you have simply composed a very advanced command, verified that it really works as you anticipated, and now need to reserve it in a file as a way to use it any time you need with out having to reinvent it. Is that tough? No, it is easy! Press the up-arrow key as when you intend to run the command once more, however, as an alternative, maintain the Ctrl key and press each the “X” and “E” keys. That is sometimes called the Ctrl-x-e sequence. While you do that, the command ought to open within the editor.

Within the instance proven under, the textual content is displayed in nano. The instructions on the backside of the display present nano’s command choices. If you choose ^O, you’ll be prompted to save lots of the command in /tmp with a random file title. Again over the instructed file title and it can save you it to your property or present location within the file system. It would ask you to verify the “totally different” file title. After that, you’ll be able to exit the editor and your command might be sitting in a file to your later use. You’ll, as you seemingly suspect, need to make the file executable since Linux would not do that with no chmod command. It would look one thing like this if you use nano:

echo It is a command that I need to save in a file


^G Assist        ^O Write Out   ^W The place Is    ^Ok Reduce         ^T Execute
^X Exit        ^R Learn File   ^ Change     ^U Paste       ^J Justify

Here is a easy instance. Say you used a command just like the one proven under to show the times of the week as they’re output by the cal command:

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

To avoid wasting the command in a file after working, press the up arrow key with out urgent the enter key to get again to it.

$ cal | head -2 | tail -1

Subsequent, enter the Ctrl-x-e sequence when you’re nonetheless positioned on the finish of the road. Doing this can open the command within the editor. You’ll be able to enter ^O to put it aside, however when you again over the instructed file title (e.g., /tmp/bash-fc.9RfMEe”) and kind in one thing like “days_of_week” or “~/days_of_week” when you’re not in your house listing, it can save you it to your present location as an alternative of to /tmp. Then sort ^X to exit.

The truth is, you should utilize this system for any command that’s nonetheless in your command historical past. Use the up arrow to get again to the command after which deploy the Ctrl-x-e trick.

Wrap-up

There are a lot of methods to save lots of instructions that you just may need to reuse. You’ll be able to sort them right into a file, use the echo command so as to add them to a file, or create an alias. The Ctrl-x-e trick makes saving a command to a file almost easy.

Copyright © 2023 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments