Vim Tips Wiki
(Become a text manipulation expert with recursive repeats)
 
(remove author info and signature, add links to related tips)
Line 1: Line 1:
Many times, when dealing with data file in text format (e.g. csv, etc), we often need to massage the data to modify, filter or rearrange them in certain manner. With Vim's complex repeats, such tasks can be done easily without having to be a REGEXpert. :)
+
Many times, when dealing with data file in text format (e.g. csv, etc), we often need to massage the data to modify, filter or rearrange them in certain manner. With Vim's [[VimTip144|complex repeats]], such tasks can be done easily without having to be a REGEXpert. :)
   
 
Let's say we have a text file with the following contents:
 
Let's say we have a text file with the following contents:
Line 43: Line 43:
   
 
With this knowledge, you can now quickly perform complex editing of any structured text with relative ease. Have fun! :)
 
With this knowledge, you can now quickly perform complex editing of any structured text with relative ease. Have fun! :)
  +
  +
==See also==
  +
*[[Recursive mappings|This can also be done with a mapping]]
  +
  +
==Comments==
   
 
----
 
----
--[[User:Hongleong|Hongleong]] 08:16, 7 November 2008 (UTC)
 
<span style="color:gray;">''Do not believe in anything merely on the authority of your teachers & elders. But after observation and analysis, when you find that anything agrees with reason and is conducive to the good and benefit of one and all then accept it and live up to it.''</span>
 

Revision as of 15:18, 10 November 2008

Many times, when dealing with data file in text format (e.g. csv, etc), we often need to massage the data to modify, filter or rearrange them in certain manner. With Vim's complex repeats, such tasks can be done easily without having to be a REGEXpert. :)

Let's say we have a text file with the following contents:

dr-------- 20906
drwx------ 20913
drwxr-x--- 20704
drwxr-xr-x 21104
lrwxrwxrwx 20606
-------r-- 21004
-rw-r----- 20716
-rwxrwx--- 21102

For some reason, we want to move the 5-digit number at the end of the line to after the last "r" character on the same line, and we want to repeat it for the whole file. Notice that all these numbers begin with "2" and there can be multiple "r" per line.

Here's the end-result that we want:

dr20906-------- 
dr20913wx------ 
drwxr20704-x--- 
drwxr-xr21104-x 
lrwxrwxr20606wx 
-------r21004-- 
-rw-r20716----- 
-rwxr21102wx--- 

Yes, this can probably be done via :substitute with back references, but that can be intimidating for some. An easier, more flexible and often quicker method to accomplish the same task is to use "complex repeats", which really isn't too complex.

Here's how:

  1. Place the cursor on the 1st character of the 1st line of the file.
  2. Hit the following keys to record the repeat-sequence:
    • qqq : Start and immediately end recording for register q -- this essentially empties register q
    • qq : Start recording actions into register q
    • f2 : Find the "2" character to the right on this line (cursor will jump there)
    • D : Delete and yank the 5-digit number into memory
    • Fr : Find the "r" character to the left on this line (cursor will jump to the last "r")
    • p : Paste the number that was yanked earlier after the "r" character
    • <enter> : Move the cursor to the next line
    • @q : Execute the contents of register q (itself!), which is for now, empty, so nothing will happen. The trick here is that when we run it again with "@q" after you have ended the recording, it will call itself after processing each line! This recursive loop will process all the subsequent lines in that file, until it hits the end-of-file. This is the essence that makes complex repeats so flexible and powerful.
    • q : End the recording
  3. Hit @q to apply the same manipulation to the rest of the file from the 2nd line onwards. Enjoy the art of flying cursor and automatic text manipulation on your screen. :)

With this knowledge, you can now quickly perform complex editing of any structured text with relative ease. Have fun! :)

See also

Comments