0

Basically I'm transposing my one record with many columns to many records in one column. A non-whitespace character begins a Word. I'm using gVim on Win7 so I need to use CTRL-Q<CR> as my new line character. The number of spaces between words is not constant.

My attempt: :%s/\S/<CTRL-Q><CR>\S/g

Questions

  1. Why does my attempt not work?
  2. How can I do this in gVim?

Convert

foo  1true   0false +pos -neg ++dblpos    (lefp        }rcurl

to

 foo 
 1true 
 0false 
 +pos 
 -neg 
 ++dblpos 
 (lefp 
 }rcurl
  • 1. Why not set the fileformat to unix and use :%s/\S/\r&/g? 2. Do you want to those leading spaces in front of each line given in the example output? 3. Your attempt will leave trailing whitespace, if used as intended. Do you want that? – muru Sep 7 '15 at 5:52
  • @muru. Thanks for suggesting setting format to unix. After doing that, and trying your suggestion, I get a new line after each character not each word. Yes, I'm content with leaving trailing white space. I can use a second command to clean those. – zundarz Sep 7 '15 at 6:00
  • 1
    Oh, wait. You want <CR> line endings, so the fileformat would be mac, not unix - sorry about that. Ok, instead of replacing non-whitespace characters with themselves, replace whitespace characters: :%s/\s\+/\r/g – muru Sep 7 '15 at 6:09
  • I just tried your second suggestion on ff=unix and that worked. Thanks. – zundarz Sep 7 '15 at 6:14
  • 2
    When replacing with \r, Vim will pick the correct line ending for the fileformat, so it will always appear to work. If you wanted OSX line endings, I think you should use ff=mac. Check the content of the file using od or hexdump to be sure. – muru Sep 7 '15 at 6:16
1

It would be simpler if:

  1. You set the fileformat to unix, so that Vim takes care of line endings;
  2. You replace the whitespace instead of the non-whitespace characters.

Thus:

:set fileformat=unix
:%s/\s\+/\r/g

When \r is in the replacement of a substitute command, Vim will pick the appropriate line ending for the file format.

Your attempt would not have worked, since \S in the replacement is just S, not the character that matched.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.