[For Linux Beginners] Done! Edit and save with vi commands

table of contents
Hello!
I'm Inoue, a Persian cat from Beyond Co., Ltd.'s Shikoku office.
This time, we'll be talking about the "vi" command, which is used to edit files!
There are so many options that it was difficult to verify.
What is the vi command?
The "vi" command is a command that says "Start an editor!" .
"vi" originally refers to the text editor used in Unix-based operating systems.
At the same time, it will also create a file if it does not exist in the directory.
When I first joined the company, I wondered, "What exactly is an editor???"
but it's easier to understand if you think of it as the "Notepad" in Windows.
Create and edit files with 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 take you to "haruka" normal mode.
At this point, no files have been created yet.
-rw------ 1 root root 12288 Jun 23 01:21 .haruka.swp
This SWAP file is created temporarily.
A SWAP file is a temporary recording file that
is created when vi is opened to prevent data loss due to an application crash, and is automatically deleted after editing in vi .
This prevents the loss of unsaved data even if vi is forcibly terminated due to a system error. For
this reason, the file "haruka" is created only when ":w" is executed.
To edit the contents of the file with vi, press the i to enter
INSERT Once in insert mode, you can write in the "haruka" file.
Let's write a profile.
Asuka Inoue (Persian cat)<font></font> Hobbies: 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, you are 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> Enter the contents into the editor in insert mode, save it, and a file named "haruka" will be created! The vi command allows you to type by switching from normal mode to insert mode. If an existing file is open in vi, it will be overwritten and saved. If a new file is open, 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 21, 07:42 Haruka</font></font>
return to command mode with " : wq ". Also, if you want to view a saved file and then quit, use " q ". To force quit vi, use " ! ". For example, if you want to quit without saving the changes you made in vi and discard them, use
[code]:q![/code]
Use
Good to know!
■Cursor movement commandsYou
can also use the arrow keys to move, but here are some useful cursor movement commands to know.
*Make sure you press the "Esc" key and exit edit mode before executing them.
| 0 (zero) | Move the cursor to the beginning of the current line |
| $ | Move the cursor to the end of the line |
| 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 the character at the current cursor position |
| DD | Delete the current cursor line |
| d$ | Delete from the current cursor position to the end of the line |
| d0 | Delete from the current cursor position to the beginning of the line |
| Yes | Delete a word |
| Dig | Delete to the first line |
| dG | Delete to the last line |
■Yank
To copy (yank), use y.
Use y followed by a cursor movement command.
| Hi | Yank the line at the current cursor position |
Finally
I had a really hard time writing a blog about the vi command. I
thought I understood it, but I didn't. It was
about the correct way to switch from insert mode, where you edit an editor, to command mode, and then exit the editor.
And how could I explain it in a way that a Linux beginner like me could understand?
After a lot of trial and error, this blog was finally born! (`・ω・´)
We also have a blog that explains Linux in an easy-to-understand way, so please take a look!
https://beyondjapan.com/blog/2022/02/linux/
[What you need to know if you're looking to work for an IT company!] What is Linux? (For beginners)
Growing every day, progressing every day.
I have to update myself every day!!!
Thank you for reading to the end.
28