top of page

Posts

【For Linux Beginners】How to Edit and Save With the vi Command



Hello!


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.


:wq

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.


:q!

Useful commands!

  • 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.



0(Zero)

Jump to the start of the line where the cursor is

​$

Jump to the end of the line where the cursor is

gg

Go to the first line

G

Go to the last linen

nG

Go to the line n


  • Deletion commands

x or dl

Delete a letter

dd

Delete a line

d$

Delete current position to end of line

d0

Delete current position to the beginning of line

dw

Delete a word

dgg

Delete from the first line to current line

dG

Delete current line to end of the line


  • yank

If you want to copy(yank), use “y”.


yy

copy a line


Thank you!


This blog post is translated from a blog post written by Inoue Haruka on our Japanese website Beyond Co..


Comments


bottom of page