How to Use Vim to efficiently Write c

Source: Internet
Author: User

We recommend a good article about how to use Vim to write c efficiently. The ctags function is similar to source insight.
If you want to work in Linux, you don't need to copy the code to Windows every time, and then use source insight ....... It's too slow and troublesome.
The advantages of using ctags are described below. I think there is another point that is related to the system. You can find a macro definition Ctrl-], so you don't need to use grep any more. Pai_^
Another feature is not mentioned in the following article: It is to position the cursor to a system function, such as sprintf, hold down shitf + k, ^ _ ^ will certainly be a surprise! More efficiency blocks than man sprintf each time. If it wasn't for this article, it would be transferred to Emacs.

Use Vim to write C/C ++ programs
VI is the most common text editor in UNIX. Vim (VI improved) is its enhanced version. Some people often compare Vim with Emacs. Although Vim does not have as many features as Emacs, it is more convenient and easy to use than Emacs. People who have used Emacs will be worried about some shortcut keys of Emacs. Sometimes they really want to have eight hands. The learning curve of VIM is not as long as that of Emacs. You only need to check the built-in vitutor of VIM, and you only need to know how to use Vim in about 20 minutes. For C/C ++ programmers in UNIX, I recommend that you use a combination of VIM + ctags to write programs. It was originally because of listening to me. (The Introduction to VIM is mainly for Versions later than 5.6)
Vim supports syntax guidance and automatic indentation
Vim automatically sets syntax highlighting Based on the file suffix, such as *. C, *. cpp, *. CC, and so on ). Some Reserved Words in C/C ++, such as if, for, and include, will have different colors, which will reduce the possibility of programmers making mistakes, reading a program is easier. Run the following commands to view help for syntax guidance in VIM: ": H Syntax", ": H syn-qstart", and ": H syntax-printing ".
Vim also supports automatic indentation Based on the GNU style, so that the Code Compiled by the programmer is easier to read, and also helps cultivate a good programming style. Run the following commands to view help for automatic indentation in VIM: "H cindent", ": H cinoptions", ": H cinoptions-values", and ": h cinkeys ",": H cinwords ".
Use ctags
The ctags program has been included in vim. Although ctags supports other editors, it only officially supports vim. Ctags can help programmers easily browse source code. Run the following command to create the "tags" file in the root directory of the source code:
[/Home/Brimmer/src] $ ctags-R
"-R" indicates recursive creation, which includes the source program under all subdirectories under the source code root directory. The "tags" file contains a list of these objects:
L macro defined by # define
L enumerated variable value
L function definition, prototype, and Declaration
L namespace)
L type definition (typedefs)
L variables (including definitions and declarations)
Class, struct, Enum, and Union)
L class, structure, and union member variables or functions
Vim uses this "tags" file to locate the marked objects above. The following describes how to locate these objects:
1) Use the command line. Add the "-T" parameter when running vim, for example:
[/Home/Brimmer/src] $ vim-T foo_BAR
This command will open the file defining "foo_BAR" (variable or function or other), and position the cursor to this line.
2) run the ": Ta" command in the vim Editor, for example:
: Ta foo_BAR
3) the most convenient way is to move the cursor over the variable or function name, and then press "Ctrl-]". Use ctrl-O to return the original location.
Note: When running vim, you must run it in the directory where the "tags" file is located. Otherwise, run the ": Set tags =" command to set the path of the "tags" file before vim can find the "tags" file.
Quick Error Correction
In the vim Editor Environment, you can use ": Make" to compile the program. Of course, the premise is that there is a MAKEFILE file in the current directory. After ": Make" is run, if an error occurs in the program, it is displayed. At this time, the cursor will automatically point to the first place where an error occurs, and you can also see the error prompt. Then, you can correct the error, instead of finding the line with the error. Remember the following useful commands:
L ": CL" listing errors
L ": CN" points the cursor to the next error
L ": CP" points the cursor to the previous error
L ": cnew" starts from scratch
You can even let Vim identify other compilers rather than GCC error prompts. This is useful to some programmers who develop embedded systems, because they may not use GCC but other compilers. By setting the value of "errorformat", VIM can identify the error prompt of the compiler. Because the error messages of different compilers are different, you need to reset them if you are not using gcc.
The value of "errorformat" is a string in the format of scanf in C.
The GCC "errorformat" value is: % F: % L:/% m. "% F" indicates the file name, "% L" indicates the row number, and "% m" indicates the error message.
Use ": H errorformat" to view detailed help information.
Use ": H quickfix", ": H make", ": H makeprg", and ": H errorfile" to view other information.
Useful shortcut keys
The following shortcut keys are helpful to programmers:
Move cursor in Function
[[Go to the previous "{" in the first column
] To the next "{" in the first column
{Go to the previous empty line
} Go to the next empty line
GD refers to the definition of the local variable to be transferred to the current cursor
* Go to the next occurrence of the word indicated by the current cursor
# Go To The last occurrence of the word pointed by the current cursor
Matching of parentheses
% Is used to match parentheses, braces, and braces. It depends on what symbol the cursor points.
Others
The Vim configuration file is ". vimrc". You can use this file to configure vim. The version of Vim's graphical interface is gvim. Vim is also available for Windows platforms.
Vim Resources
Vim homepage: http://www.vim.org /.
Copyright description
The copyright of this article belongs to author Brimmer (brimmer@linuxaid.com.cn) and www.linuxaid.com.cn. If you want to reprint it, add a copyright notice.
Brimmer homepage: www.linuxaid.com.cn/engineer/brimmer. HTML and Word documents can be found on the homepage.

 

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.