[Linux] Linux editor - vim use

Why learn vim?

"Everything is a file in a Linux system, and configuring a service is modifying the parameters of its configuration file." And in daily work, everyone must inevitably have to write documents, and these tasks are all done through text editors.

Vim is a text editor (a tool for writing code) on Linux. Sometimes our software may have various problems in our production environment. These problems need to be solved through vim. If we don’t vim, at this time we need to carry out local and remote joint development in VS code under Windows to check the problem, and there is no way to modify it directly on Linux, which is too slow.

Our goal is to use vim first, and then we can program directly under the black screen of windows, so that when we develop software that has problems running under Linux, we can use vim to face these extreme environments (such as: modifying the source code) , which also satisfies our regular development.

learn:

The difference between vi and vim To put it simply, they are both multi-mode editors. The difference is that vim is an upgraded version of vi. It is not only compatible with all commands of vi, but also has some new features in it. For example, syntax highlighting and visualization operations can be run not only on the terminal, but also on x window, mac os, and windows.

Here is a keyboard map of vi/vim commands, which can be learned in conjunction with this blog
insert image description here

1. The basic concept of vim

Unlike integrated development environments (IDEs) such as VS 2019 and idea, vim is a pure editor.

Vim is generally installed by default on the Linux operating system we use, and we don't need to install it again. We can directly enter vim and press Enter to check whether it is installed. If the display effect is as follows, it means it is installed. (use ctrl+z to go back)

insert image description here

If you do not have vim, you can use the following command to install it

yum install -y vim         //root下
sudo yum install -y vim    //普通用户下,使用sudo安装

vim is a multi-mode editor. The usage and differences of each mode are different. Each mode can be switched between each other. Here we mainly master three types, namely command mode, insert mode and bottom line mode. The functions of each mode are distinguished as follows:

  • normal/normal/command mode

    Control the movement of the screen cursor, delete characters, words or lines, move and copy a section and enter Insert mode, or go to last line mode

  • insert mode

    Only in Insert mode, text input can be done, press the [ESC] key to return to the command line mode. This mode is the most frequent editing mode we will use later.

  • last row mode

    Save or exit the file, you can also perform file replacement, find character strings, list line numbers and other operations. In command mode, shift+: to enter this mode. To see all your modes: open vim, the bottom line mode is inserted directly.

    :help vim-modes

2. Basic operation of vim

These three modes in the vim editor, each mode supports a variety of different command shortcut keys, which greatly improves the efficiency of work, and users will feel quite comfortable after getting used to it. If you want to manipulate text efficiently, you must first understand the differences between the three modes and how to switch between them.

insert image description here

  • Enter vim, enter vim and the file name at the system prompt, and enter the vim full-screen editing screen:

    • vim file name (we can directly use the vim file name to open the file for compilation, if the file does not exist, the file will be created by default, if the file exists, open the file)
    • After entering vim, it is in [command mode], you have to switch to [insert mode] to be able to enter text.

insert image description here

  • 【Command Mode】Switch to 【Insert Mode】

    • input a

    • input i

    • input o

insert image description here

  • 【Insert Mode】Switch to 【Command Mode】

    • Currently in [Insert Mode], you can only input text all the time. If you find that you have entered a wrong word and want to use the cursor keys to move back and delete the word, you can first press the "ESC" key to enter [Normal Mode] and then delete Word. Of course, it can also be deleted directly.
  • 【Command Mode】Switch to 【Last Line Mode】

    • [shift + ;], in fact, enter [:] colon
  • Exit vim and save the file. In [Command Mode], press the [:] colon key to enter [Last line mode], for example:

    • :w (save current file)
    • :wq (enter [wq], save and exit vim)
    • :q! (Enter q! to force quit vim without saving)
    • :q (exit, if the file has been modified, there is no way to exit directly)
    • :set nu (display line numbers)
    • :set nonu (do not display line numbers)
    • :command (execute the command)
    • : integer (jump to the line)

    The following picture shows from insert mode to exit vim
    insert image description here

Summarize:

Every time you run the Vim editor, it enters the command mode by default. At this time, you need to switch to the input mode before writing the document. After writing the document, you need to return to the command mode first, and then enter the last line mode. Perform a save or exit operation on the document. In Vim, there is no way to switch directly from input mode to last-line mode.

  • If we don’t know what mode we are currently in, there is no brain [ESC], after pressing it, we must be in command mode
  • When exiting, be sure to save before exiting.
  • Do not use the mouse and mouse wheel after opening vim.

3. vim command mode command set

