[Linux] Basic use of package manager yum and editor vim

First, yum background knowledge

1. Business ecology

Before we install a software, we need to download the corresponding software package first, but this software package does not exist on our local computer disk, but exists on the remote server; then how does the computer know which specific software package exists? What about on a server?

For computers, we generally get the corresponding software packages by searching the official website of the corresponding software; for mobile phones, on the surface, we download the software packages through the app store that comes with the phone, but in fact there are no software packages in the app store , but there is only a link to the official website of the corresponding software, and finally our software package is downloaded from the official website;

Second, who provides these packages? The answer is obvious, they are provided by some enterprises, organizations or individuals; these enterprises, organizations and individuals have written software packages for certain interests, and then put them on the corresponding servers.

Since then, a simple business ecological chain has come out - some people have written software packages for others to use, and get benefits from them through charging or built-in advertisements; at the same time, they also pay fees to mobile phone manufacturers to make their own The software can be downloaded and used in the application store of the corresponding mobile phone or a third-party download tool (such as Thunder, Yongyongbao).

2. Open source ecology

Some people write software to make money, and naturally some people don’t do it for making money. They may do it to improve their skills and gain a sense of accomplishment, or to increase their popularity, or because they are different or boring; in short, some people will Disclosing the source code of the software package written by oneself, so that others can use it for free, this is called open source;

After Torvalds wrote the Linux operating system and made it open source, many people in the world participated in the improvement and expansion of Linux, including those who wrote software for Linux for free; these people will join the corresponding Linux community, and then Put the software you write on the server corresponding to the community; and different communities will have built-in servers and download links corresponding to the software in their own Linux versions, and the software used to store the download links is yum ;

Yum (Yellow dog Updater, Modified) is a very commonly used package manager under Linux; it is mainly used on Fedora, RedHat, Centos and other distributions; software packages and package managers are like "App" and "App Store " Such a relationship.

The above process is also the formation process of the open source ecology – different Linux distributions create corresponding Linux communities, and the community has a public account to accept donations from all over the world, and then use the funds in the account to purchase servers and hold various activities, etc. etc.; people with open source spirit from all over the world deploy their own open source software on the server of the community; the community then copies the download links of these software to the package manager (such as yum) for the Linux distribution, so that We can install and use various software through the package manager in Linux.

3. Localization of software ecology

Since the west started and developed computers much earlier than our country, the open source ecology we mentioned above was first formed in the west, that is, most Linux communities, including the servers corresponding to the community, are deployed abroad; When downloading software from the link, the access will be relatively slow, coupled with some special national conditions of our country, sometimes the access will fail;

In response to the above situation, some universities and companies in China have mirrored foreign software services , that is, copied the software on the foreign server to the server of the domestic company, so that we can directly access the domestic server to download the software;

But just copying the software is not enough, because the default links accessed when downloading software in yum are still foreign, so these universities/companies also provide a set of domestic download link configuration files – yum source configuration files ;

In Linux, the yum source configuration file is the CentOS-Base.repo file that exists in the /etc/yum.repos.d/ directory:image-20221022183303532

If you are using a cloud server, the yum source is generally configured. If you are using a virtual machine, you need to open CentOS-Base.repo to check whether the link in it is a domestic link. If not, you need to Manual configuration, there are many configuration methods on the Internet, just search for the corresponding Linux distribution directly;image-20221022182100949

image-20221022182231376


Second, the basic use of yum

1. Check the software package

We can use the yum list command to list the current software packages; but because there are so many packages, we generally use the grep command to filter out the packages we care about; for example:image-20221022184139736

Precautions

  • Software package name composition: major version number. minor version number. source program release number - software package release number. host platform. cpu architecture;
  • The "x86_64" suffix indicates the installation package of the 64-bit system, and the "i686" suffix indicates the installation package of the 32-bit system. When selecting the package, it must match the system;
  • "el7" indicates the version of the operating system distribution: "el7" indicates centos7/redhat7, and "el6" indicates centos6/redhat6;
  • The base in the last column represents the name of "software source", which is similar to concepts such as "Xiaomi App Store" and "Huawei App Store";

Note: The rzsz tool is used for windows machines and remote Linux machines to transfer files through XShell. After installation, we can upload files by dragging or commanding.

