Http://cygwin.com/index.html or look at the official website!
Many Windows users are not used to Linux development environments. Although I would like to try it out, I am often afraid that the Linux system will disrupt my normal life: 1> install the Linux system and break down the windows system, resulting in my failure to live normally; 2> Linux development is too difficult to get started. writing the first helloworld is no less difficult than Java, and the Environment configuration is hard to understand. My opinion on this is: whether the road is flat or steep, you will always go. If you are willing to invest in the Linux development community, you will not install the Linux system, it cannot be imagined that the work environment will not be configured. (In fact, you really need to understand the principles of a lot of things, otherwise it is difficult to troubleshoot: for example, disk boot principle, partition structure principle, LINUX partition structure and file system, environment variable settings, a wide variety of compressed package installation package unzip installation method, user management permission management and other common commands, so that the driver installation system, culture, and so on, are trivial ).
Http://www.fanqiang.com)
This article tries to skip this difficult step and enable a Linux simulator in a win environment (which is basically the same as the command line development environment in Linux). In just 20 minutes, it teaches you how to make a pure helloworld compiled by GCC in Linux. It's like trying it before buying a snack. Isn't it a pleasant thing? (Note: In fact, cygwin not only has this simulation function, but it is also a very effective tool for porting UNIX <--> win programs, and someone uses it for Embedded System Development)
1. What is cygwin?
To answer this question, you 'd better Google "cygwin's history" and perhaps get a more detailed answer. In short, cygwin is a Linux Simulation Environment running on the Windows platform. It is a free software developed by Cygnus solutions. (The company has developed many good things, famous for its ECOs, but it is now acquired by RedHat ). Many friends do not understand the differences and connections between Linux and Unix. I would like to give a brief introduction here. UNIX is a registered trademark. It is an operating system that can be authorized to use only when a large number of conditions are met and a considerable fee is paid. Linux is a clone version of UNIX, it is an operating system created by its founder Linus and many world-renowned hackers. Why is there a lot of software between Linux and UNIX that can be easily transplanted? Because Linux also meets the POSIX specifications, the operating mechanism is similar to Unix.
Here is a reference to the Internet (Source: http://blog.csdn.net/glock18/archive/2004/07/10/38275.aspx ),
It is used to explain how cygwin works: Cygnus first improved GCC, GDB, gas and other development tools so that they can generate and explain Win32 target files. Then they will port these tools to the Windows platform. One solution is to greatly modify the source code of these tools based on Win32 APIs, which obviously requires a lot of work. Therefore, they adopt a different method-they write a shared library (cygwin DLL), and call the Unix-style calls (such as fork, spawn, signals, select, sockets, and so on) are encapsulated in, that is, they are based on
Win32 API writes a simulation layer of the UNIX system library. In this way, as long as the source code of these tools and the shared library are connected together, you can use the cross compiler on the unix host to generate a tool set that can run on the Windows platform. Based on these development tools transplanted to the Windows platform, Cygnus gradually uses other tools (almost no need to modify the source code, but only need to modify their configuration scripts) the software is transplanted to Windows. In this way, running bash, development tools, and user tools on Windows seems to work on UNIX. For a more detailed description of cygwin implementation, please refer to the http://cygwin.com/cygwin-ug-net/highlights.html
Ii. Install cygwin.
The Installation File of cygwin can be easily found by Google. At present, there are two types of websites in China: "network installation version" and "Local installation version. The standard release version should be the network installation version. There is no difference between the two. The following describes the installation process.
Step 1. Click the Installation File (setup.exe) after the download. the first screen is the GNU copyright description. Click "next (N)-> ",
Go to the installation mode selection screen.
Step2. the installation modes include "Install from Internet", "Download Form Internet ",
"Install from local directory. "Install form Internet" is installed directly from the Internet, suitable for fast network. If you are not as fast as you are, or you want to save the downloaded installation file after installation, you should choose "Download Form Internet" Next time you no longer download and directly install it ", download and install the file (about 40 MB ).
In fact, the so-called "Local installation version" is also a package that someone else downloads all files from the Internet (applicable to China's national conditions)
Step select the installation Destination path and the path of the installation source file, and then enter the path where the installation package is selected.
Note that this is a major event. I did not see this step during the first installation. The GCC was not installed, and thus the file could not be compiled.
+ All default
+ Admin default
....
+ Devel default
+ Editors default
....
As shown in, you can double-click a node in the Treeview to change its status, such as default, install, uninstall, and reinstall. The default status is default. Many tools are not installed by default.
Here, I chose install on the default next to the all line and install it all to avoid future problems. (Note: The tree control here is different from the one below win. You can try to have a different response when you click "all" and "default" next to "all)
Step 4. Click next and the installation is successful. It automatically creates a shortcut on your desktop.
Now, let's start my Linux journey. Double-click cygwin to enter the system.
First, we will introduce several simple Linux commands.
PWD displays the current path
CD changes the current path. If no parameter exists, enter the home directory of the corresponding user.
Ls to list files in the current directory. This command has n multiple parameters, such as LS-Al
PS: list the current system process
Kill to kill a process
Create a directory using mkdir
Rmdir Delete directory
Rm delete file
Renamed the MV file or directory
Man online help
Less: displays the last few lines of a file.
Most Linux commands have many parameters and can be used in combination. Therefore, whenever you do not know or cannot remember which parameter to use, you can use man to find the switch. For example, if you want to find how to use ls, you can type
$ Man ls
The system Echo information is as follows:
Ls (1) FSF ls (1)
Name
Ls-list directory contents
Synopsis
Ls [Option]... [file]...
Description
List information about the files (the current directory
Default). Sort entries alphabetically if none of-cftusux
Nor -- sort.
-A, -- all
Do not hide entries starting.
-A, -- almost-all
Do not list implied. And ..
-B, -- escape
Print octal escapes for nongraphic characters
-- Block-size = size
Use size-byte Blocks
:
It's all about it, hey.
Let's write a hello World Program.
# Cd
The/home/Administrator directory is displayed. My current Logon account is administrator.
# Mkdir Source
Create a subdirectory called Source
# Cd Source
Go to/home/Administrator/source
# Vim hello. c
Start the vim editor to compile the program. Now it is necessary to briefly introduce vim.
In the Linux field, there are two most historical editors. One is VI, and the other is Emacs. VI has now evolved into vim, which is more powerful than the current VI. Vim and Emacs are two completely different things. Vim emphasizes using simple commands to complete functions, whether it is searching, replacing,
Regular Expression matching, compilation, linking, troubleshooting, inter-function jump, and so on are all completed in the command line, and it also integrates the direction keys on the four keys of hjkl. It can be said that, with vim, you can basically skip the mouse: P Emacs is a powerful tool to make a fuss about version control, simulate a variety of editing environments, and perform various operations on text, A powerful text processing system. Emacs is controlled by a combination of keys such as CTRL + ALT + K. Vim is controlled by simple commands such as Se (SET.
Because cygwin only provides Vim (Can I install Emacs myself? I haven't tried it yet), let's first try VIM: P
Vim loads text files in command mode and insert mode. The insert mode, as its name implies, is to input and edit text; the command mode is to input various Vim control commands, which are commonly used:
I enter the editing mode
H Shift left
J move down
K move up
L right shift
W disk storage
Q exit
! Emphasize execution
Some commands can be used in combination. If you modify a file and want to save the disk and exit, you can use WQ. If you want to discard the disk and exit directly, you can use Q! Now, let's try it out. Remember: In edit mode, return to command mode, press "ESC", and then press ":". In input command, switch from command mode to edit mode and use I, it must be hard to use Vim for the first time. Hey hey, I am used to it when I use Vim for the first time. It is really effort-saving.
Edit the hello. c file and enter:
# Include
Int main (void ){
Printf ("Hello world! ");
}
Then, enter the WQ command to return to the command line.
Enter the compilation command:
# GCC hello. C-O hello
After compilation is successful, you can check
# Ls
Look at hello.exe.
Okay. Let's run it.
#./Hello
What is the effect? Good, right?
Okey, now playing a C ++ Hello world,
# Vim world. cpp
Input:
# Include
Using namespace STD;
Void main (){
Cout <"Hello world! ";
}
Compile the C ++ program using G ++
# G ++ world. cpp-O world
Run it,
#./World
How can this problem be solved? All of you are experts. It is no longer a problem to compile some simple programs at this step: P
Okay. How can I use GDB to write makefile files. Vim introduction Source: http://elephant.linux.net.cn/articles/vim.php? Lang = ZH
VI improved (Vim) is a text editor developed by Bram moolenaar that is compatible with the universal text editor VI in UNIX. It supports syntax discoloration, regular expression matching and replacement, insertion completion, custom keys, and other functions, providing great convenience for editing text, especially programming. Vim can run on "any" operating system, including common windows and Unix/Linux. Once you have mastered vim, you have mastered a cross-platform tool.
Although Vim is very powerful, it is not very easy for people who are new to it, especially those who are familiar with notepad similar to Windows. After all, it is compatible with VI rather than notepad. This article aims to introduce the usage I know in Vim. I hope more people like vim. It should be pointed out that Vim has too many functions and commands that you don't use, so there is no need to remember them. I don't know some of them. It depends on you to find out, the key is to read more: Help and try multiple times. In addition, depending on your vim version and settings during compilation and installation, some functions or commands described in this article may not exist in your vim. If necessary, please upgrade.
1. Basic usage of vi
The VI screen area is divided into two parts: the bottom line is the command line, which is generally used to prompt information or command line input; in addition to this is the text display area. Different from notepad, there are two modes in VI: Normal Mode and insert mode.
After entering VI, the default mode is normal. After a newbie enters VI for the first time, he wants to enter a string of characters. The result shows that VI has a series of inexplicable responses. In normal VI Mode, any buttons, including common characters, represent a command and do not insert characters at the current cursor. Common commands: (case sensitive)
: Enter the command line
I or a enters the insert mode. The difference is: After I enters the insert mode, the cursor is before the current character; after a enters the insert mode, the cursor is behind the current character
H j k l: move the cursor to the left, move down, move up, and move right (generally you will not use them to move the cursor, just press the direction key)
X delete a character
Dd deletes a row
J. Delete the carriage return of the row and add the next row to the end of the row.
The character R replaces the cursor with the new character
^ $ Move the cursor to the beginning and end of the line
The number G moves the cursor to several rows. If you press G directly, it moves to the last row.
In normal mode, the command is input as a key. In the command line, the command is input as a string. The following are common command lines:
: Q exit! (To be more precise, close the current file)
: W file name storage disk. If the file is still saved as the current file, you do not need to write the file name.
: Log out of the WQ disk.
: Open the new file name or create a new file (close the current file at the same time ). If the file name is not specified or the file name does not exist, the file is created.
: HELP! Use Q to close the window. You can add a help topic name after help, for example, help dd Or: Help help
Another point is that if a command is warned (execution is denied), you must add an exclamation mark after the command word to force execution. For example, if you have modified the file but want to discard the disk and exit, if you enter Q, VI will tell you that the file has been modified. In this case, you can only enter Q! Exit. Another example: W! A.txt indicates saving the current file as a.txt, regardless of whether a.txt already exists.
You don't need to talk about the insert mode. Add your new content. However, you may find that the use of the backspace key and the delete key is very limited, and you cannot select, copy, or paste the backspace key as NotePad does. These are all because they are VI standards. We will see later that Vim's extended functions can solve these problems. Remember to Press ESC to exit the insert mode and return to normal mode.
2. copy and paste
To facilitate Text Selection, VIM introduces the visual mode. To select a text segment, first move the cursor to the beginning of the segment, press V in normal mode to enter the visible mode, and then move the cursor to the end of the segment. Note that the character of the cursor is included in the selection area. In this case, you can perform some operations on the selected text. Common (visual mode) Commands include:
X or d cut (that is, delete, and the selected text enters the clipboard)
Y Replication
Replace all characters of the r character with new characters
U ~ All letters are written less, capitalized, and case-insensitive.
> <Indent and reverse indent respectively
After the command is entered, VIM will return to normal mode. You can press P or P to paste it. Commands for copying and pasting in Normal Mode:
V enters the visual mode
P or P paste the content of the clipboard at the current position. P sticks to the character behind the cursor, and P sticks to the front.
I have to admit that although the visual mode is introduced, copying and pasting in VIM is still troublesome, which may be the only drawback of vim. :-) In addition, VIM also introduced the select mode, similar to the visual mode. With some key definitions and option settings, you can achieve the same copy and paste usage habits as Notepad. The length and level are limited.
3. Vim Customization
Vim extends many functions and commands Based on VI and provides many options. However, some functions are disabled by default, and some options may not meet your usage habits. Therefore, we need to write a vimrc file. In the DOS/Windows Vim version, this file should be placed in the vim directory with the file name "_ vimrc ". In Vim of UNIX version, this file can generally be placed in the user's personal home directory, and the file name is ". vimrc ". At the startup of VIM, each line in the vimrc file will be executed as a command line in sequence. We can add several commands to the file so that Vim can automatically enable some useful functions at startup, define common shortcut keys.
The following is an example of a vimrc file: (Note that the line feed of the browser is different from the actual line feed .)
"Set the flexibility of backspace and delete. If backspace is set to 2, there is no restriction.
Set backspace = 2
"Sets the modes in which the mouse function is used. Mouse = A indicates all modes.
Set mouse =
"Set the path. This parameter is involved in commands such as F.
"For Windows programming, path can be set
"Set Path =.," C:/program files/Microsoft Visual Studio/vc98/include ",,
"For Unix programming, path can be set as follows:
Set Path =.,/usr/include,/usr/include/Qt ,,
"Display the row and column positions of the cursor
Set ruler
"Set the Hop Distance
Set tabstop = 4
"Set the number of automatically indented Cells
Set shiftwidth = 4
"Enable auto indent
Set autoindent
"Set which keys can be bypassed between lines. If the following settings are used, backspace and direction keys can be bypassed between lines.
Set whichwrap = B, S, H, L, <,>, [,]
"The current File Syntax automatically changes color. Vim recognizes the syntax of hundreds of text files, such as HTML, C ++, and Java.
Syntax on
"The following are my habits and key definitions to facilitate switching between the insertion mode and the visual mode.
"(1) Enable x d y to automatically return the insert mode
Vnoremap y "ryi
Vnoremap X "rxi
Vnoremap d dI
"(2) both normal and insert modes can be pasted
IMAP: If COL (".")! = 1 EXE 'normal "RP 'elseexe 'normal" RP 'endif '[I
NMAP "RP
"(3) in normal and insert modes, you can press to enter the visual mode.
IMAP: If COL (".")! = 1 EXE 'normal lv' elseexe 'normal V' endif
NMAP v
Below are my commonly used vimrc files. In addition, your vim installation directory may also contain a default vimrc file, which you can refer. If you encounter commands or parameters that you have never seen before, you can look at the help.
* Vimrc
4. Other Vim commands
If you really want to use vim, the basic commands of VI alone will not work. Here we will introduce more commands. Some of the following commands are specific to vim and some also exist in VI, so I will not differentiate them. ":" Indicates that the command is input in the command line. "I" indicates that this is a command in the insert mode, and other commands in the normal mode. CTRL + X.
4.1 help
In: Help, you can press Ctrl +] to jump to a hyper-connection.
In: Help, press Ctrl + T to jump back
4.2 open multiple files
: Split the file name into a new window to open the specified file. If the file name is omitted, the current file is still displayed, which can be used to observe different parts of the file at the same time. (Note the difference with: New)
F. Split the file name pointed by the cursor. Vim searches for the file name in the path. For example, it is often used to open the file in the # include statement.
When several files are opened at the same time, switch between windows
_ When several files are opened at the same time, press _ to maximize the current window
: Set scrollbind. All windows with the volume binding attribute set will be rolled together. You can use: Set noscrollbind to unbind
4.3 revocation and recovery
Errors are inevitable during the editing process, but it does not matter. Vim allows unlimited revocation. As long as you haven't closed the file, you can even undo it all the time and return to the status when the file was just opened and started working a few hours ago.
U undo
Redo
4.4 string SEARCH replacement
/Search string down
? Search string up
* # Search for the words pointed by the cursor downward and upward respectively
N repeat the previous search
: Start row, end row S/search string/replace string/g from start row to end row, replace all search strings with replace string
: Set ignorecase settings ignore uppercase and lowercase letters. You can use: Set noignorecase to cancel ignoring uppercase and lowercase letters.
For example,/Hello searches for "hello" from the current cursor position. commands without strings/You can repeat the previous search, which is equivalent to N. Another example is: 1, $ S/Hello/Hi/g. Change "hello" in the full text to "hi", where "$" indicates the last line. In addition, you can select a piece of text in visual mode, press: Enter the command line and enter S/Hello/Hi/g. Vim will replace the text in the selection area.
The search string uses a regular expression (Regular Expression), many of which have special meanings:
/Cancel the special meanings of the subsequent characters. For example,/[Vim/] matches the string "[Vim]".
[] Match one of them. For example, [Vim] matches the letter "V", "I", or "M", and [A-Za-Z] matches any letter.
[^] Match is not one of them. For example, [^ Vim] matches all characters except the letter "V", "I", and "M ".
. Match any character
* Match the first character to be greater than or equal to zero times. For example, VI * m matches "VM", "Vim", and "viim "......
/+ Match the first character to be greater than or equal to one time. For example, VI/+ M matches "Vim", "viim", and "viiim "......
/? Match the previous character Zero or once. For example, VI /? M matches "VM" or "Vim"
^ Match the beginning of a row. For example,/^ hello, search for the word "hello" at the beginning of the row.
$ Match the end of a row. For example,/Hello $ search for the word "hello" at the end of the row.
// (/) Enclose a regular expression
/The number matches the expression enclosed in the previous segment repeatedly. For example,/(Hello/). */1 matches a string whose start and end are "hello" and whose center is any string.
To replace a string, you can use "&" to represent the entire search string, or use "/number" to represent an expression that contains a certain segment of the search string.
To give a complex example, put all the strings in the text "ABC ...... Replace XYZ with xyz ...... ABC can be written as follows:
: % S/ABC/(. */) XYZ/1abc/g
: % S // (ABC/)/(. */)/(XYZ/)/3/2/1/g
For more details about regular expression search replacement, see help pattern.
4.5 insert completion
In insert mode, VIM provides several shortcut keys to reduce repeated input keys. When you want to enter a string that has been input in a context, you only need to enter the first several characters, with the shortcut key, VIM searches for the context, finds the matching string, and completes the remaining characters, so you don't have to knock on it. In this way, it doesn't matter how long your variable name is during programming, and you can also reduce input errors. In my opinion, insertion and completion are all the most prominent functions of vim.
I. For example, if you want to input another filename, you only need to press F. If Vim searches up and finds that the first match starting with F is not filename, you can continue to search for the next match for completion. Of course, if you want to succeed at one time, you can enter a few more characters, such as filen, and then press "complete ".
I. Search down and add all the words
I. For example, you have written a row for (INT I = 0; I <>. If the line is not the one you want, you can press or select the previous or next matching row.
I search in the file system and fill in all file names
If the match is not found in the current file after the word is pressed or supplemented, VIM searches for the file in the # include statement, and the file location searches in the path.
4.6 key definition
In vim, you can define a key. If you press this key, it is equal to pressing a string of predefined keys. For example
: Map! DDI
This means that if you press DDI consecutively in the insert mode, VIM will return to the normal mode, delete a row, and then enter the insert mode.
The map command has many variant forms. The keys defined for each variant form are valid only in some modes, but not in other modes. Use the correct form as needed:
: The NMAP key is only valid for normal mode.
: The IMAP key is only valid for the insert mode.
: The vmap key is only valid for the visual mode.
: The cmap key is valid only under the command line.
: The map key is valid in both normal and visual modes.
: Map! The key is valid in both the insert mode and command line.
Note that if you define map d dI, this will cause a loop definition error. In this case, you need to use: noremap d dI to define. Similarly, noremap has different pattern variations.
For example, if you want to insert the string "ABC" in the penultimate character of each line of the file, you can define
: NMAP $ hiabcj
In normal mode, click "move" once: move the cursor to the end of the row, move the cursor to the left to enter the insert mode, enter "ABC", return to normal mode, and move the cursor down a row. Solve the problem without interruption. This is a commonly used method for temporary processing of multi-row repeated operations. Of course, this is a stupid method. :-) The definition of the previously written pair is a more complex usage of map. You can also define
: Map: W :! Gcc-o % <-I/usr/include/Qt-lqt %
: Map :!. /% <
The current file is compiled and executed.
The completion line mentioned in insert completion needs to be pressed. If you are in trouble, you can define
: Inoremap
Reduce the number of keys. Similarly, similar definitions can be made.
4.7 Other commands
:! Execute an external command on the command line.
. Repeat the previous operation at the current position of the cursor
I. Enter special ASCII characters or keys for subsequent characters. In addition to the insert mode, it is also applicable to command lines. Subsequent characters can be any key on the keyboard, or three decimal digits to represent the character ASCII code
I command to execute a normal mode command, after the execution is complete, return to the insert mode
I jump to the definition line of the identifier referred to by the cursor. For example, if you encounter a createwindow function during programming and want to see its definition statement, you can press I on it. Vim will open a new window and move the cursor to its definition. Of course, the premise is that the createwindow definition exists in the current file or its # include file. This also involves path settings. However, VIM is not necessarily accurate.
K. view the man help page of the identifier indicated by the cursor.
I. Copy the characters in the corresponding column of the previous row.
I copied the characters in the corresponding column of the next row (I used it when writing this line)
The cursor points to an integer plus one.
The integer indicated by the cursor minus one
The cursor returns to the previous position. It is equivalent to a "undo" move by the cursor"
The cursor returns to a later position. It is equivalent to "Restoring" when the cursor moves"
Go to the visual mode and select a rectangular area. This command is usually used to operate on multiple rows and columns. Then press I to insert characters in each row before the block; press a to insert characters in each row after the block; Press X, D, or Y to cut or copy;
: X encrypts the current file
5. Some new features of VIM 6.0
The Vim September 26, 2001 release in 6.0 added some new features that were not previously available. The following is a brief introduction. For more details, see help.
* Open the Directory
In Vim 6.0, commands such as: New and: Split can not only open common files, but also open directories. After a directory is opened, the file information will be listed. You can press enter to continue opening the corresponding file or subdirectory, or press? You can get help for other directory operations (such as modifying file names and deleting files.
* Fold
When a text is too long and you don't care about the long content, you can fold the lines that you don't care about and let them disappear from your sight. The collapsed row is replaced by a line of display, for example:
+ -- Row 217:2. Vim Customization---------------------
Folding can be controlled in multiple ways. You can change it by setting the value of the foldmethod option. By default, foldmethod = manual is manually folded. The following describes several simple commands for folding:
(In visual mode) ZF manually creates a fold. Select a piece of text in visual mode, and then press ZF to manually create a fold
The left or right direction keys are folded. In normal or insert mode, moving the cursor horizontally over the collapsed row opens the collapsed row.
ZC hide
* Vertical Split Window
: Vertical Split Window for vsplit file names. Similar to: the split command, but the new window and the original window are parallel.
* More regular expressions
Vim 6.0 has added many new regular expressions than before, among which I think the most useful is the line feed match/n. In previous versions, regular expressions can only match on the same row. Now, as long as/N is explicitly given, the regular expression can span multiple rows.
* Diff Mode
This mode is used to compare and edit two or more files with similar content. In general, for example, if you want to compare and edit the files a and B, you can run the command line to start vim-d a B or, in this way, open file a first, and then open file B in vsplit, enter the command diffthis for files a and B. At this time, VIM will clearly compare the differences between the two files, which is very convenient to edit.
Take a closer look, this diff mode uses a lot of VIM features, including rolling binding, folding, and syntax discoloration. So advanced!
The Vim functions that I am familiar with have been introduced almost, and the rest will be learned by yourself. In fact, VIM has many other functions. For more information, see help. Try again. The help of VIM is quite easy to see. I believe you will have more surprises after reading it. In addition, there are a lot of useful tips on the vim homepage, which is also a good place to go. If you have any experiences or new discoveries, please discuss them with me.
Hope you like vim.