Loading ...
Sorry, an error occurred while loading the content.

Question: Change >% behavior

Expand Messages
  • Ben Kim
    Hi list, In this snippet: ===================== int main() { if (a) { cout
    Message 1 of 3 , Oct 17, 2008
    • 0 Attachment
      Hi list,


      In this snippet:
      =====================
      int main()
      {
      if (a)
      {
      cout << "first" << endl;
      if (b)
      {
      cout << "second" << endl;
      }
      }
      return 0;
      }

      What is the best way to indent them like this?
      ======================
      int main()
      {
      if (a)
      {
      cout << "first" << endl;
      if (b)
      {
      cout << "second" << endl;
      }
      }
      return 0;
      }


      ">%" key combination seems close, but it moves the { lines together.
      >% Move the lines between current bracket and the matched one by one right
      tab, like

      if (b)
      {
      cout << "second" << endl;
      }

      Is there some tweak so {} are not moved together?

      As an alternative:
      =====================
      In simple cases I can go to the outermost { and do
      :+1,/^}/-1s/^/\t/ ... (1)

      but in nested brackets, it of course doesn't work.

      Is there a symbol that indicates the "matched bracket" which I can use
      instead of the /^}/ in (1)?



      Thanks.
      Ben Kim


      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Gary Johnson
      ... Make sure you have cindent set, either by setting it manually or by enabling filetype detection. Then select the region to wish to indent--the entire
      Message 2 of 3 , Oct 17, 2008
      • 0 Attachment
        On 2008-10-17, Ben Kim <bkim@...> wrote:
        > Hi list,
        >
        >
        > In this snippet:
        > =====================
        > int main()
        > {
        > if (a)
        > {
        > cout << "first" << endl;
        > if (b)
        > {
        > cout << "second" << endl;
        > }
        > }
        > return 0;
        > }
        >
        > What is the best way to indent them like this?
        > ======================
        > int main()
        > {
        > if (a)
        > {
        > cout << "first" << endl;
        > if (b)
        > {
        > cout << "second" << endl;
        > }
        > }
        > return 0;
        > }

        Make sure you have 'cindent' set, either by setting it manually or
        by enabling filetype detection. Then select the region to wish to
        indent--the entire region, not just individual blocks--and apply the
        = command to that region. For example, you could put the cursor on
        the { on the line following "main()" and type

        =%

        Or you could move the cursor somewhere inside the main function
        block, but not inside any of the inner blocks, and type

        =aB

        Or you could visually select the main function by putting the cursor
        at the top of the function, hitting

        V

        and moving the cursor to the bottom of the function by whatever
        motion commands work best for you, then type

        =

        to indent the selected region. See

        :help 30.2
        :help usr_43.txt

        > ">%" key combination seems close, but it moves the { lines together.
        > >% Move the lines between current bracket and the matched one by one right
        > tab, like
        >
        > if (b)
        > {
        > cout << "second" << endl;
        > }
        >
        > Is there some tweak so {} are not moved together?

        See

        :help object-select

        > As an alternative:
        > =====================
        > In simple cases I can go to the outermost { and do
        > :+1,/^}/-1s/^/\t/ ... (1)
        >
        > but in nested brackets, it of course doesn't work.
        >
        > Is there a symbol that indicates the "matched bracket" which I can use
        > instead of the /^}/ in (1)?

        Not in a regular expression, no. But that approach is way too much
        work anyway, given vim's object-selection and motion capabilities.

        Regards,
        Gary


        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Ben Kim
        ... Thanks much. Ben Kim --~--~---------~--~----~------------~-------~--~----~ You received this message from the vim_use maillist. For more information,
        Message 3 of 3 , Oct 17, 2008
        • 0 Attachment
          > Make sure you have 'cindent' set, either by setting it manually or
          > by enabling filetype detection. Then select the region to wish to
          > indent--the entire region, not just individual blocks--and apply the
          > = command to that region. For example, you could put the cursor on
          > the { on the line following "main()" and type
          >
          > =%
          >
          > Or you could move the cursor somewhere inside the main function
          > block, but not inside any of the inner blocks, and type
          >
          > =aB
          >
          > Or you could visually select the main function by putting the cursor
          > at the top of the function, hitting
          >
          > V
          >
          > and moving the cursor to the bottom of the function by whatever
          > motion commands work best for you, then type
          >
          > =
          >
          > to indent the selected region. See
          >
          > :help 30.2
          > :help usr_43.txt
          >

          Thanks much.

          Ben Kim

          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        Your message has been successfully submitted and would be delivered to recipients shortly.