"In-depth understanding of computer systems"

Source: Internet
Author: User
Tags set background

Experiment One

Introduction to VIM Mode

There are many modes of vim from VI, this unique design is easy to confuse the beginner. Almost all editors have two modes of inserting and executing commands, and most editors use a different approach to vim: the command directory (mouse or keyboard driver), the combination of keys (usually with the control key (CTRL) and the ALT key (ALT)) or the mouse input. Vim and VI, just as the keyboard to switch among these modes. This allows vim to operate without a menu or mouse, and to minimize the operation of key combinations. It can greatly enhance the speed and efficiency of the text entry clerk or programmer.

VIM has 6 basic modes and 5 derivation modes, and here we simply introduce the next 6 basic modes:

    • Normal mode

In normal mode, the editor commands, such as moving the cursor, deleting text, and so on. This is also the default mode after Vim is started. This is exactly the opposite of what many new users expect (most editor default mode is insert mode).

Vim's powerful editor can come from its normal mode commands. Normal mode commands often require an operator end. For example, the normal mode command dd deletes the current row, but the first "D" can be followed by another move command instead of the second one d , such as a "J" key that moves to the next line to delete the current row and the next row. You can also specify the number of commands to repeat, 2dd (repeat dd two times), and dj the effect is the same. Users learn a variety of text between the move/jump commands and other common mode of editing commands, and can be used flexibly combined, can be more efficient than those without the pattern of the editor for text editing.

In normal mode, there are many ways to get into insert mode. The more common way is to press a (append/append) or i (insert/insert) key.

    • Insertion modes (insert mode)

In this mode, most keystrokes insert text into the text buffer. Most new users want this mode to persist throughout the text editor editing process.

In insert mode, you can press the ESC key back to normal mode.

    • Visual modes (visual mode)

This pattern is similar to normal mode. However, the move command expands the highlighted text area. The highlighted area can be a character, a line, or a piece of text. When a non-moving command is executed, the command is executed to the highlighted area. Vim's "text object" can also be used in this mode as the move command.

    • Selection mode (select modes)

This pattern is similar to the behavior of the modeless editor (the way Windows standard text controls are). In this mode, you can use the mouse or cursor keys to highlight the selection of text, but the input of any character, Vim will use this character to replace the selected highlighted text block, and automatically enter the insertion mode.

    • Command line mode

In command-line mode, you can enter text that will be interpreted and executed. For example, execute commands ( : keys), search ( / and ? keys), or filter commands ( ! keys). After the command is executed, VIM returns to the pattern before the command-line mode, usually in normal mode.

    • Ex modes (ex mode)

This is similar to command-line mode, where :visual you can execute multiple commands at one time before using the command to leave the ex mode.

This is commonly used in common mode, insert mode, and command line mode, and this course covers only the three common patterns.

2. Switching of three common modes

