replace space with new line in vi
how to replace every space with newline?
For example, if I have "i want to go to school", i want to convert it to ===== I want to go to school ====== I try :%s/ /\n/g , but it does not seem to work Also is there a way to count the number of space or number of word within vi? |
Use Control-V and then Enter key instead of \n.
Control-V is the special character escape key. |
wow! it works great..
Do you happen to know how to count number of word, or number of provided character? For example, I want to know how many character "x" or number of "<tag>" is in the text. Is there a way to do it? |
Vim doesn't do this natively, but there may be plug-ins that can help at vim.org.
This is the type of thing you can write a script for, and call from vim. You can do some trickery. Perform a global substitution of your char or word, and see the status line at the bottom. It will tell you the number of replacements. Then, just undo! :%s/x/Z/g then see at the bottom: 4 substitutions on 3 lines |
another way, without undo
Code:
:%s/e/&/g |
Quote:
Code:
:%s/\u//gn |
These are good suggestions. The thing that I don't like about not undo'ing is that it changes the file modified status. I've been a :set autowrite/^Z user for 25+ years, and don't want file modifications times changing unless there really are changes. No big deal if the file is changing anyway though. Good suggestions.
|
Quote:
The reason it doesn't work is that \n has a different meaning in the replacement part of :substitue. See :help sub-replace-special for more information. Instead, you'll want to use the command :%s/ /\r/g. Use \s instead of a space if you want to include tabs, too. And \s\+ if you want it to replace consecutive spaces and/or tabs with one newline. |
All times are GMT -5. The time now is 03:47 AM. |