Monday, December 30, 2013

Vim tips and tricks for developers

http://www.openlogic.com/wazi/bid/326642/Vim-tips-and-tricks-for-developers


The Vim text editor provides such a vast set of features that no matter how much you know, you can still learn new and better techniques. If you're a programmer, here are some tips and tricks to help you do things such as compile your code from within Vim, or save your changes when you've edited a file but later realized that you should have opened it using sudo.
To take advantage of these tips you should have a basic understanding of Vim editor modes and understand the difference between normal and command-line modes.

Delete complete words

Suppose you're writing a program that has a function declaration like
void anExampleOfAVeryLongFunctionName(int a, char b, int *c);
and suppose you wanted to declare five more functions with the same return types and arguments. You'd probably copy and paste the existing declaration five times, delete the function name in each declaration, and replace it with the new function name. To speed things up, instead of deleting the name using the backspace key, place the cursor on the function name and enter diw to delete the whole function name in a go.
Generally speaking, use diw to delete complete words. You can also use ciw to delete complete words and leave the editor in insert mode.

Delete everything between parentheses, braces, and quotes

Suppose you have a situation similar to the first example in which you need five more function declarations with the same name and return type, but with different arguments – a practice known as function overloading.
Again, the common solution would be to copy and paste the first declaration five times, delete the individual argument lists, and replace them with new argument lists. A better solution is to move the cursor below the opening parentheses and enter the di( command to delete the complete argument list. Similarly, ci( deletes the list and leaves the editor in insert mode with the cursor positioned between the parentheses.
Along similar lines, the di" and ci" commands delete text between double quotes, and the di{ and ci{ commands delete the text between braces.

Compile code from within the editor

Programmers usually exit Vim or use a different window or tab to compile the code they've just edited, which can waste a lot of time when you do it repeatedly. However, Vim lets you run shell commands, including compiles, from within the editor by entering :! command. To compile the C program helloworld.c from within the file, for instance, you would use the command:
:! gcc -Wall helloworld.c -o helloworld
The output of the command is displayed at the command prompt. You can continue working at the command prompt or press Enter to go back to the editor. If you've already executed a command this way, you can simply type :! next time and use the up and down arrow keys to select the command.
Rarely, you may need to copy and paste the output of a command into a file you're editing into Vim. You can do that with the command
:.! command
This takes the content of the buffer displayed at the command prompt and pastes it into the code. The dot (.) between the colon and the exclamation represents the current line. If you want to dump the output at some other line, say line number 3, you can enter :3! command.

Save typing and improve accuracy with abbreviations

Programmers tend to do a lot of debugging by adding print statements. For a C program, for instance, you might add multiple printf() statements by writing one statement, copying it, pasting it elsewhere, then replacing the debugging text.
You can reduce the time this takes by creating an abbreviation for printf() – or for any text string. The following command creates the abbreviation pf for printf("\n \n");:
:ab pf printf("\n \n");
After you've created this abbreviation, whenever you type pf and press the space bar, Vim will enter the complete function call. An abbreviation declared this way lasts only for that particular editing session. To save the abbreviation so that it is available every time you work on Vim, add it (without the colon) to the file /etc/vim/vimrc.
You can also use abbreviations with opening braces, brackets, and quotes so that their closing counterparts appear automatically with a command such as :ab ( ().
To disable an abbreviation use the command
:unab abbreviation

Use % to jump between parenthesis and brace delimiters

Sometimes, while performing a code review or debugging a compilation error, you may need to match and jump between opening and closing parentheses or braces – a task that may not be easy if you have complicated conditions and densely nested code blocks. In these situations, move the cursor to an opening or closing brace or parenthesis and press %. The cursor will automatically jump to the matching delimiter.

Use . to repeat the last edit

Sometimes developers define functions by copying and pasting declarations from header files to source files, removing the trailing semicolons, and adding a function body. For instance, consider this set of declarations:
int func1(void);
int func2(void);
int func3(void);
int func4(void);
int func5(void);
Here's a trick that makes adding bodies to all these functions easier:
  • Make sure that Vim is in normal mode.
  • Move the cursor to the beginning of the declaration of func1 – that is, below the i.
  • Press A. The cursor should move past the last character in the line (;) and the editor should enter insert mode.
  • Use the backspace key to delete the semicolon.
  • Press Enter.
  • Add a pair of braces and a return statement between them.
At this stage, the declaration set should look like:
int func1(void)
{
return 0;
}
int func2(void);
int func3(void);
int func4(void);
int func5(void);
  • Press Esc to make sure that the editor is back in normal mode.
  • Press j. four times to move down to subsequent lines and make similar changes to the remaining four functions.
Generally speaking, you can use dot (.) to repeat the last edit you made to the file.

Select large number of lines using visual mode

As we have seen, programmers do a lot of copy-and-paste work within their code. Vim provides nyy and ndd commands to copy and delete n lines at a time, but counting a large number of lines is a tedious and time-consuming task. Instead, you can select lines just as you'd do in a graphical text editor by enabling visual mode in Vim.
For example, consider the following C code:
vim helloworld resized 600
To select both the main() and func1() functions, first move the cursor to the beginning of main() function, then press v to invoke visual mode. Use the down arrow key to select all the lines you want:
vim select lines resized 600
Finally, use the yy or dd commands to copy or delete the selected lines.

More tips

Here are a few more commands in normal mode that can help programmers save time:
  • Use the = command to indent a line and =G to indent a file from the current cursor position to the end of the file. You can also use gg=G to indent a complete file irrespective of the cursor position. To define the indentation width, use :set shiftwidth=numberOfSpaces.
  • Programmers who work on Image, video and audio files can use :%!xxd to convert Vim into a hex editor, and :%!xxd -r to revert back.
  • Use :w !sudo tee % to save a file that requires write permissions but that you accidentally opened without using the sudo command.
Learn these tricks and use them in your day-to-day programming work to save time. You may also want to read a few related articles on Wazi: Tips for Using Vim as an IDE, Vim Undo Tips and Tricks, and Create Your Own Syntax Highlighting in Vim.

No comments:

Post a Comment