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

Hello!
I'm Inoue, the Persian cat at Beyond Shikoku office.

This time, we'll be discussing the "vi" command for editing files!
There are so many options, I had a hard time testing them all.

What is the vi command?

a command that instructs the system to "launch the editor!"is
"vi" itself refers to the text editor used in Unix-like operating systems.
It also creates the file if it doesn't 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 Windows equivalent of "Notepad."

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 switch you to "haruka"'s normal mode.
No files have been created yet at this point.

-rw------ 1 root root 12288 Jun 23 01:21 .haruka.swp

A SWAP file like this is created temporarily.
to prevent data loss in the event of an application crash
a temporary record file that is created when vi is opened.
This prevents the loss of data before saving even if vi is forcibly terminated due to a system error. Therefore,
the file named "haruka" is created only when ":w" is executed.

To edit the file contents with vi,ipress theINSERTto enter
Once in insert mode, you can write to the "haruka" file.
Let's try writing a profile for now.

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>

":You can return to command mode withwqsave by overwriting withq". To force exit of vi, press "!". For example, to discard changes made in vi without saving and exit:

[code]:q![/code]

Use

Good to know!

■Cursor Movement Commands
You can also move the cursor using the arrow keys, but here are some useful cursor movement commands.
*Please make sure to press the "Esc" key to exit edit mode before executing these commands.

0 (zero) Move the cursor to the beginning of the current line
$ Move the cursor to the end of the line
Google 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


To perform a yank copy (yank), use the 'y' command.
Specify a cursor movement command after 'y'.

Hi Yank the line at the current cursor position

Finally

I had a really hard time writing a blog post about the vi command.
I thought I understood it, but I didn't really. I struggled with things like
the correct way to switch from insert mode (where you edit in the editor) to command mode, and how to exit the editor.
And also, how to explain it in a way that a Linux beginner like me could understand...
After much trial and error, this blog post was finally born!! (`・ω・´)

There's also a blog that explains Linux in an easy-to-understand way, so please check that out too!
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, moving forward every day.
I must update myself every single day!!!
Thank you for reading to the end.

If you found this article helpful,please give it a "Like"!
29
Loading...
29 votes, average: 1.00 / 129
82,717
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Ayaka Inoue

I belong to the Systems Solutions Department.
I joined Beyond as a founding member of the Shikoku office. I jumped into the IT industry with no prior experience.
As part of the training team, I create curricula and conduct training for new graduates, mid-career hires, and existing members. My
main duties are server operation and maintenance.
Above all, I value our customers' content.
I also belong to the Web Content Business Department and the YouTube team.