I’m going to talk about the vi command which is for editing files in Linux.
There are so many options…!
The vi command is a command that gives an order like “start the editor!!” At the same time, if the file doesn’t exist, it creates a file.
It is easy to understand if you think of it as a "Notepad" in Windows.
Let’s create and edit a file with the vi command
First, create a file named “TestDir” under “test” directory using the vi command.
[root@test-aws-TestDir test]# pwd /root/test
[root@test-aws-TestDir test]# vi test
If you run the command above, it moves to normal mode of “test”.
The file hasn't been created at this point yet.
-rw------- 1 root root 12288 Jun 23 01:21 .test.swp
A SWAP file like the above is temporarily created.
A SWAP file is a temporary record file that is created when vi is opened and then automatically deleted after editing with the vi. It prevents losing data in case the application crashes.
Because of this, you don’t need to worry about losing data if the vi is forced to close due to a system error.
Therefore, the file “test” is created only when “:w” is executed.
When you want to edit inside of the file with the vi, press the “i” key to activate “INSERT” mode.
When it's in insert mode, you can write in the file “test”.
I’m just going put random informations about beyond GTA
Name: Beyond GTA Location: Toronto, ON Favorite: Linux -- INSERT --
When it displays “-- INSERT --” at the bottom, it means it’s in the insert mode.
When you want to exit insert mode, press the “ESC” key.
Write the contents in the editor in insert mode, and save it, the file “test” is created at the same time!
The vi command allows you to type some letters by going from normal mode to insert mode.
When the file exists, it overwrites and saves the changes. When it’s a new file, it creates a new file and saves it.
[root@test-aws-TestDir test]# ll total 4 -rw-r--r-- 1 root root 83 Jun 21 07:42 test
You can go back with “:” and overwrite with “wq”.
Also, when you want to exit from viewing a saved file, use “q”, and when you want to force to end the vi, add “!” to before “q”.
For example, when you want to discard the changes and exit, use this.
Cursor motion command
You can use arrow keys to move the cursor, but there are more useful commands.
*Press the “ESC” key to make sure you are not in insert mode when you use these commands.
Jump to the start of the line where the cursor is
Jump to the end of the line where the cursor is
Go to the first line
Go to the last linen
Go to the line n
x or dl
Delete a letter
Delete a line
Delete current position to end of line
Delete current position to the beginning of line
Delete a word
Delete from the first line to current line
Delete current line to end of the line
If you want to copy(yank), use “y”.
copy a line
This blog post is translated from a blog post written by Inoue Haruka on our Japanese website Beyond Co..