In vim, you can run commands to simplify tasks. One case is to replace spaces with newline, or any character.
Inside vim, go to command mode by pressing the ESC key. Then enter the following command:
This will substitute (%s) spaces (\s) with return or newline (\r) and continue the replacement until end of line (g).
This is the command to use if you need to replace newline with spaces:
This will substitute (%s) newline (\n) with space (\s) and continue the replacement until end of line (g).
You may notice that newline and return characters are used differently and not interchangeable. This is because \r means ‘carriage return’ and if ever you had one of those typewriting classes wherein you learn how to use a typewriter (not a computer!), you will know what carriage return means. Anyway, in computing, it means the same thing, to move the cursor to the beginning of the line. Although it means almost the same, newline or \n does things differently. Newline character, or ‘line feed’, instructs the cursor to move to the next line.
So how is this relevant to the vim commands above?
When the user presses the ‘Enter’ key, the equivalent command is actually ‘CR-LF’, a combination of carriage return (CR) and line feed (LF)on Windows systems. In Linux systems, the ‘Enter’ key is just ‘LF’, it is understood that you want the cursor the start at the beginning of the line.
However, in replacing \n and \r characters in vim, these two are not interchangeable. If you need to add line feed, use the CR character, \r. If you need to remove the line feed, use the LF \n character.
Subscribe to Email Feeds
- Vim: How to Copy Multiple Lines
- Required Packages for Oracle 11gR2 on Red Hat Enterprise Linux 6
- How Replace Space with Newline in Vim
- 3ix Web Hosting Complaints | First WebHosting on Backspace Key Not Working in PuTTY
- Cheap Web Hosting 3ix | Best in Webhosting on Cebu Pacific Sucks
- 3ix Web Hosting | Best in Webhosting on See Actual Street Pictures with Google Street
- 3ix Web Hosting Complaints – Raw Ask on Cebu Pacific Sucks
- The Best Place to Work: Google and their Office in Zurich | Viral News Worthy on Google Fun Facts