Vim Tips Wiki
(Change <tt> to <code>, perhaps also minor tweak.)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipNew
[[Category:Map]]
 
  +
|id=1539
 
  +
|previous=1538
{{Tip
 
  +
|next=1540
|title=Exchanging a word with the adjacent word
 
|created=7 Dec 2007
+
|created=2007
|complexity=intermediate
+
|complexity=basic
|author=[[User:michaelrgeddes]]
+
|author=[[User:michaelrgeddes|Michael Geddes]]
 
|version=6.0
 
|version=6.0
  +
|subpage=/200712
|text=
 
  +
|category1=Advanced Regex
  +
|category2=
 
}}
 
}}
 
 
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>
I'm sure that a version to handle line breaks can be done, especially if you relax the requirement of using ''\k'' to match keyword characters.
 
 
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.
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."
 
   
  +
The traditional way to swap words is something like <code>dawwp</code> (see [[VimTip47]]) or such movements. However these aren't a generic solution and only work where the delimiters are whitespace.
See {{help|/\%#}}.
 
   
  +
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
==Comments==
 
  +
<pre>this->test</pre>
"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:
 
#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.
 
   
  +
becomes
This tip's saving grace is that it highlights the use of the '''<tt>%#</tt>''' atom, which is fairly obscure and probably underused.
 
  +
<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).
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.
 
   
 
The trick for this tip is the '''<code>%#</code>''' 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 <code>\v</code>, "very magic" and of <code>\_.</code>, "any character including newline".
--[[User:Fritzophrenic|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.
 
   
  +
==References==
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.
 
 
*{{help|/\%#}}
  +
*{{help|/\_.}}
   
 
==Comments==
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>'''.
 
--[[User:Michaelrgeddes|Michaelrgeddes]] 21:20, 7 December 2007 (UTC)
 

Latest revision as of 06:31, 13 July 2012

Tip 1539 Printable Monobook Previous Next

created 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[]