vi - Text editor for command line

vi is a text editor that comes with Linux by default and can be run from the command line.

The problem with Linux is how can I edit a document directly?

In Linux, it is common to edit the configuration file from the command line to configure the web server and database server.

The command line text editor vi is a bit difficult to use for first-time users.

If you're used to a graphical GUI editor, it's probably the first challenge to getting used to Linux.

Since you can't use a mouse, you have to do everything with the keyboard, and there's also the concept of edit mode.

Let's run vi

First, let's run vi with an empty file.

Creating a text file

To create a text file:

vi TextFileName

If you pass the file name as an argument of vi, it will create a new one if the text does not exist. Let's create a file called foo.txt.

vi foo.txt

I think vi will start up.

Exit the vi editor

I think the problem for beginners here is that they don't know how to quit the vi editor, so let's just remember this. The procedure is as follows.

Press Esc

Pressing Esc changes vi to a mode called command mode . vi has two modes, one for editing characters and one for executing commands. To exit the vi editor, first press Esc and then in command mode. Must be.

Then type ":q".

:q

Type ":q" and press Enter. Exit the vi editor. I am running a command to exit the editor called q. The colon ":" is required before the command.

Kill without saving

Also remember the command to kill without saving your edits. In the vi editor, if there are changes to the contents of the editor, you cannot exit with the q command.

Use the q! command to kill without saving. Press Esc to change to command mode, then type the following and press Enter.

:q!

Save edits

Use the "w" command to save your edits. You can save it by pressing Esc to enter command mode, then typing ":w" and pressing Enter.

:w

Save your edits and exit

Save another one and remember the end. Use the wq command. Press Esc to enter command mode, then type ":wq" and press Enter to save and exit.

:wq

If you remember the three exit methods, you can manage to start vi by mistake.

  • q - End
  • q! - Exit without saving
  • wq - Save and exit

Edit text with vi editor

Let's edit the text using the vi editor. Write the minimum necessary information.

Just like last time, let's start the vi editor first.

vi foo.txt

It has started.

Edit mode

The vi editor initially starts in command mode. vi has two modes, command mode and edit mode. Initially you are in command mode, so you need to be in edit mode.

Press i to enter edit mode.

i

You can enter characters in edit mode. Feel free to enter characters such as abc. After pressing i, you will find that you are free to edit.

If you want to save your edits, use the w command after entering command mode. To enter command mode, I first pressed the Esc key.

:w

You can save the text by typing as above and pressing Enter. To edit, press i again to switch to edit mode.

If you want to save and exit, do the following.

:wq

Now you can edit the configuration file somehow.

basic vi commands

If you remember the minimum requirements for vi, you will be able to edit the configuration file, so it's a good idea to move on to the next chapter. Here, I would like to introduce a more convenient way to use vi.

Display line numbers

When you start vi, you will notice that there is no line number. It's inconvenient without the line number. In such a case, do as follows.

First press the Esc key to enter command mode. Then type the following and press Enter.

:set number

The line number will appear on the left.

vi configuration file

The line number setting is not permanent and only takes effect while you launch vi. If you want to keep the settings, use the configuration file . If you write it in the configuration file, it will execute the command when you start vi.

The vi configuration file is .vimrc in the user's home directory. Isn't there such a file? It doesn't exist by default, so you need to create it.

vi ~/.vimrc

Let's write the command in this file.

set number

The leading colon is not needed.

You can move to your home directory by typing cd ~ . Tilde is a special character that means the user's home directory.

Let's use vi to create a .vimrc . Then press i and when you're in edit mode, type:

set number

When writing to a vi configuration file, the colon is not at the beginning. Write without a colon. Then press Esc to enter command mode, type :wq to save and exit. Then the file is created.

On Linux, files starting with . are hidden files. Configuration files are often hidden files. The regular ls command doesn't list it in the file, but you can use the a option to see it, including hidden files.

ls -a

Now you can see that you have a file called .vimrc. Let's open a text file with vi. You can see that the line numbers are displayed from the beginning.

I want to enter a blank in the tab

Many programmers want to type blank with the tab key instead of the tab.

:set expandtab

I want to set the tab width

If you are a programmer, you also want to set the tab width.

:set tabstop=2

I want to indent when a line breaks

If you are a programmer, you want to indent instead of returning to the beginning when you start a new line.

:set autoindent

The configuration file is large, so I want to move it to the end of the file

If you want to move to the end, type G in command mode.

:G

I want to move to my favorite line number

If you want to move to your favorite line number, use number G .

:1G
:25G

If it is 1G, you can move to the 1st line, and if it is 25G, you can move to the 25th line.

You can move by just typing the line number in command mode with the Esc key.

:3

I want to search for a character string

If you want to search, first type / in command mode. Hit the slash. Then it will be as follows.

/

Then type in the characters you want to search for here.

/foo

Then press Enter. You can now search. Press n to move to the next matched position. Press N to return. You can now search.

  • n - Forward
  • N - Back
  • I want to copy

    You can copy by left-clicking and selecting a range.

    I want to paste

    You can paste with the right click. This may have different settings depending on the environment.

    I want to delete a lot of ranges

    You can also delete it with delete or backspace, but sometimes you want to delete a lot of ranges at once. To delete one line, type the following in command mode.

    dd
    

    If you want to delete multiple lines, just before this, describe the number of lines you want to delete.

    5dd
    

    Move up / down / left / right

    You can also use the arrow keys to move up / down / left / right, but you can also use the Esc key to switch to command mode and then use the next key to move up / down / left / right.I can do it.

    • k - above
    • h - left
    • j - below
    • k - above

    Advance one character to edit mode

    You can use "i" to enter edit mode, but you can use "a" to advance one character to edit mode.

    Move to the end

    You can move to the end of the line with "$" in command mode.

    Undo

    You can undo your work by running "u" in command mode.

    I want to run Perl

    In command mode, you can execute any Linux command by prefixing it with "!". When running Perl, do the following:

    !perl ScriptName
    

    If you remember this much, you will find it much easier to edit the configuration file.

Associated Information