2. Install the software

We can install the package with the following command (where -y means direct installation without asking):

yum install -y 软件名

image-20221022185454445

Some software has not been included in the official software collection of related ecological platforms such as Centos, Ubuntu, Kail, etc. If we want to use these software, we need to install the unofficial software collection list – epel-realse ;image-20221022183624866

Precautions

  • When installing software, it is necessary to write content to the system directory, which generally requires sudo or switching to the root account to complete;
  • The yum installation software can only install one after the other is installed; in the process of installing a software with yum, if you try to install another software with yum, yum will report an error;
  • There is a relationship between software and software, that is, there is a certain degree of coupling; in order to solve the problem of interdependence between software, yum sometimes installs some other software when installing one software.

3. Uninstall the software

The command to uninstall the software is as follows (where -y means to uninstall directly without asking):

yum remove -y 软件名

image-20221022190220279

Note: All operations on yum must ensure that the host (virtual machine) network is unblocked; the network can be verified through the ping command:image-20221022191209930


3. Basic use of vim

1. The basic concept of vim

Vim is known as the most powerful editor in the world. It is a well-known powerful and highly customizable text editor similar to Vi . It has improved and added many features on the basis of Vi; vi/vim are multiple The mode editor, 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, such as syntax highlighting and visual operation; it can not only run in the terminal, but also run For x window, mac os, windows.

