Created as a memorandum
--Recently, there are excellent editors such as VS Code and Atom, and vim / vi and emacs are still popular. ――However, if you try to change a limited environment such as Docker or a system setting that has passed a considerable number of years, you may not be able to use the usual editor. ――Therefore, I summarized the rescue measures that can be set in any situation. --Examples of using ed, ex of line editor, cat for file display, echo for character string output are described.
ed --Line editor for UNIX operating systems. The author is Ken Thompson --Can be used on almost all Unixes --Since ex and vi were created based on this, the operation method is similar to the command line mode (ex mode) of vi.
$ ed test.txt //ed is the command name. test.txt is the file name
95 //The number of characters in the file is displayed
//Waiting for input (nothing is displayed)
$ ed -p : test.txt //If you want to match the appearance with ex described later"-p : "With the addition of":"Can be used as an input standby symbol
95 //The number of characters in the file is displayed
: //Waiting for input
--Text file display: (Display characters waiting for subsequent input are:)
:,p //Standby symbol:Against (specified above)",p"Enter to display all lines
// ","Indicates the entire line of the file and p indicates print, so",p"Is displayed in all lines
This is No.1 line.
This is No.2 Line.
This is No.3 Line.
:. // "."Indicates a so-called cursor line."."When".p"Is the same
This is No.3 Line.
:
:.= // ".="Indicates a line number.".="When".=p"Is the same
3
:
:2 //Enter the line number directly to move the line
This is No.2 Line.
:
--Character input: a command Add text below the cursor line --Note: The following is an example of moving the cursor to the second line and inputting. --You can use the i command as well as vi / vim. For i command, text is added above the cursor line
:2 //Move line
This is No.2 Line.
:a // "a" (Move to text input mode with the add command). After inputting"."Enter
This line has been inserted.
The add command can insert multiple lines. //Multiple lines can be entered
. //Text input is completed here
:
:,p //Show the above input
This is No.1 line.
This is No.2 Line.
This line has been inserted.
The add command can insert multiple lines.
This is No.3 Line.
:
--Line deletion: Deleted line by line and cannot be deleted by character or word (replacement described later is possible)
:d //Delete cursor line
:
:4d //Deleted the 4th line
:
:2,25d //Delete 2 to 25 lines at once
:
--Line change: c command Delete the specified line and insert a new sentence (multiple lines are possible).
:,p //Show before change
This is No.1 line.
This is No.2 line.
This line has been inserted.
The add command can insert multiple lines.
This is No.3 line.
:
:4,5c //Delete lines 4-5 and replace with new lines
This is No.4 line.
. //Text input completed
:,p //Show after change
This is No.1 line.
This is No.2 line.
This line has been inserted.
This is No.4 line.
:
--Search: Enter "/ character string" when searching backwards. Enter "? Character string" when searching forward. --Replacement: Replace old with new with the s command s / old / new /. This replacement replaces only the first matching string on a line-by-line basis, so the g option is required to convert all matching strings. s / old / new / g --Save: Same as w command vi. Save your changes. If you specify a file name, it will be saved with that file name. --End: Same as q command vi. (Forced termination is uppercase Q)
:w //Save command
126 //The number of saved characters is displayed
:q //End command (type wq to end saving)
$
ex --Line editor for Unix systems written by Bill Joy in 1976 --ex was finally given a full screen visual interface and became vi --Currently ex is implemented as part of vi. vi has "ex mode", which you can call using the ex command or by typing ":" from within vi. Although the functions of ex and vi are duplicated, some of them can be executed only by the ex command, so it is also useful when using vi. In other words, you can think of ex = vi at present (same for vim).
$ ex test //ex is the command name. test is the filename
"test" 6L, 126C
Entering Ex mode. Type "visual" to go to Normal mode. //Hit visual and vi/Go to vim standard screen
:
--Display text file:% command
:% //Show all lines
This is No.1 line.
This is No.2 line.
This is No.3 line.
:
:2 //Enter the line number directly to move the line
This is No.2 line.
:
--Character input: a command Add text below the cursor line --Note: The following is an example of moving the cursor to the second line and inputting. --Since it is the same as vi / vim, you can also use the i command. For the i command, a sentence is added above the cursor line.
:2 //Move to the second line
:a // "a" (Move to text input mode with the add command). After inputting"."Enter
This line has been inserted.
The add command can insert multiple lines. //Multiple lines can be entered
. //Text input is completed here
:
:% //Show the above input
This is No.1 line.
This is No.2 line.
This line has been inserted.
The add command can insert multiple lines.
This is No.3 line.
:
--Search, replace, save, exit is the same as vi / vim
cat
--cat is a standard UNIX command used to concatenate and display files. --Abbreviation for "catenate" which means to concatenate --You can also create a text file using cat. However, it is only a new creation and cannot be edited later. --Pass standard input to the cat command (it is possible without passing it), and exit with Ctl + C (Ctl + D) when writing multiple lines is completed.
Bill Joy, who developed vi, said in an interview "Which editor is easy to use", "cat is easy to use". It is a world that ordinary people cannot understand that tools that cannot be edited and cannot be turned back are easy to use.
$ cat > test.txt //Note that the contents of the specified file name will be newly created from scratch.
This is No.1 line.
This is No.2 line.
This is No.3 line.
^C //Ctl when editing is complete+C ( Ctl+D )
$
$ cat test.txt //Check the edited contents
This is No.1 line.
This is No.2 line.
This is No.3 line.
$
echo
--The Unix echo command is a command that outputs the character string given as an argument as input. --In some cases, such as when all external commands cannot be used for some reason, it is often an internal command. --Use echo redirect --From standard output, press> to overwrite the file and >> to add to the file --Exactly the last resort
$ echo "This is No.1 line." > test.txt //Note that the contents of the specified file name will be newly created from scratch.
$ echo "This is No.2 line." >> test.txt //Add line after last line
$ cat test.txt //Check the edited contents
This is No.1 line.
This is No.2 line.
$
--There are other methods such as sed --Since I have only done the above four, the description is omitted.
Recommended Posts