[For Linux beginners] Done! Edit and save with vi command
table of contents
Hello!
This is Inoue, a Persian cat from Beyond Shikoku Office.
This time we will talk about the “vi” command for editing files!
There were so many options that it was difficult to verify.
What is the vi command?
is
a command that says "Start the editor!" First of all, "vi" refers to the text editor used in Unix-based OS.
At the same time, if the file does not exist in the directory, it will also create the file.
When I first joined the company, I was wondering, "What is an editor anyway?", but
it's easier to understand if you think of it as "Notepad" in Windows.
Create and edit a file using the vi command
First, use the vi command to specify the file name ``haruka'' in the ``test'' directory and edit it.
[code][root@test-aws-harukainoue test]# pwd /root/test[/code]
[root@test-aws-harukainoue test]# vi haruka
Executing the above command will move you to normal mode of "haruka".
At this time, no file has been created yet.
-rw------ 1 root root 12288 Jun 23 01:21 .haruka.swp
A SWAP file like this will be created temporarily.
is
a temporary recording file that is created when vi is opened and is automatically deleted after editing in vi to prevent the application from crashing and data being lost This prevents unsaved data from being lost even if vi is forcibly terminated due to a system error.
Therefore, the file "haruka" is created only when ":w" is executed.
If you want to edit the contents of a file in vi, press the i to enter
INSERT Once in insert mode, you can write it in the "haruka" file.
I'll write about it on my profile as well.
Akika Inoue (Persian cat)<font></font> Hobby: Cooking<font></font> Likes: strawberries and cats<font></font><font></font> <font style="vertical-align: inherit;"><font style="vertical-align: inherit;">-- insert --[/code]</font></font></pre> While " <strong>-- INSERT --</strong> " is displayed, it is in insert mode. To exit edit mode, press the " <strong>Esc</strong> " key.<pre> <font style="vertical-align: inherit;"><font style="vertical-align: inherit;">[code]:wq[/code]</font></font></pre> When you write the content in the editor in insert mode and save it, a file called "haruka" is also created! The vi command allows you to type characters by switching from normal mode to insert mode. If an existing file is opened in vi, it will be overwritten and saved; if a new file is opened, a new file will be created.<pre> <font style="vertical-align: inherit;"><font style="vertical-align: inherit;">[code][root@test-aws-harukainoue test]# ll</font></font><font></font> <font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Total 4</font></font><font></font> <font style="vertical-align: inherit;"><font style="vertical-align: inherit;">-rw-r--r-- 1 root root 83 June 21st 07:42 Haruka</font></font>
return to command mode with " : overwrite with wq add q to view the saved file and exit, or ! to forcefully exit vi For example, if you want to discard and exit without saving the changes edited with vi,
[code]:q![/code]
use.
Useful to know!
■Cursor movement commands
You can also use the arrow keys to move the cursor, but here are some useful cursor movement commands you should know.
*Be sure to press the "Esc" key and confirm that the editing mode is finished before executing.
0 (zero) | Move the cursor to the beginning of the line where the cursor is. |
$ | Move the cursor to the end of the line where the cursor is located |
Gugu | Move the cursor to the beginning of the file. |
G | Move the cursor to the end of the file. |
Mr. | Move the cursor to the end of the file. |
■Delete command
x or dl | Delete character at current cursor position |
DD | Delete the line under the current cursor |
d$ | Delete from current cursor position to end of line |
d0 | Delete from the current cursor position to the beginning of the line |
Dawa | delete one word |
dig | delete up to first line |
dG | delete to last line |
■
If you want to yank copy (yank), use y.
Use by specifying a cursor movement command after y.
Hi | Yank the line at the current cursor position |
Finally
I had a hard time writing about the vi command on my blog.
I thought I knew about the vi command, but I didn't.
The correct way to change the editor from edited insert mode to command mode and exit the editor.
In addition, I was wondering how to explain it in a way that a Linux beginner like me could understand...
After much trial and error, this blog was finally born! ! (`・ω・´)
I also have a blog that explains Linux in an easy-to-understand manner, so be sure to check it out!
https://beyondjapan.com/blog/2022/02/linux/
Growing every day, moving forward every day.
I have to update myself every day! ! !
Thank you for reading to the end.