Vim has a total of 12 modes, and we currently only need to master three of them, which are command mode, insert mode and last line mode. The functions of each mode are as follows:

  • Normal/Normal/Command mode (Normal 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, you can do text input, press the "ESC" key to return to the command line mode, this mode is the most frequent editing mode we will use later;

  • Bottom line mode (last line mode)

Save or exit the file, you can also perform file replacement, find strings, list line numbers, etc. In command mode, enter: to enter this mode;

We can enter "help vim-modes" in the bottom line mode to view all modes of vim.

2. The basic operation of vim

2.1 Switch between modes

When we use vim to open a file, it is in the command mode by default, and then we can switch to other modes through the following command:

  • Switch from [command mode] to [insert mode]: input i/a/o; the cursor is before the first character when entering with i, after the first character when entering with a, and is at the first character when entering with o the beginning of a blank line after a line;
  • [Command Mode] Switch to [Last Line Mode]: "shift + ;", actually input ":";
  • [Other modes] switch to [Command mode]: [Esc];
  • Exit vim: Enter wq in the bottom line mode, where w means to save, and q means to quit, we can also exit directly without saving;

image-20221022230230199

Note: It is [Esc] to switch from any other mode to the command mode, but some only need to be pressed once, and some need to be pressed multiple times. In short, the brainless [Esc] will eventually return to the command mode.

2.2 Cursor positioning

  • $ – moves the cursor to the end of the line;
  • ^ – move the cursor to the beginning of the line;
  • G - move the cursor to the end of the file;
  • gg - move the cursor to the beginning of the file;
  • n + G – move the cursor to the nth line;
  • hjkl moves the cursor to the left, bottom, top, and right;

2.3 Text Copy

  • yy – copy the current line (nyy: copy n lines down from the current line);
  • p – paste once (np: paste n times);
  • dd – delete the current line (ndd: delete n lines from the current line);
  • u – undo operation (one step back);
  • ctrl + r: cancel the undo operation (one step forward);

Note: All delete operations under vim are equivalent to cut operations under Windows;

2.4 Text Editing

  • ~ – switch the case of the character where the cursor is located (long press ~: switch the case of all characters from the character where the cursor is located to the end of the line);
  • rx – replace the character where the cursor is with x character (nrx: replace n characters after the character where the cursor is located with x character);
  • R – Batch replacement, that is, switch to the replacement mode , replace the character where the cursor is with the character entered by the keyboard, and the cursor will automatically move back after the replacement, waiting for the next character to be replaced; we need to enter [Esc] to switch back to the command mode from the replacement mode ;
  • x – delete the character where the cursor is located (nx: delete n characters after the character where the cursor is located);
  • X – delete the character before the character where the cursor is located (nX: delete deletes the first n characters of the character where the cursor is located);
  • w – jump the cursor to the first character of the next word (nw: jump the cursor to the first character of the next n words);
  • cw – change the word where the cursor is to the end of the word, like R, this command will let us jump to insert mode (cnw: change n words);

2.5 Operation of Bottom Row Mode

All the above operations are performed in the command mode. In addition to the command mode, we also commonly use the insert mode and the bottom line mode, and the insert mode basically has no shortcut operations, so let’s learn some basic operations of the bottom line mode. ;

  • set nu – set the line number (set nonu: cancel the line number);
  • vs – split screen (note: no matter how many screens we split, there is only one cursor, which represents which screen we are operating on);
  • ctrl + ww – switch the cursor to a different screen (note: this command is executed in command mode);
  • wq – save and exit, w means save, q means quit, the two can be executed separately;
  • wq! – force save and force quit, w! stands for force save, q! stands for force quit, the two can be executed separately;
  • %s/mark1/mark2/g – replace all mark1 in the text with mark2 (s stands for replacement, g stands for global);
  • !Command – Execute various commands in Linux without exiting vim;

Note: In addition to ctrl + ww, the above commands are executed on the premise that they are currently in the bottom line mode, that is, they all need to be preceded by a : sign;

Finally, I recommend two articles about vim learning written by big guys: Concise Vim leveling strategy | Cool Shell - CoolShell GitHub - Vim from entry to mastery


4. Simple vim configuration

## 1、什么是 vim 配置

Under the directory /etc/, we can find a file named vimrc , which is a public vim configuration file in the system and is valid for all users ;image-20221023203506184

image-20221023204326557

The default vim global configuration file of the system has some basic configurations for vim, such as syntax highlighting, background color, etc., but there are still many inconveniences, such as no automatic indentation, no bracket completion, no line number display, etc. wait;

So we need to do some other basic configurations on vim so that we can write C/C++ programs in vim conveniently; special attention: Although all users in Linux use the same vim program, everyone uses the Different vim configurations, because each user's home directory will have its own.vimrcfile, which saves the vim configuration exclusive to this user .image-20221023205429420

2. Simple vim configuration

We can search on the Internet to perform some basic configurations of vim, such as

  • Set syntax highlighting: syntax on;
  • Display line number: set nu;
  • Set the number of spaces for indentation to 4: set shiftwidth=4;

image-20221023205834491

image-20221023205901658

image-20221023205954783

3. One-click vim configuration

Because vim has a lot of configuration options, it will take a lot of time to configure a convenient and easy-to-use vim; so I found a vim one-click configuration method on Gitee – VimForCpp: quickly build vim into c++ IDE (gitee.com)image-20221023210454269

You only need to enter the following command in your user directory to complete the one-click vim configuration:

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

image-20221023210847311

Then we enter "source ~/.bashrc" or restart the terminal to invalidate the vim configuration:image-20221023211212970

The vim after vim is as follows:image-20221023211455914

It can be seen that after the configuration is complete, vim will not only display the current mode, file name, and number of characters, but also support automatic indentation and automatic completion (syntax, brackets, quotation marks, etc.). For more details, you can read this The project's README.md file – VimForCpp: quickly build vim into a c++ IDE (gitee.com)

But there is a small problem here: the automatic indentation here is two characters by default, and when we write C/C++ programs, we generally require four characters to be indented, so we need to open the .vimrc file to modify the default indentation:image-20221023212041164

image-20221023212131293

image-20221023212216909

So far, our vim one-click configuration is complete. (Note: Various hidden files generated when installing vimforcpp, such as .VimForCpp .vim must not be deleted; non-hidden files generated – install.sh can be deleted, but it is also recommended not to delete)image-20221023212739537


Five, configure sudo permissions

When learning file permissions , we mentioned that the sudo command can be used to escalate the privileges of ordinary users, so that ordinary users can execute commands as the root user, but the premise is that the user must be a trusted user, that is, the user Must be in the sudoers file;image-20221023213338252

image-20221023213422979

After learning how to use vim, we can add ourselves to the sudoers file under root:image-20221023213750863

image-20221023213940202

image-20221023214140992

Now, we can use sudo to escalate the command under ordinary users:image-20221023214448488


Guess you like

Origin blog.csdn.net/m0_62391199/article/details/127486029