Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting lines in Vim 1

Status
Not open for further replies.

TonyGroves

Programmer
Aug 13, 2003
2,389
IE
This seems such a basic question, but after R'ing TFM I'm still stuck ...

I'm using Vim and I want to split a line by replacing every comma and subsequent whitespace with a newline. Problem is, I can't figure out how to substitute with a special character like a newline, by using a regex (or anything else). Can anybody help?

Thanks a lot.
 
You can use ctrl-V followed by the relevant key to insert non-printing characters into a command or search. In this case, you would use ctrl-V and then hit the enter key.

In your substitution command, when you get to the replacement portion, hit ctrl-V and Vim will display a carat (^) character. When you see this, hit "enter" and the carat will change to ^M (probably displayed in blue), which represents a carriage return, if I recall correctly. Note that just typing the characters "^M" will not work.
 
That sounds like it should work, because the Ctrl-V system certainly works in insert mode.

However, if I'm in command mode and entering the second part of the regex, nothing happens when I press Ctrl-V, then when I press Enter, the unfinished command gets executed.

I'm surprised that Vim doesn't seem to support the inclusion of character codes in replacement strings - something like [tt]:s/,\s*/\0x0a/g[/tt] - or does it?

Thanks.
 
That's strange. It works for me on both my home Slackware system and my WinXP machine at work. Could ctrl-V have been remapped to some other function?

Also, Vim does support hex codes in regexps - it just does it using the ctrl-V trick. So, instead of typing the code 0x0a, you would hit ctrl-V and then type x0a.
 
It was the key mappings; it works now, thanks.

I was using the default key mappings for gVim for Windows, whose .exrc I copied across to my Linux system.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top