) Use Vim to write programs.

Source: Internet
Author: User
12.12.06 use Vim to write a program

Posted in Linux related § Linux at PM by Zhou. B02
Tags is Vim

Algorithms have been developed to generate practical code. The efficiency in MATLAB is really low. I only use C, and I did not find a user-friendly pure C compiling environment in windows, so I decided to compile the program in Linux. The environment is VIM + GCC + GDB.

I am a pragmenter. Although I love new technologies, I must be able to use these functions. Vim has been used for several years, but in fact I will only use some of the simplest operations. It is enough at ordinary times and I will forget it when I do not need to learn it, but now we need to program in Vim. It is estimated that there will be more than 10 thousand lines. This is not enough. So in the past few months, I found that Vim is used more frequently than before.

The following operations are available in any document that introduces Vim operations. They are very simple, but I propose them because I think they are particularly useful for programming. Add ":" In command mode, but not in normal mode.

  • G, $, 0, which are instructions to jump to the end of the document, the end of the line, and the beginning of the line.
  • : N, for example: 0,: 200, n is a number that can be jumped to any row.
  • :? STR,:/Str. The search is respectively up and down, and STR is the content to be searched.
  • %, Jump to the corresponding brackets, such as int main (INT argc, char * argv []), put the cursor on "(", % will let the cursor jump ")", if the cursor is at "]", % redirects the cursor "[". After vim7.0, it seems that there is no big use, because there is a highlighted display.
  • Replace: The replace of VIM is very flexible. It depends on the individual. Because I don't often use it, I still need to check documents every time I need a complicated replacement. Even so, I still think it may be very helpful to master replacement when writing large programs.
  • : V, block operation, mainly used to copy, cut, and paste large code segments. Usage: v. the area between the current cursor and the subsequent target cursor is highlighted. The subsequent operation "Y" is copy, and "D" is cut. Then the content is in the buffer zone, if you need to paste the content to another place, move the cursor here "p ".
  • U, CTRL + R, undo and redo.
  • : W file,: WQ,: Q. After the file is edited, W file is saved as another disk, w Q is saved and exited, and Q is exited. If yes, if yes: Q won't let you quit. You need to strengthen the notation "! "To Q !.
  • : Shell, "Ctrl + z" + "FG ". In many cases, You need to exit the shell to perform some operations, and then return immediately. It is very troublesome to exit and enter again. There are two ways to quickly solve this problem. : Shell: run a new shell in vim. "Ctrl + z" enables Vim to run in the background and return to the shell interface. If you want to return to VIM, type Fg in shell. These two methods have different principles, but I think they can accomplish the same purpose.
  • : Make. Do not exit Vim to run make in the local directory. It is convenient to modify program bugs.
  • Split the screen. I think this operation is the most important for programmers. You often need to view multiple program files, but only one Vim interface can be seen on the text interface, I learned to split the screen when using gvim in windows. In a word, I learned many common operation hotkeys in gvim. CTRL + W, V Vertical Split screen, CTRL + W, N horizontal split screen, split screen can be nested, CTRL + W, CTRL + W switch screen focus. In addition, CTRL + W and other Jian combinations can also adjust each split screen, for example, + "|" can maximize the current split screen, + "=" can divide two split screens and so on.

To program with vim, you also need to set the necessary environment for vim. The Vim environment configuration file for each user is ~ /. Vimrc. It is necessary to configure the following environment variables:

  • Set tabstop = N
  • Set softtabstop = N
  • Set shiftwidth = N
  • Set expandtab
  • Set cindent
  • Syntax on

The first four determine the tab style. For details about how to set the tab style, see help tabstop in Vim, which provides four setting styles. Cindent settings enable Vim to enable automatic indent for C language. Syntax on enables the highlight switch. On the Internet, there are still many well-written vimrc that can be used for reference. Most of them will provide some additional convenience for programming.

Many programmers are not satisfied with this. Vim is just an editor after all. It is a good tool like VC to compile programs. We can see all functions, jump to declarations, and complete functions. Fortunately, compared with other editors, VIM can still be extended. Many third-party plug-ins now enhance this feature. Common examples include ctags, cssag, taglist, cppcomplete, and global. My current environment makes ctags + taglist. I personally feel that it is enough. cppcomplete can partially complete the code. I don't think it is easy to use or use it, after all, we can work in the text interface and cannot have too many requirements.

  • Ctags: http://ctags.sourceforge.net /. This is a powerful tool that can analyze the association between code in a specified directory and generate a tags file in the respective directories, when you specify the tags file in the vim program, the cursor can jump to the function or variable Declaration place. Ctags has many parameters that I usually use as follows:
  • Ctags-r 'Find sourcedir-name "*. c" ''find headerdir-name" *. H "'
  • For convenience ~ Set "set tags =./tags" in/. vimrc so that you do not need to set tags in VIM every time. Note that Emacs also has a ctags, which is different from the ctags. If Debian is used, use apt-Get install exuberant-ctags.
  • Taglist: http://www.vim.org/scripts/script.php? Script_id = 273. It can create a split screen on the left side of VIM to display the file or the tag including other files. This effect is obviously similar to that in VC.

I like the simple style on the Linux text interface. After this setting, I enjoy this style and improve programming efficiency.

 

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.