Vim starts into normal mode, in insert mode or command line mode, just press Esc or Ctrl+[ (this is no use in the VIM course environment) to enter normal mode. In normal mode i , press (insert) or a (attach) keys to enter the insert mode, in normal mode press : enter command line mode. In the command line mode, enter the wq carriage return and save and exit Vim.

Enter VIM1. Use the VIM command to enter the Vim interface

Vim is appended with the file name that you want to open, or that does not exist (then as a new file). Open the Xfce terminal and enter the following command

$ vim practice_1.txt

Vim editor can also be opened using Vim directly, but no files will be opened.

$ vim

Enter the command line mode and the :e 文件路径 same file can be opened.

2. Cursor movement

After entering Vim, press the i key to enter insert mode. In this mode you can enter text information below, please enter the following three lines of information:

12345678abcdefghijkshiyanlou.com

Press Esc Enter normal mode, use the arrow keys in this mode or,,, the h j k l Key can move the cursor.

Key Description
h Left
l Right (lowercase L)
j Under
k On
w Move to the next word
b Move to the previous word

Try using the arrow keys to move the cursor over these letters in normal mode shiyanlou .

Enter insert mode 1. Enter insert mode

Use the following key in normal mode to enter insert mode, and to start typing from the corresponding position

Command Description
i Edit at current cursor
I Insert at beginning of line
A Inserting at the end of a row
a Insert an edit after the cursor
o Insert a new row after the current row
O Insert a new row before the current line
cw Replace the character from the position of the cursor to the end of a word

Try different ways to get into the insert mode from normal mode, add in front of the last line Shiyanlou www. , Notice that each time you go back to normal mode to switch to insert mode in different ways

Save Document 1. Save the document in command line mode

Enter the : command line mode from normal mode, enter w a carriage return, and save the document. Enter to save the :w 文件名 document as a different file name or save it to another path

Vi. exit vim1. Exit vim in command line mode

Enter : command line mode from normal mode, enter wq return, save and exit edit

Here are a few other ways to exit:

Command Description
:q! Force quit, do not save
:q Exit
:wq! Force Save and exit
:w <文件路径> Save As
:saveas 文件路径 Save As
:x Save and exit
:wq Save and exit
2. Exiting vim in normal mode

Normal mode input Shift+zz can be saved out of vim

Delete Text 1. Delete vim text information in normal mode

Go to normal mode and use the following commands to quickly delete text:

Command Description
x Delete the character that the cursor contains
X Delete the previous character of a cursor
Delete Withx
dd Delete entire row
dw Delete a word (not in Chinese)
d$OrD Delete to end of line
d^ Delete to beginning of line
dG Delete to end of document
d1G Delete to document header

In addition, you can add a number to the command before it, indicating that multiple rows are deleted at once, such as:

2ddRepresents deleting 2 rows at a time

2.shiftwidth command

shiftwidthThe command refers >> to the indentation generated by the previous command (which can be abbreviated sw ). Enter : command line mode to set values in normal mode to control the number of characters in shiftwidth indentation and fallback get current setpoint

:set shiftwidth?

Set indent to 10 characters:set shiftwidth=10

Enter ESC back into normal mode and try again to >> see if the indent changes

Problem: There is no need to ESC exit, after the command, enter directly back to normal mode

3. Adjust the text position

Command line mode enter :ce (center) command to center the contents of the bank:ce

Command line mode enter :ri (right) command to make our text:ri

Command line Mode enter: le (left) command to leave the contents of the bank:le

Quick Find

Enter Normal mode / and then type the string you want to find and press ENTER to find it. is the same as the / function, but looks up and / down. After entering the lookup, input n and N can continue to find (equivalent to find the next and previous) n means to continue to find, N reverse lookup

Advanced Find
    • Enter \* a word in normal mode to find where the cursor is located
    • Normal mode input \# Ibid, but \* forward (up) to find, #则是向后 (bottom) find
    • Normal mode g\* \* , but partially matches the word
    • Normal mode g\# \# , but partially matches the word

Search for n The above, N continue to find command can still be used

Use Vim to edit multiple files

There are two forms of editing multiple files, one being a parameter that is used before entering Vim is multiple files. Another is to enter vim and then edit the other files. Create two new files and edit them simultaneously$ vim 1.txt 2.txt

Default Access 1.txt to file editing interface

Command line mode input :n edit 2.txt file, you can ! :n! Force switch, the input of a file is not saved, just switch to another file

Command line mode input :N edit 1.txt file, can be ! forced to :N! switch, the input in the previous file is not saved, just switch to another file

Open new file after entering vim

Command line mode enter :e 3.txt open new file 3.txt

Command line mode input :e# back to the previous file

command-line mode input :ls to list previously edited documents

Command line mode input :b 2.txt (or number) can be directly into the file 2.txt edit

Enter :bd 2.txt (or number) in command-line mode to delete a file item from a previously edited list

Command line mode input :e! 4.txt , new open file 4.txt, discard the file being edited

Enter :f The file name you are editing in command line mode

Input in command line mode :f new.txt , change the file being edited name to New.txt

Recover files

If the document is not saved due to a power outage, you can use the recovery method, vim -r Enter the document ($ vim-r 1.txt), input :ewcover 1.txt to restore

Introduction to the Visual Mode command (I understand that it is good to choose an object to work with)

Enter in normal mode v (lowercase), enter the character selection mode, you can move the cursor, where the cursor will be selected. The selection is deselected when the V-meeting is pressed again.

In normal mode input Shift+v (lowercase), enter the line selection mode, press V will be the entire row selection, you can move up and down the cursor select more rows, again, press once Shift+v can be deselected.

In normal mode Ctrl+v (lowercase), this is the area selection mode, you can select the rectangular region, and then cancel the selection by one time Ctrl+v .

Enter d Delete selection area in normal mode

Enter y Copy selection area contents in normal mode

Introduction to Windows Operations

Vim can open multiple windows in one interface for editing, which are called Vim windows. There are many ways to open a method, for example, you can use the command-line mode to enter : New   Open a new vim window , and enter the window to edit a new file (normal mode input ctrl+w The is OK, but ctrl+w will conflict with Chrome's shortcut keys to close tabs under Chrome, so use this shortcut to practice in IE or other browsers, except for : New command, The various methods listed below can also open a new window in command mode or Normal mode:

    • Command-line mode enter :sp 1.txt open new Landscape window to edit 1.txt
    • Command-line mode :vsp 2.txt to open a new vertical window to edit 1.txt
    • Ctrl-w ssplit the current window into two horizontal windows in normal mode
    • Ctrl-w vsplit the current window into two vertical windows in normal mode
    • In normal mode Ctrl-w q , it is: Q ends the split window. If you have input in a new window, you need to use the mandatory character! namely: q!
    • Ctrl-w oOpen a window in normal mode and hide all previous windows
    • Ctrl-w jmove to the following window in normal mode
    • Ctrl-w kmove to the upper window in normal mode
    • Ctrl-w hmove to the left window in normal mode
    • Ctrl-w lmove to the right window in normal mode
    • Ctrl-w Jmove the current window below in normal mode
    • Ctrl-w Kmove the current window to the top in normal mode
    • Ctrl-w Hmove the current window to the left in normal mode
    • Ctrl-w Lmove the current window to the right in normal mode
    • Ctrl-w -reduce the height of the window in normal mode
    • Ctrl-w +increase the height of the window in normal mode

If you use a non-Chrome browser, you can use Ctrl+w the jump between windows

Create an encrypted document
$ vim -x file1

Enter your password to confirm the password so that the next time you open, VIM will ask you to enter the password

Enter command-line mode ! to execute external shell commands

    • :!lsUsed to display the contents of the current directory
    • :!rm FILENAMEUsed to delete a file named filename
    • :w FILENAMESave the file you are editing in the current VIM as a filename file
View Help in Vim
    • Press F1 vim to open your own preset help document in normal mode
    • Enter :h shiftwidth shiftwidth The open Help file in command line mode
    • Input :ver display version and parameters in command line mode
Vim's function setting

Can be set when editing the file function settings, such as the command line mode input :set nu (display number of lines), set value exit Vim will not be saved. to permanently save the configuration, you need to modify the Vim configuration file . Vim configuration file ~/.vimrc , you can open the file for modification, but be careful not to affect the normal use of vim

Get the current settings
    • Enter :set or :se display all modified configurations in command line mode
    • command-line mode input :set all displays all the SetPoint values
    • Command-line mode :set option? to enter a setting value that displays option
    • Command line mode to enter the :set nooption cancel current set value
Description of the Set function
    • :set autoindent(ai)automatic indentation of input settings in command line mode
    • Command line mode input :set autowrite(aw) set AutoArchive, default not open
    • Enter or in command line :set background=dark mode light , set the background style
    • Command line mode input :set backup(bk) set automatic backup, default not open
    • Command line mode input : set cindent(cin) set C language style indent

Experiment: Use of GCC

GCC supports compilation of some source file suffix names

suffix source File
. C C Language Source files
. C. cc. cxx C + + source files
. m Object-c source File
. I Pre-processed C source file
, i+ Pre-processed C + + source files
. S. S assembly language Source file
. h preprocessing files (header files)
. o Target file
. A Archive file

Procedures for GCC compilers

Linux executables do not have the obvious. exe suffix name as Windows does, just assign x (executable) permissions to it.sudo chmod u+x excutefile

You can have GCC end at any stage of the compilation in order to check or use the output of that phase (this is important)

Example

Open the GVIM environment in input I enter edit mode, enter the following code

// filename: hello.c#include <stdio.h>int main(int argc, char **argv){ printf("Hello, Shi-Yan-Lou!");}/** *在XfceTerminal打开后的界面中输入:$gcc hello.c -o hello *如果没有error,说明编译成功,将会在当前目录生成一个可执行文件 hello *继续输入:./hello 就会运行该程序,在bash上打印出 Hello, Shi-Yan-Lou! **/

期中:gcc hello.c -o hello---The second hello is the filename, the name is arbitrary (but not against Bash's rules)

From the programmer's point of view, a simple-o option can omit many intermediate steps to output an executable file at once; But from the compiler's point of view, behind this command is a series of miscellaneous work.

GCC process:

1. GCC invokes the preprocessor CPP, which is responsible for expanding the macros defined in the source program (example: #include <stdio.h>), inserting the contents of the # include statement into it (expand the code contained in stdio.h)

In the Xfce terminal, enter:$ gcc -E hello.c -o hello.i

Hello.I This is a C source file that has been processed by the preprocessor

The-e parameter of GCC allows GCC to stop the compilation process at the end of preprocessing.

2, the hello.i compiled into the target code, GCC by default, the. i file as the pre-processing C language source code, so it will skip preprocessing directly, to begin the compilation process.

$ gcc -c hello.i -o hello.o

Similarly, using Vim to open the. o file, it should be garbled (it is already a binary file)

Tips:

  1. Keep in mind that when GCC preprocessed the source file (first step), no syntax errors were checked
  2. The grammar check takes place in the second step, such as curly braces mismatch, no semicolon at the end of the line, keyword error ...

3, GCC connector link the target file to an executable file, an approximate compilation process ends

gcc hello.o -o hello


示例及作业
作业只需要改一下函数hello_print.c
实验示例:

作业:


experiment: GDB's use

GDB (GNU Debugger) debugger method, the program is a debugger that is used to help programmers find bugs in the program software.

Start the program, you can follow the user-defined requirements to run the program at will.

Allows the program to be debugged to stop at the breakpoint specified by the user (the breakpoint can be a conditional expression).

When the program stops, you can check what happens in the program at this time.

Dynamically change the execution environment of the program.

From the above, GDB and the general debugging tools are not very different, basically to complete these functions, but in the details, will find GDB this debugging tool powerful. The command-line debugging tool has features that the graphical tools cannot do.

GDB's main debugging is the C + + program. In order to debug a C + + program, the debugging information must be added to the executable file at compile time. Use the-g parameter of the compiler (cc/gcc/g++) .

Such as:$ gcc -g hello.c -o hello

$ g++ -g hello.cpp -o hello

Without-G, the function name and variable name of the program are not visible, instead they are all memory addresses at runtime. When the debug information is added with-G and the target code is compiled successfully, see how to debug with GDB. There are several ways to start gdb:

    1. GDB <program> Program is the execution file, usually in the current directory.
    2. GDB <program> Core uses GDB to debug a running program and core file at the same time, and the core is the file generated after the core dump is executed illegally by the program.
    3. GDB <program> <PID> If the program is a service program, you can specify the process ID of the service program when it runs. GDB will automatically attach up and debug it. The program should be searched in the PATH environment variable. GDB start, you can add some gdb start switch, the detailed switch can be viewed with gdb-help. Here are just a few of the more commonly used parameters:-symbols <file>-S <file> reading the symbol table from the specified file. The-se file reads the symbol table information from the specified files and uses it in the executable file. -core <file>-C <file> Debug Core dump core file. -directory <directory>-D <directory> joins a search path for a source file. The default search path is the path defined by path in the environment variable.
Makefile use

The benefits of makefile are-"automated compilation", one but write, only need a make command, the entire project can be fully compiled, greatly improve the software development efficiency

There are countless source files in a project, which are placed in several directories by their type, function and module. Makefile defines a set of rules to specify which files need to be compiled first, which files need to be compiled, which files need to be recompiled, and even more complex functional operations

The most important and basic function of make tools is to automate compiling and maintaining multiple source files according to the relationship between source programs described in the makefile file. While the makefile file needs to be written in a certain syntax, the file needs to explain how to compile the individual source files and link them to the executable file, which requires defining the dependencies between the source files.

Here is an example of a simple makefile.

Use the following command to compile:

$ gcc -c code.c -o code.o$ gcc -c prog.c -o prog.o$ gcc prog.o code.o -o test

First of all$ vim Makefile

test: prog.o code.o gcc prog.o code.o -o testprog.o: prog.c code.h gcc -c prog.c -o prog.ocode.o: code.c code.h gcc -c code.c -o code.oclean: rm -f *.o test

编译命令反向写makefile

With this makefile, whenever you modify the source file, just execute the Make command, and all necessary recompilation will be performed automatically. The Make program uses the data in the makefile to generate and traverse the tree with test as the root node; now take the above example to learn the general wording of makefile:

test(目标文件): prog.o code.o(依赖文件列表)tab(至少一个tab的位置) gcc prog.o code.o -o test(命令).......
一个Makefile文件主要含有一系列的规则,每条规则包含一下内容:一个目标,即make最终需要创建的文件,如可执行文件和目标文件;目标也可以是要执行的动作,如‘clean’;一个或多个依赖文件的列表,通常是编译目标文件所需要的其他文件。之后的一系列命令,是make执行的动作,通常是把指定的相关文件编译成目标文件的编译命令,每个命令占一行,并以tab开头(初学者务必注意:是tab,而不是空格) 执行以上Makefile后就会自动化编译

Using the Makefile with macros

Makefile can also define and use macros (also known as variables) to make them more automated and flexible, and the format for defining macros in Makefile is:

macroname = macrotext

The format for using macros is:

$(macroname)
 
The foundation of C language programming in experiment two Linux

In Vim:

    • The uppercase "K" can be used to find help for the function: Look at the man page, in command mode, place the cursor on the function name, press "K" to see the mans page directly.
    • Several settings

: Set NU Displays line number

: Set AI automatically indents rows

: Set ts=4 sets a TAB key equal to several spaces

    • Move cursor

[[Go to Previous ' {' in first column]

]] to go to the next "{" In the first column

{Go to previous empty row

} go to the next empty line

GD goes to the definition of the local variable referred to by the current cursor

The process of compiling the code for GCC is as follows:

    • Preprocessing: Gcc–e hello.c–o hello.i;gcc–e Call CPP
    • Compilation: Gcc–s hello.i–o hello.s;gcc–s Call CCL
    • Compilation: Gcc–c hello.s–o hello.o;gcc-c Call as
    • Link: gcc hello.o–o hello; gcc-o call ld

The compilation process is difficult to remember, we simplify, the first three steps, GCC parameters linked to "ESc", the corresponding input file suffix is "ISO", so memory is much easier.

Gdb:

Note Use GCC to compile with the "-G" parameter.

    • GdB Programm (Start gdb)
    • B Set Breakpoints (4 breakpoints are set: Line breakpoint, function breakpoint, conditional breakpoint, temporary breakpoint)
    • Run starts running the program
    • BT Print function Call stack
    • P View variable values
    • C continue running from the current breakpoint to the next breakpoint
    • N Single Step operation
    • S Single Step operation
    • Quit Quit GDB

Problem: GDB's n (next) command lets GDB execute the next line and then pause. The s (step) command acts like this, except that the step command enters the function when the function is called, so what should be preferred in actual use? Why?

I think we should first use the n command, which is directly the result of the function, if it is correct, if it is not correct, it indicates a problem inside the function, then use the S command.

    • Change of display tracking variable value
    • Until jump out of the loop
    • Finish Jump function
    • Help

Use VIM to enter the following code and contact use "K" to find the Help document:

Write these six codes in vim first.

The above code in addition to the main function of the code to make a static library math.a and a shared library math.so;

Write makefile, let the code compile, the use of static libraries and shared libraries is automated.

"In-depth understanding of computer systems"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.