Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a code like this:

588          while ($linecounter > 0) {
589             $token = " ";
590             while ($token != PHP_EOL) {
591                 if (fseek($handle, $pos, SEEK_END) == -1) {
592                     $beginning = true;
593                     break;
594                 }
595                 $token = fgetc($handle);
596                 $pos--;
597             }
598             $linecounter--;
599             if ($beginning) {
600                 rewind($handle);
601             }
602         }

The cursor is at the character = in the line 590.

Which is the most efficient way to select the code block:

  1. lines 590-597
  2. lines 591-596 (just the inner part)
share|improve this question

migrated from serverfault.com Aug 6 '11 at 12:42

This question came from our site for system and network administrators.

up vote 15 down vote accepted

To do the first:

  • Hit $ to go to the end of the lineover the {
  • Push v or V (depending on whether you want to select lines or not)
  • Push % (to jump to the matching bracket).

To select just the inner part, go inside the inner part and use the i{ directional modifier.

For example, to delete everything inside the current {…} block, type: di{.

share|improve this answer
Wow, I have just learned how to use yi(, thanks :) – takeshin Aug 11 '11 at 9:05
  1. $ to jump to the end of the line you're on (to be over the opening brace)
    v to begin a selection (V for whole lines)
    % to jump to the matching brace.

  2. j to go down one line so your cursor is inside the braces you are trying to select
    v to begin a selection (V for whole lines)
    i{ select everything inside the braces

share|improve this answer
+1 for nice formatting – takeshin Aug 11 '11 at 9:06
Method 2 is probably the best method here, since the $ movement in method 1 only works because the opening brace is at the end of the line. Method 2 works without the j movement if your cursor is anywhere within the braces. – SpoonMeiser Oct 11 '11 at 11:23

To expand on Caleb's answer just slightly, vi{ will select the "inside" of the code block. To include the "outside" of the code block, ie including the braces, use va{.

This won't include the while stanza though. To do that you can use o to move the cursor to the beginning of the selection, and then 0 to move the selection to the beginning of the line.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.