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
- 12 Little Things That You As An Ateneo Hearter Can Do To Help Your Country | Yoo Jin and Akeem's Blog on Bayanihan Linux: True Blue Pinoy Linux
- [RESOLVED]Can you do this in aspx page | ASP Questions & Answers on Tip: How to Decode CSR
- How to Add or Remove Library Path in RHEL | PinoyTux Weblog on What are Libraries in Linux?
- 博文：《RHEL4 安装yum》 - 苗乡小鱼的博客 on Howto: Install yum On RHEL 4
- Hiding Files Inside An Image in Linux | SACHIN JINDAL'S BLOG on Hide Files Inside An Image File