abstract
| - 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 be done via :substitute with back references (Try: %s/\v(.+r)(.+)\s(\d{5})/\1\3\2/), 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 aren't too complex. Here's how: 1.
* Place the cursor on the first character of the first line of the file. 2.
* Hit the following keys to record the repeat-sequence: 3.
* qqq : Start and immediately end recording for register q -- this essentially empties register q 4.
* qq : Start recording actions into register q 5.
* f2 : Find the "2" character to the right on this line (cursor will jump there) 6.
* D : Delete and yank the 5-digit number into memory (rest of line) 7.
* Fr : Find the "r" character to the left on this line (cursor will jump to the last "r") 8.
* p : Paste the number that was yanked earlier after the "r" character 9.
* Enter : Move the cursor to the first non-whitespace character of the next line 10.
* @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. 11.
* q : End the recording 12.
* 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. This works much the same way as typing 9999@q to execute the 'q' register as many times as the count you pass, except that you do not need to think about the size of your file before doing so to make sure enough 9s are added.
|