(vim) How to swap two words efficiently ? - Editors

This is a discussion on (vim) How to swap two words efficiently ? - Editors ; Hi, Suppose I am editing the two lines : Give Alice a chocolate. Bob is a bad girl. and I want to swap the two words "Alice" and "Bob". What's the shortest way do to this with vim ? Atm, ...

+ Reply to Thread
Results 1 to 7 of 7

(vim) How to swap two words efficiently ?

  1. Default (vim) How to swap two words efficiently ?

    Hi,

    Suppose I am editing the two lines :

    Give Alice a chocolate.
    Bob is a bad girl.

    and I want to swap the two words "Alice" and "Bob". What's the shortest way
    do to this with vim ?

    Atm, I'm doing :
    ggwdwj^Pldwk^wP

    Thanks in advance,
    Manuel.



  2. Default Re: (vim) How to swap two words efficiently ?

    Sun, 23 Sep 2007 09:53:42 +0200 from mpg <manuel.pg@free.fr>:
    > Atm, I'm doing :
    > ggwdwj^Pldwk^wP


    Yuck! :-)

    Why not

    dwwP

    ?

    Or perhaps, if you want to allow for punctuation,

    dWWP

    ?

    --
    Stan Brown, Oak Road Systems, Tompkins County, New York, USA
    http://OakRoadSystems.com/
    "If there's one thing I know, it's men. I ought to: it's
    been my life work." -- Marie Dressler, in /Dinner at Eight/

  3. Default Re: (vim) How to swap two words efficiently ?

    On Sep 23, 3:53 am, mpg <manuel...@free.fr> wrote:
    > Hi,
    >
    > Suppose I am editing the two lines :
    >
    > Give Alice a chocolate.
    > Bob is a bad girl.
    >
    > and I want to swap the two words "Alice" and "Bob". What's the shortest way
    > do to this with vim ?
    >
    > Atm, I'm doing :
    > ggwdwj^Pldwk^wP
    >
    > Thanks in advance,
    > Manuel.


    Hmm... not sure I can help alot w/ navigating between the two words
    (though, if they're a ways a way, you might want to consider setting a
    mark with m [ see :help mark-motions ])
    But you can save some work by combining your "put Alice" and "delete
    Bob" steps into one "replace Alice with Bob, leaving Bob in the paste
    buffer"

    So, broken down
    ggw # get to Alice
    ma # set a mark so I can come back here later
    de # delete Alice (leaving the trailing space)
    j0 # get to Bob
    ve # select 'Bob' but not trailing space in visual mode
    p # replace 'Bob' with 'Alice' and put Bob in the " register
    (see :help registers).
    `a # jump back to where I deleted Alice (note backtick, not single-
    quote)
    P # put 'Bob' before the trailing space

    so, in total that is
    ggwdej0vep`aP

    For a total of two keystrokes saved.

    If you want to switch ALL the instances of Bob and Alice in a
    document, you might want to try something like

    :%s/\<Alice\>/UNIQUE/ge | %s/\<Bob\>/Alice/ge | %s/\<UNIQUE\>/Bob/ge
    (followed by hitting return/enter/whatever dvorak keyboards have).

    HTH
    Noah


  4. Default Re: (vim) How to swap two words efficiently ?

    rampion wrote:
    >
    > So, broken down
    > ggw # get to Alice
    > ma # set a mark so I can come back here later
    > de # delete Alice (leaving the trailing space)
    > j0 # get to Bob
    > ve # select 'Bob' but not trailing space in visual mode
    > p # replace 'Bob' with 'Alice' and put Bob in the " register
    > (see :help registers).
    > `a # jump back to where I deleted Alice (note backtick, not single-
    > quote)
    > P # put 'Bob' before the trailing space
    >
    > so, in total that is
    > ggwdej0vep`aP


    You cheat !
    >
    > For a total of two keystrokes saved.


    For a total of 0 keystrokes saved.

    Now this is really 2 less :

    2Gdw-wvaWP+P

    For a total of 10 character.


    -ap

  5. Default Re: (vim) How to swap two words efficiently ?

    muede73 wrote:
    >
    > For a total of 10 character.
    >
    >

    In base 12 , of course ;-) .

    -ap

  6. Default Re: (vim) How to swap two words efficiently ?

    muede73 wrote:

    > rampion wrote:
    >>
    >> So, broken down
    >> ggw # get to Alice
    >> ma # set a mark so I can come back here later
    >> de # delete Alice (leaving the trailing space)
    >> j0 # get to Bob

    By the way, we can use + here, saving a character.

    >> ve # select 'Bob' but not trailing space in visual mode
    >> p # replace 'Bob' with 'Alice' and put Bob in the " register
    >> (see :help registers).
    >> `a # jump back to where I deleted Alice (note backtick, not single-
    >> quote)
    >> P # put 'Bob' before the trailing space
    >>
    >> so, in total that is
    >> ggwdej0vep`aP

    >
    > You cheat !


    Indeed, you mean ggwmadej0vep`aP, which is 15 characters, like my original
    way.

    >> For a total of two keystrokes saved.

    >
    > For a total of 0 keystrokes saved.
    >

    Well, I don't know how to count keystrokes. Let's count characters instead.

    > Now this is really 2 less :
    >
    > 2Gdw-wvaWP+P
    >

    And if we know the file is only 2 lines long, we just use G instead of 2G,
    for a new total of 10 characters... in base 11

    Alternatively,
    2Gde-wvepj0P
    seems to do the trick too. We save a character replacing 'vaW' with 've' but
    loose it again with j0 in place of just +.

    By the way, thanks to both of you, since I learned useful tricks I didn't
    know before (visual + paste, and marks).

    Manuel.

  7. Default Re: (vim) How to swap two words efficiently ?

    On Sep 26, 8:39 am, mpg <manuel...@free.fr> wrote:
    > muede73 wrote:
    > > rampion wrote:

    >
    > >> So, broken down
    > >> ggw # get to Alice
    > >> ma # set a mark so I can come back here later
    > >> de # delete Alice (leaving the trailing space)
    > >> j0 # get to Bob

    >
    > By the way, we can use + here, saving a character.
    >
    > >> ve # select 'Bob' but not trailing space in visual mode
    > >> p # replace 'Bob' with 'Alice' and put Bob in the " register
    > >> (see :help registers).
    > >> `a # jump back to where I deleted Alice (note backtick, not single-
    > >> quote)
    > >> P # put 'Bob' before the trailing space

    >
    > >> so, in total that is
    > >> ggwdej0vep`aP

    >
    > > You cheat !

    >
    > Indeed, you mean ggwmadej0vep`aP, which is 15 characters, like my original
    > way.
    >
    > >> For a total of two keystrokes saved.

    >
    > > For a total of 0 keystrokes saved.

    >
    > Well, I don't know how to count keystrokes. Let's count characters instead.
    >
    > > Now this is really 2 less :

    >
    > > 2Gdw-wvaWP+P

    >
    > And if we know the file is only 2 lines long, we just use G instead of 2G,
    > for a new total of 10 characters... in base 11
    >
    > Alternatively,
    > 2Gde-wvepj0P
    > seems to do the trick too. We save a character replacing 'vaW' with 've' but
    > loose it again with j0 in place of just +.
    >
    > By the way, thanks to both of you, since I learned useful tricks I didn't
    > know before (visual + paste, and marks).
    >
    > Manuel.


    And I learned about -,+, and aW
    (sorry about the miscount - more proof that I should double check
    before I post).


+ Reply to Thread

Similar Threads

  1. Efficiently de-duping an array
    By Application Development in forum Perl
    Replies: 8
    Last Post: 08-25-2007, 01:18 AM
  2. Replies: 23
    Last Post: 10-04-2006, 02:26 PM
  3. Efficiently Handling Many Options
    By Application Development in forum Smalltalk
    Replies: 3
    Last Post: 07-25-2006, 05:15 PM
  4. pseudo code for replacing words in a paragraph efficiently?
    By Application Development in forum Inetserver
    Replies: 1
    Last Post: 07-13-2006, 09:44 AM
  5. Efficiently finding combinations
    By Application Development in forum PROLOG
    Replies: 9
    Last Post: 05-23-2005, 04:15 PM