The following commands are implemented in command mode

  • insert mode

    • Press [i] to switch to insert mode [insert mode], press "i" to enter insert mode to start inputting files from the current position of the cursor;
    • After pressing [a] to enter the insert mode, the text will be inserted from the position next to the current cursor position;
    • After pressing【o】to enter the insert mode, a new line is inserted, and the text is input from the beginning of the line.

    Press [ESC] key to return to command mode from insert mode

  • move cursor

    • Vim can directly use the cursor to move up, down, left, and right, but the prescribed vim uses lowercase English letters [h], [j], [k], and [l] to control the cursor to move left, down, up, and right by one space respectively.

      insert image description here

    • Press【G】: Move to the end of the article

    • Press 【$】: Move to the "end of line" of the line where the cursor is located

    • Press 【^】: move to the "beginning" of the line where the cursor is located

    • Press [w]: the cursor jumps to the beginning of the next word

    • Press [n + w]: the cursor jumps to the beginning of the next nth word

    • Press【e】: the cursor jumps to the end of the next word

    • Press [b]: the cursor returns to the beginning of the previous word

    • Press [n + b]: the cursor jumps to the beginning of the nth word ahead

    • Press 【#】: the cursor moves to the #th position of the line, such as: 51, 561

    • Press 【gg】: enter the beginning of the text

    • Press [shift + g]: enter the end of the text

    • Press [n + shift + g]: first enter the number n of lines to be located, and then press shift + g to move to the line

    • Press【ctrl】+【b】: Move the screen to "Back" by one page

    • Press [ctrl] + [f]: the screen moves to "forward" by one page

    • Press [ctrl] + [u]: the screen moves half a page to "backward"

    • Press [ctrl] + [d]: the screen moves half a page to "forward"

  • delete text

    • 【x】: Each time you press, delete a character at the cursor position
    • [#x]: For example, [6x] means to delete the 6 characters behind "(including yourself)" where the cursor is located
    • 【X】: Uppercase X, every time you press it, delete a character "before" where the cursor is located
    • 【#X】: For example, 【20X】indicates to delete the 20 characters "before" where the cursor is located
    • [dd]: Cut the line (use p to paste, do not use p to delete)
    • 【#dd】: Cut # line from the line where the cursor is
  • copy

    • [yw]: Copy the characters from the cursor position to the end of the word to the buffer.
    • 【#yw】: Copy # words to the buffer
    • [yy]: Copy the line where the cursor is located to the buffer
    • [#yy]: For example, [6yy] means to copy 6 lines of text "downwards" from the line where the cursor is located
    • 【p】: Paste the characters in the buffer to the position of the cursor. Note: All copy commands related to "y" must cooperate with "p" to complete the copy and paste functions.

    Once saved and exited in vim, the previous copy operation will not be saved again, and the following operations are the same.

    As shown in the figure, copy one line and multiple lines (yy + p):
    insert image description here

  • replace

    • [r]: replace the character where the cursor is
    • [R]: Replace the character where the cursor is, until the [ESC] key is pressed
  • undo last action

    • 【u】: If you execute a command by mistake, you can press 【u】immediately to return to the previous operation. Press "u" multiple times to perform multiple replies
    • [ctrl + r]: undo recovery

    The figure below shows the undo and redo commands (u / ctrl + r):
    insert image description here

  • Change

    • [cw]: Change the word where the cursor is to the end of the word
    • [c#w]: For example, [c3w] means change 3 characters
    • [shift + ~]: switch case
    • [r]+[replacement character]: press r first and then replace the character according to the need, the character pointed by the cursor will be replaced with the character to be replaced
    • 【n+r】+【replaced characters】: add n to replace n characters after the cursor position.
    • [shift + r]: enter the replacement mode (use [ESC] to exit this mode)

    As shown in the figure below, switch case (shift + ~) and
    insert image description here
    enter replacement mode (shift + r) as shown in the figure below
    insert image description here

  • jump to the specified line

    • 【ctrl】+【g】List the line number of the line where the cursor is located.
    • 【#G】: For example, 【15G】, means to move the cursor to the beginning of the 15th line of the article.

4. vim last line mode command set

Before using the last line mode, please remember to press the [ESC] key to confirm that you are in the command mode, and then press the [:] colon to enter the last line mode.

  • list line numbers

    • 【set nu】: After inputting set nu, the line number will be listed in front of each line in the file.
    • 【set nonu】: After inputting set nonu, the line number will not be displayed.
  • jump to a line in a file

    • 【#】:【#】sign represents a number, enter a number after the colon, and press the Enter key to jump to the line, such as: enter the number 15, press Enter, it will jump to the 15th article Row.
  • find character

    • [/Keyword]: Press the [/] key first, and then input the character you want to find. If the keyword you find for the first time is not what you want, you can keep pressing [n] to find the desired keyword. .

    • [? Keyword]: Press the [?] key first, and then input the character you want to find. If the keyword you search for the first time is not what you want, you can keep pressing [n] to find the desired keyword.

    • Question: / and ? Find the right difference?

      the difference:

      ? Find Press【n】to search up

      / Find Press【n】to find down

    insert image description here

  • save document

    • [w]: Enter the letter [w] with a colon to save the file.
  • leave vim

    • [q]: Press [q] to exit, if you can’t leave vim, you can follow [q] followed by a [!] to force you to leave vim.
    • [wq]: It is generally recommended to use it together with [w] when leaving, so that the file can be saved when exiting.
  • operate on the file

    • 【! + Command】: Use! Followed by a command means to execute the command.

      Such as:

      ! gcc test.c means to generate an executable file for this file.

      ! ls -la displays the file information in the directory where the file is located

    insert image description here

  • replace operation

    • [s/replace target/replaced data]: For example: s/ZS/MAN, replace the first ZS in the line where the cursor is currently located with MAN
    • [s/replace target/replaced data/g]: such as: s/ZS/MAN/g, replace all ZS in the line where the current cursor is located with MAN (g—global, global)
    • [%s/replacement target/replaced data/g]: For example: %s/ZS/MAN/g, replace all ZS in the full text with MAN

    insert image description here

  • split screen operation

    • [vs file name]: split screen to create a new file.
    • Notice:
      1. There is only one cursor, where is the cursor, write that file.
      2. Use ctrl + w + w (press ctrl and press w twice quickly to switch the cursor to a different file)
      3. You can use vs to generate multiple split screens, but generally up to three, no matter how much it affects the actual operation.
      4. The newly created file can be created or uncreated, and it will be created by default after vs
      5. Quit with wq or q

5. Simple vim configuration

In our study and work, the initial vim of the Linux system or the vim we downloaded by ourselves was not very convenient to use at the very beginning, which requires us to configure or download the vim configuration file of a big guy to modify ourselves vim.

When we configure vim ourselves, the configuration file name is .vimrc . When vim starts, it will automatically search for the configuration file in the current user directory. If it is not found, the original vim is still used.

We can create the .vimrc file ourselves, configure it in it, and add the functions we need.

cd ~                  //进入家目录
touch .vimrc          //创建vim配置文件

The .vimrc configuration file created by myself , each Linux user is independent and does not affect each other. We configure a vim configuration file under one user. The configuration will not affect other users under the Linux system, but only modified vim under this user.

We generally put the .vimrc configuration file in the user's home directory (the following is the configuration file I downloaded from others)

insert image description here

If you want to configure your own configuration file, you can search it online.

insert image description here

Find some settings you like and write them in your own configuration file.

The pictures above are the vim before I configured v, and the picture below is the vim that I downloaded and configured

insert image description here

If your version is centos7, you can copy the following command to the command line, download the vim, and restart it to use it.

curl -sLf https://gitee.com/HGtz2222/VimForCpp/raw/master/install.sh -o ./install.sh && bash ./install.sh

You can also click the link below to learn more vim knowledge.

Vim from entry to proficiency

6. Use sudo to escalate privileges

Under Linux, the root user can do anything regardless of permissions, but other ordinary users cannot, but in study and work, we also need to use root permissions to do some things under ordinary users, here we need to use sudo Let's escalate the privileges of our command so that it has root privileges.

If we want to use sudo, we need to configure it ourselves before we can use it. The following is the configuration method.

  1. switch to root user

    insert image description here

  2. Open /etc/sudoers file

    insert image description here

  3. Find the line below and add users who need to use sudo

    insert image description here

    Add a user to enable sudo using the following format

    用户      ALL=(ALL)       ALL
    
  4. exit root user

    //方法1
    ctrl+d
    
    //方法2
    su 用户名
    

7. How to solve the problem of using ctrl + z to exit when closing vim?

When we use vim to write a file and accidentally use it to ctrl + zexit, and use vim to enter the file next time, we will encounter the following situation
insert image description here
. How to solve this problem? The following is the method I use, I hope it will help you

  • First use the command on the terminal to jobsview the serial number of the vim file that needs to be terminated, and other programs running in the background will also be displayed, as follows
    insert image description here
    Then use fg 序号, open the file represented by the serial number, and use the normal exit operation :wq!to force exit, weak Enter y when you encounter the following prompt
    insert image description here

Guess you like

Origin blog.csdn.net/m0_52094687/article/details/128542024