Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents

· Introduction to Operating Systems
· Linux Basics
· Working with the System
· Shells and Utilities
· Editing Files
· Basic Administration
· The Operating System
· The X Windowing System
· The Computer Itself
· Networking
· System Monitoring
· Solving Problems
· Security
· Installing and Upgrading
· Linux and Windows

Glossary
MoreInfo
Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
FAQ
Copyright Info
Terms of Use
Privacy Info
Disclaimer
WorkBoard
Thanks
Donations
Advertising
Masthead / Impressum
Your Account

Communication
Feedback
Forums
Private Messages
Surveys

Features
HOWTOs
News Archive
Submit News
Topics
User Articles
Web Links

Google
Google


The Web
linux-tutorial.info

Who's Online
There are currently, 72 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

  

HOWTO Home

Current HOWTO: C-editing-with-VIM-HOWTO


Moving around.

2. Moving around.

2.1. w, e and b keystrokes

One can use the w, e and b keys to move around a file. VIM is capable of recognizing the different tokens within a C expression.

Consider the following C code

Figure 1. A C snippet

 
		  ...
		  if(( NULL == x ) && y > z )
		  ...
	       

Assume that the cursor is positioned at the beginning of the if statement. By pressing w once the cursor jumps to the first (. By typing w again the cursor moves to NULL. Next time the cursor will move to the == token. Further keystrokes will take you as follows. x... )... &&... y... >... z... and finally )...

e is similar to w only that it takes you to the end of the current word and not to the beginning of the next word.

b does the exact opposite of w. It moves the cursor in the opposite direction. So you can moving backwards using the b keystroke.

2.2. {, }, [[ and ]] keystrokes

The { and } keys are used to move from paragraph to paragraph. When editing C files these keys have a slightly different meaning. Here a paragraph is taken as a bunch of lines separated by an empty line.

For Example

Figure 2. Another C snippet

The above snippet shows two paragraphs. One can easily move from the beginning of one to the other, by using the { and } keys. { will take the cursor to the paragraph above and } will take the cursor to the paragraph below.

Many people have the coding style where a logical set of statements are grouped together and separated by one or more blank lines.

For Example

Figure 3. Another C snippet

The { and } keys are very useful in such situations. One can very easily move from one "paragraph" to another.

Another set of keys which are useful are the [[ and ]] keys. These keys allow you to jump to the previous { or next { in the first column.

For Example

Figure 4. The next snippet of C code

Lets say you were editing foo() and now you want to edit bar(). Just type ]] and the cursor will take you to the opening { of the bar() function. The reverse is slightly different. If you were in the middle of bar() and you type [[ the cursor will move to the first { above i.e. the beginning of bar() itself. One has to type [[ again to move to the beginning of foo(). The number of keystrokes can be minimized by typing 2[[ to take the cursor to the beginning of the previous function.

Another set of similar keystrokes are the ][ and [] keystrokes. ][ takes the cursor to next } in the first column. If you were editing foo() and wanted to go to the end of foo() then ][ will take you there. Similarly if you were editing bar() and wanted to go to the end of foo() then [] would take the cursor there.

The way to remember the keystrokes is by breaking them up. The first keystroke will indicated whether to move up or down. [ will move up and ] will move down. The next keystroke indicates the type of brace to match. If it same same as the previous keystroke then the cursor will move to {. If the keystroke is different then the cursor will move to }.

One caveat of the ]], ][, [[ and [] keystrokes is that they match the braces which are in the first column. If one wants to match all braces upwards and downwards regardless of whether its in the first column or not is not possible. The VIM documentation has a workaround. One has to map the keystrokes to find the braces. Without spending too much time on mapping, the suggested mappings are

:map [[ ?{<CTRL-VCTRL-M>w99[{

:map ][ /}<CTRL-VCTRL-M>b99]}

:map ]] j0[[%/{<CTRL-VCTRL-M>

:map [] k$][%?}<CTRL-VCTRL-M>

2.3. % keystroke

The % is used to match the item under the cursor. The item under the cursor can be a parenthesis, a curly bracket or a square bracket. By pressing the % key the cursor will jump to the corresponding match.

Amongst other things, the % keystroke can be used to match #if, #ifdef, #else #elif and #endif also.

This keystroke is very useful in validating code that one has written. For Example

Figure 5. The next snippet of C code

Checking the above code will involve checking the correctness of the parenthesis. The % can be used to jump from one ( to its corresponding ) and vice versa. Thus, one can find which opening parenthesis corresponds to which closing parenthesis and use the information to validate the code.

Similarly the % can also be used to jump from a { to its corresponding }.


The Linux Tutorial completely respects the rights of authors and artists to decide for themselves if and how their works can be used, independent of any existing licenses. This means if you are the author of any document presented on this site and do no wish it to be displayed as it is on this site or do not wish it to be displayed at all, please contact us and we will do our very best to accommodate you. If we are unable to accommodate you, we will, at your request, remove your document as quickly as possible.

If you are the author of any document presented on this site and would like a share of the advertising revenue, please contact us using the standard Feedback Form.


  

Looking for a "printer friendly" version?


Login
Nickname

Password

Security Code
Security Code
Type Security Code


Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!


Amazon Wish List

Did You Know?
You can choose larger fonts by selecting a different themes.


Friends



Tell a Friend About Us

Bookmark and Share



Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.06 Seconds