Vim Tips Wiki
Register
m (format change, References section heading)
(Insert TipProposed template + manual clean)
Line 1: Line 1:
  +
{{TipProposed
{{Tip
 
  +
|id=0
|title=Exchanging a word with the adjacent word
 
  +
|previous=0
|created=7 Dec 2007
 
  +
|next=0
|complexity=intermediate
 
 
|created=December 6, 2007
|author=[[User:michaelrgeddes]]
 
 
|complexity=basic
 
|author=[[User:michaelrgeddes|Michael Geddes]]
 
|version=6.0
 
|version=6.0
|text=
 
 
}}
 
}}
 
 
Map a key combination to this regular expression: this transposes the keyword under the cursor with the next keyword, including skipping line breaks.
 
Map a key combination to this regular expression: this transposes the keyword under the cursor with the next keyword, including skipping line breaks.
  +
s/\v(<\k*%#\k*>)(\_.{-})(<\k+>)/\3\2\1/
 
  +
<pre>
 
s/\v(<\k*%#\k*>)(\_.{-})(<\k+>)/\3\2\1/
  +
</pre>
   
 
To replace the keyword under the cursor with the previous keyword (no line breaks), use:
 
To replace the keyword under the cursor with the previous keyword (no line breaks), use:
  +
s/\v(<\k+>)(.{-})(<\k*%#\k*>)/\3\2\1/
 
  +
<pre>
 
s/\v(<\k+>)(.{-})(<\k*%#\k*>)/\3\2\1/
  +
</pre>
   
 
I'm sure that a "previous word" version to handle line breaks can be done, especially if you relax the requirement of using ''\k'' to match keyword characters.
 
I'm sure that a "previous word" version to handle line breaks can be done, especially if you relax the requirement of using ''\k'' to match keyword characters.
   
The traditional way to swap words is something like <tt>dawwp</tt> (see [[VimTip47]]) or such movements. However these aren't a generic solution and only work where the delimiters are whitespace.
+
The traditional way to swap words is something like <tt>dawwp</tt> (see [[VimTip47]]) or such movements. However these aren't a generic solution and only work where the delimiters are whitespace.
   
 
This approach is cleaner about leaving the surrounding non-keyword characters where they are, and works cleanly at the end of lines and over line breaks.
 
This approach is cleaner about leaving the surrounding non-keyword characters where they are, and works cleanly at the end of lines and over line breaks.
   
  +
For example
Eg:
 
this->test
+
<pre>this->test</pre>
  +
 
becomes
 
becomes
test->this
+
<pre>test->this</pre>
   
 
It can also be used with other atoms such as [a-zA-Z0-9] which in some ways is simpler as it can be negated more easily (aiding with the 'reverse' case).
 
It can also be used with other atoms such as [a-zA-Z0-9] which in some ways is simpler as it can be negated more easily (aiding with the 'reverse' case).
   
The trick for this tip is the '''<tt>%#</tt>''' atom, which matches the cursor position. This atom can easily be used in cases like this for acting on text under the cursor. Note the use of <tt>\v</tt>, "very magic" and of <tt>\_.</tt>, "any character including newline".
+
The trick for this tip is the '''<tt>%#</tt>''' atom, which matches the cursor position. This atom can easily be used in cases like this for acting on text under the cursor. Note the use of <tt>\v</tt>, "very magic" and of <tt>\_.</tt>, "any character including newline".
   
 
==References==
 
==References==
Line 35: Line 42:
 
==Comments==
 
==Comments==
 
"Transpose is an Emacs only game no longer." -- [[User:Garoth|Garoth]] 00:26, 7 December 2007 (UTC)
 
"Transpose is an Emacs only game no longer." -- [[User:Garoth|Garoth]] 00:26, 7 December 2007 (UTC)
  +
 
----
 
----
I'm not so sure about this one...seems like another simple application of a substitution command, especially since something similar could be accomplished with regular old command sequences like <tt>dawwP</tt> and <tt>dawbP</tt>. Granted, the application is fairly useful (switching words is something I do quite often) but I think that:
+
I'm not so sure about this one...seems like another simple application of a substitution command, especially since something similar could be accomplished with regular old command sequences like <tt>dawwP</tt> and <tt>dawbP</tt>. Granted, the application is fairly useful (switching words is something I do quite often) but I think that:
 
#This task is fairly simple and really doesn't require a regular expression or even a mapping
 
#This task is fairly simple and really doesn't require a regular expression or even a mapping
#We don't want a bunch of "map this regular expression to do blah" tips...help with regular expressions is fine, a repository of ready-made regular expressions is not. "Teach a man to fish" as they say.
+
#We don't want a bunch of "map this regular expression to do blah" tips...help with regular expressions is fine, a repository of ready-made regular expressions is not. "Teach a man to fish" as they say.
   
 
This tip's saving grace is that it highlights the use of the '''<tt>%#</tt>''' atom, which is fairly obscure and probably underused.
 
This tip's saving grace is that it highlights the use of the '''<tt>%#</tt>''' atom, which is fairly obscure and probably underused.
Line 49: Line 57:
 
I've now worked out how to rename. I realised my mistake with the / as soon as I had done it.
 
I've now worked out how to rename. I realised my mistake with the / as soon as I had done it.
   
Actually, I originally did this in response to an Emacs user in #vim, and it turned out that none of the daawwP etc really did what was required. He wanted a single map that did what the Emacs command did. This one does it over line-breaks which emacs didn't.
+
Actually, I originally did this in response to an Emacs user in #vim, and it turned out that none of the daawwP etc really did what was required. He wanted a single map that did what the Emacs command did. This one does it over line-breaks which emacs didn't.
   
 
I'm beginning to think that originally I might have used '''<tt>[a-zA-Z0-9]</tt>''' or something as the atom rather than '''<tt>\k</tt>'''.
 
I'm beginning to think that originally I might have used '''<tt>[a-zA-Z0-9]</tt>''' or something as the atom rather than '''<tt>\k</tt>'''.
Line 69: Line 77:
   
 
----
 
----
 
BTW, [[User:Fritzophrenic|Fritzophrenic]] is not correct about it being equivalent to the simpler versions, and is very well worth mapping, especially for EMACS users who are used to being able to do this operation. Personally I don't use it though.
 
BTW, [[User:Fritzophrenic|Fritzophrenic]] is not correct about it being equivalent to the simpler versions, and is very well worth mapping, especially for EMACS users who are used to being able to do this operation. Personally I don't use it though.
 
 
--[[User:Michaelrgeddes|Michaelrgeddes]] 21:13, 10 December 2007 (UTC)
 
--[[User:Michaelrgeddes|Michaelrgeddes]] 21:13, 10 December 2007 (UTC)
  +
  +
----

Revision as of 04:04, 5 January 2008

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created December 6, 2007 · complexity basic · author Michael Geddes · version 6.0

Map a key combination to this regular expression: this transposes the keyword under the cursor with the next keyword, including skipping line breaks.

s/\v(<\k*%#\k*>)(\_.{-})(<\k+>)/\3\2\1/

To replace the keyword under the cursor with the previous keyword (no line breaks), use:

s/\v(<\k+>)(.{-})(<\k*%#\k*>)/\3\2\1/

I'm sure that a "previous word" version to handle line breaks can be done, especially if you relax the requirement of using \k to match keyword characters.

The traditional way to swap words is something like dawwp (see VimTip47) or such movements. However these aren't a generic solution and only work where the delimiters are whitespace.

This approach is cleaner about leaving the surrounding non-keyword characters where they are, and works cleanly at the end of lines and over line breaks.

For example

this->test

becomes

test->this

It can also be used with other atoms such as [a-zA-Z0-9] which in some ways is simpler as it can be negated more easily (aiding with the 'reverse' case).

The trick for this tip is the %# atom, which matches the cursor position. This atom can easily be used in cases like this for acting on text under the cursor. Note the use of \v, "very magic" and of \_., "any character including newline".

References

Comments

"Transpose is an Emacs only game no longer." -- Garoth 00:26, 7 December 2007 (UTC)


I'm not so sure about this one...seems like another simple application of a substitution command, especially since something similar could be accomplished with regular old command sequences like dawwP and dawbP. Granted, the application is fairly useful (switching words is something I do quite often) but I think that:

  1. This task is fairly simple and really doesn't require a regular expression or even a mapping
  2. We don't want a bunch of "map this regular expression to do blah" tips...help with regular expressions is fine, a repository of ready-made regular expressions is not. "Teach a man to fish" as they say.

This tip's saving grace is that it highlights the use of the %# atom, which is fairly obscure and probably underused.

The title needs to be changed because of the '/' character, so maybe it can be changed to highlight the use of this atom, like "Swap adjacent words under the cursor with regex" or something like that.

--Fritzophrenic 14:42, 7 December 2007 (UTC)


I've now worked out how to rename. I realised my mistake with the / as soon as I had done it.

Actually, I originally did this in response to an Emacs user in #vim, and it turned out that none of the daawwP etc really did what was required. He wanted a single map that did what the Emacs command did. This one does it over line-breaks which emacs didn't.

I'm beginning to think that originally I might have used [a-zA-Z0-9] or something as the atom rather than \k.

--Michaelrgeddes 21:20, 7 December 2007 (UTC)


Hi Michael - Don't worry, Fritzophrenic is not usually so grumpy! Why don't you create a user page (can be extremely simple - see examples at Special:Listusers), then we can officially welcome you on the talk page of your user page. The simplest way to create your user page would be to log on, then click your name in the Author field at the top of this tip (this will work because you have made your name a link).

I don't think I've heard of %# so I am really impressed by this tip! If you wanted suggestions on your comment above, you might post on the vim_use mailing list. That might be a good idea because it would be good to get more people to examine this. Also, I like to regularly prod them to try and get more involvement here.

See VimTip47 for a previous tip on a similar topic. Ideally, this tip and tip 47 might be merged, or at least should reference each other. Also, this tip should have a bit more explanation of the regex. It doesn't have to go overboard and explain every tiny detail.

I removed [[Category:Map]] because I don't think this tip has anything to do with mapping (nearly all tips could use a mapping, so it's not much use to flag them with that category). Someone wiser than me can later figure out how to categorise this.

FYI: Later we will delete these comments.

--JohnBeckett 00:06, 8 December 2007 (UTC)


BTW, Fritzophrenic is not correct about it being equivalent to the simpler versions, and is very well worth mapping, especially for EMACS users who are used to being able to do this operation. Personally I don't use it though. --Michaelrgeddes 21:13, 10 December 2007 (UTC)