0

My text in vim looks like,

i am one line
i come in next line
    i come after a tab space

Can the above text be converted like this,

i am one line\ni come in next line\n\ti come after a tab space
  • 1
    :set list will show hidden characters for you, and also check variable listchars. I don't think you can concatenate all lines like that. – cuonglm Sep 11 '15 at 4:19
1

From my understanding of your question you want to convert newlines to the literal sequence backslash followed by n and convert tabs to the literal sequence backslash followed by t.

For replacing the tab character, sure, no problem, it's the same as normal string replacement. Try this:

:%s/\t/\\t/

Note: the recognition of escape sequences like \t in search patterns may only work in Vim. I'm not sure. If it does not work in your vi then you must type an actual tab character in that location. The tab will show up as ^I (but it is not the same as ^ followed by I).

:%s/^I/\\t/

For the newline, try the same thing:

:%s/\n/\\n/

Again, this might be Vim-only, I'm not sure. For replacing newlines, you may be out of luck in regular vi because vi is fundamentally a line-based editor.

| improve this answer | |
  • If vim was non basically a line editor as well, the vim people should stop claiming that vim is vi compatible. The vi interface definition is based on a line editor. – schily Sep 11 '15 at 8:19
  • @schily Vim has very good vi compatibility, to the point that I only ever noticed one difference that bugged me between Vim in compatibility mode and the Solaris vi I "grew up" with. And I contributed a patch to Vim to fix that in 2003, so even that's long gone. The fact is, we could do much worse than Vim as the default vi-like editor in most GNU/Linux distributions. – Celada Sep 12 '15 at 3:43
  • If you ask me, Vim is welcome to add features that original vi can't do, such as the ability to substitute newline characters at the end of lines (effectively merging lines), so long as it doesn't conflict with or change or break true vi functionality. – Celada Sep 12 '15 at 3:43
  • Vi allows to combine lines with the yank command and the fact that this is a special command in vi verifies the underlying line editor. With my ved (available in the schily tools), you just delete the newline character at the end of the first line. – schily Sep 12 '15 at 7:52
  • Compatibility is important in special when is applies to a standard. But from what I have been told from friends who use vi is that vim is not fully compatible with vi and the standard while the Solaris vi is. What difference did you see? So maybe this is one of the criticism from my friends... – schily Sep 12 '15 at 7:55

Your Answer

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