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

Move just a word

Expand Messages
  • Garriss, Michael
    Consider the following text: int some_fn( int this, int that); // 2 spaces between int and that This happens to me a lot. I want that above line to look like
    Message 1 of 2 , Nov 1, 2002
      Consider the following text:

      int some_fn( int this, int that); // 2 spaces between int and that

      This happens to me a lot. I want that above line to look like this:

      int some_fn( int this, int that ); // same length as above but that is
      moved over

      The question/request:

      Is there some way to move a word left or right with out changing the rest of
      the line? In other words delete one space on one side and add one space to
      the other.

      TIA

      -Mike Garriss
    • Charles E. Campbell
      ... Of course there s a way, its Vim! :) However, its not as obvious as one might wish. Problems will crop up with single-letter words, etc -- it seems to me
      Message 2 of 2 , Nov 1, 2002
        On Fri, Nov 01, 2002 at 09:19:34AM -0700, Garriss, Michael wrote:
        > Is there some way to move a word left or right with out changing the
        > rest of the line? In other words delete one space on one side and add
        > one space to the other.
        ---------------------------------------------------------------------

        Of course there's a way, its Vim! :)

        However, its not as obvious as one might wish. Problems will crop up
        with single-letter words, etc -- it seems to me your problem is
        related to http://vim.sourceforge.net/tips/tip.php?tip_id=329 . So
        let's see how one might modify the mapping mentioned in that tip to do
        what you want:

        nmap <silent> \wr "_yiw:s/\(\%#\w\+\)\(\s\)/\2\1/<cr>
        nmap <silent> \wl "_yiw:s/\(\s\)\(\%#\w\+\)/\2\1/<cr>

        Yep, with a bit of testing they worked for me. Now for an
        explanation; I'll just explain the first mapping:

        nmap : normal mode mapping

        <silent> : tells vim to be quiet about it
        \wr : the key sequence \wr will shift a word right
        "_yiw : "_ is the "blackhole" register (trash bin)
        "_yiw yanks the inner word to the black
        hole; the purpose here is to move the
        cursor to the beginning of the word, even if
        its only one character long.

        :s/\(\%#\w\+\)\(\s\)/\2\1/ : \%# matches where the cursor is, \w\+
        matches the word, \s matches a trailing
        whitespace. The \(\) allow me to store
        the matching patterns into \1 and \2.
        Change the matching pattern to \2\1;
        ie., reverse the order of the word and space.

        Regards,
        C Campbell

        --
        Charles E Campbell, Jr, PhD _ __ __
        Goddard Space Flight Center / /_/\_\_/ /
        cec@... /_/ \/_//_/
        PGP public key: http://www.erols.com/astronaut/pgp.html
      Your message has been successfully submitted and would be delivered to recipients shortly.