How to efficiently transpose rows into columns in Vim?
perl, vim
Solution
Basically `:.s/ /`SpaceCtrl+vEnter`/g`Enter`jma:.s/ /`Ctrl+vEnter`/g`EnterCtrl+v`'axgg$p'adG` will do the trick. :)
OK, let's break that down:
- `:.s/ /`Ctrl+vEnter`/g`Enter: On the current line (`.`), substitute (`s`) spaces (`/ /`) with a space followed by a carriage return (SpaceCtrl+vEnter`/`), in all positions (`g`). The cursor should now be on the last letter's line (`e` in the example).
- `j`: Go one line down (to `A B C D E`).
- `ma`: Set mark `a` to the current position... because we want to refer to this position later.
- `:.s/ /`Ctrl+vEnter`/g`Enter: Do the same substitution as above, but without the Space. The cursor should now be on the last letter's line (`E` in the example).
- Ctrl+v`'a`: Select from the current cursor position (`E`) to mark `a` (that we set in step 3 above), using the block select.
- `x`: Cut the selection (into the `"` register).
- `gg`: Move the cursor to the first line.
- `$`: Move the cursor to the end of the line.
- `p`: Paste the previously cut text after the cursor position.
- `'a`: Move the cursor to the `a` mark (set in step 3).
- `dG`: Delete everything (the empty lines left at the bottom) from the cursor position to the end of the file.
P.S. I was hoping to learn about a "built-in" solution, but until such time...
Problem
I have a data file like the following: ``` ---------------------------- a b c d e ............. A B C D E ............. ---------------------------- ``` But I want it to be in the following format: ``` ---------------------------- a A b B c C d D e E ... ... ---------------------------- ``` What is the quickest way to do the transformation in Vim or Perl?