Linux editor-vim use && Linux compiler-gcc/g++ use

Linux editor-vim use

1. Basic concepts of vim

There are many modes of vim, in fact, we commonly use about three, namely: command mode (command mode), insert mode (Insert mode) and bottom line mode (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 certain section and enter Insert mode, or to last line mode

  • Insert mode

    Only in Insert mode, can you do text input, press "ESC" key to return to command line mode. This mode is the most frequent editing mode we will use later.

  • Last line mode

    Save or exit the file, you can also perform file replacement, search for character strings, and list line numbers. In command mode, shift+: can enter this mode. To view all your modes: open vim and type directly in the bottom line mode.

2. The basic operation of vim

  • Enter vim, enter vim and file name after the system prompts symbols, and enter the full-screen editing screen of vim:
$ vim test.c

After entering vim, you are in [Normal Mode], you can only enter text when you switch to [Insert Mode]

  • [Normal Mode] Switch to [Insert Mode]

    Input a
    input i
    input o

  • Insert mode] switch to [normal mode]

    Currently in [Insert Mode], you can only input text all the time. If you find that you have typed a wrong word and want to use the cursor keys to move back to delete the word, you can press the "ESC" key to go to the [Normal Mode] and then delete the text . Of course, it can also be deleted directly.

  • [Normal Mode] Switch to [Last Line Mode]

    Enter [shift +:]

  • Exit vim and save the file. In [Normal Mode], click the ":" colon key to enter "Last line mode", for example:
:w (保存当前文件)
:wq (保存并退出vim)
:q (输入q,不存盘直接退出vim)

3.vim normal mode command set

  • Insert mode

    Press "i" to switch to the insert mode "insert mode", press "i" to enter the insert mode to start inputting the file from the current position of the cursor;

    After pressing "a" to enter the insert mode, the text is entered from the position next to the current cursor position;

    After pressing "o" to enter the insert mode, a new line is inserted, and text is entered from the beginning of the line

  • Switch from insert mode to command mode

    Press the [ESC] key

  • Move the cursor

    Vim can directly use the cursor on the keyboard to move up, down, left, and right, but the regular vim uses lowercase English letters "h", "j", "k", and "l" to control the cursor to move left, down, up, and right. grid

    Press [G]: Move to the end of the article
    Press [$]: Move to the "end of line" of the line where the cursor is
    Press [^]: Move to the "beginning of the line" of the line where the cursor is
    Press [w]: Move the cursor to the next
    Press [e] at the beginning of a word: the cursor jumps to the end of the next word and
    press [b]: the cursor returns to the beginning of the previous word
    press [#I]: the cursor moves to the #th position of the line, such as: 5I , 56I
    Press [gg]: enter the text and start
    press [shift + g]: enter the end of the text and
    press "ctrl" + "b": the screen moves one page to the "back"
    Press "ctrl" + "f": the screen moves to " Move one page forward and
    press "ctrl" + "u": the screen moves half a page to the "back"
    Press "ctrl" + "d": move the screen half a page to the "front"

  • Delete text

    "X": Each time you press, delete a character at the cursor position.
    "#x": For example, "6x" means delete 6 characters "behind (including yourself)" at the cursor position. "
    X": uppercase X , Each time you press, delete the character
    "#X" "in front of" the cursor position : For example, "20X" means delete the 20 characters "in front" of the cursor position
    "dd": delete the line where the cursor is
    "#dd": Delete # 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 "counting down" from the line where the cursor is located.
    "P": Paste the characters in the buffer to the cursor position. Note: All copy commands related to "y" must cooperate with "p" to complete the copy and paste function

  • replace

    "R": Replace the character at the cursor.
    "R": Replace the character where the cursor is, until the "ESC" key is pressed

  • Undo the last operation

    "U": If you execute a command by mistake, you can press "u" immediately to return to the previous operation. Press repeatedly "u" can perform multiple back to
    the complex.
    "Ctrl + r": Undo recovery

  • change

    "Cw": Change the word at the cursor to the end
    "c#w": For example, "c3w" means change 3 characters

  • Jump to the specified line

    "Ctrl" + "g" lists 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

  • List the line number
    "set nu": After inputting "set nu", the line number will be listed before each line in the file.

  • Jump to a line in the file
    "#": The "#" sign represents a number. Enter a number after the colon and press the Enter key to jump to that line. For example, enter the number 15 and press Enter, it will Skip to line 15 of the article.

  • Find the character "/keyword":
    Press the "/" key first, and then enter 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" and you will be found later To the keywords.
    "? Keyword": Press the "?" key first, and then enter 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 key you want. Word so far.

  • Save the file
    "w": Enter the letter "w" in the colon to save the file and leave vim
    "q": Press "q" to exit. If you can't leave vim, you can force vim after "q" followed by a "!" .
    "Wq": It is generally recommended to use it with "w" when leaving, so that the file can be saved when exiting.

6. Simple vim configuration

To configure a good-looking vim, the native configuration may not be fully functional, you can choose to install a plug-in to complete the configuration, ensure that the user is the user you want to configure,
attach the link that has been written by the boss and follow the prompts to configure a good-looking vim: one Command to configure powerful vim

Linux compiler-gcc/g++ use

  • 1. Background knowledge
    code to generate executable program to go through the following four steps:

    1. Preprocessing (for macro replacement)
    2. Compile (generate assembly)
    3. Assembly (generate machine identifiable code)
    4. Link (generate executable file or library file)

    Install gcc: sudo yum install -y gcc
    install g++:sudo yum install -y gcc-c++

  • 2. How to use gcc commands

    Format gcc [option] file to be compiled [option] [object file]

    Preprocessing (for macro replacement) The
    preprocessing function mainly includes macro definition, file inclusion, conditional compilation, and interpretation.
    The preprocessing instruction is an
    example of a line of code beginning with the # sign : gcc -E hello.c -o hello.i
    option "-E", the function of this option is to make gcc stop the compilation process after the preprocessing ends.
    The option "-o" refers to the target file, and the ".i" file is the original C program that has been preprocessed.

    Compiling (generating assembly)
    In this stage, gcc first checks the standardization of the code, whether there are grammatical errors, etc., to determine the actual work of the code, and after the checks are correct, gcc translates the code into assembly language.
    Users can use the "-S" option to view, this option only compiles without assembly, and generates assembly code.
    Examples:gcc –S hello.i –o hello.s

    Assembling (generating machine-recognizable code) The
    assembly stage is to convert the ".s" file generated during the compilation stage into an object file.
    Readers can use the option "-c" to see that the assembly code has been converted into a binary ".o" Target code
    example:gcc –c hello.s –o hello.o

    Linking (generating executable files or library files)
    After successful compilation, it enters the linking phase.
    Examples:gcc hello.o –o hello

In our C program, the function implementation of "printf" is not defined, and there is only the declaration of the function in the "stdio.h" included in the precompilation, but the implementation of the function is not defined. Then, where is the implementation of "printf" "What about the function?

  • [] The answer is that the system has implemented these functions in a library file named libc.so.6. Unless otherwise specified, gcc will search under the system default search path "/usr/lib" , Which is to link to the libc.so.6 library function, so that the function "printf" can be realized, and this is the function of the link

    Function libraries are generally divided into static libraries and dynamic libraries

  • The static library means that when compiling and linking, all the code of the library file is added to the executable file, so the generated file is relatively large, but the library file is no longer needed at runtime. The suffix name is generally ".a"

  • On the contrary, the dynamic library does not add the code of the library file to the executable file when compiling and linking, but when the program is executed, the library is loaded by the runtime link file, which can save system overhead. The general suffix of the dynamic library is ".so". As mentioned earlier,
    libc.so.6 is the dynamic library. gcc uses dynamic libraries by default when compiling. After completing the link, gcc can generate an executable file, such asgcc hello.o –o hello

  • The binary program generated by gcc by default is dynamically linked, which can be verified by the file command

gcc options:

-E 只激活预处理,这个不生成文件,你需要把它重定向到一个输出文件里面
-S 编译到汇编语言不进行汇编和链接
-c 编译到目标代码
-o 文件输出到 文件
-static 此选项对生成的文件采用静态链接
-g 生成调试信息。GNU 调试器可利用该信息。
-shared 此选项将尽量使用动态库,所以生成文件比较小,但是需要系统由动态库.
-O0
-O1
-O2
-O3 编译器的优化选项的4个级别,-O0表示没有优化,-O1为缺省值,-O3优化级别最高
-w 不生成任何警告信息。
-Wall 生成所有警告信息。

Guess you like

Origin blog.51cto.com/14289099/2545531