2

I need an option in vim to display a text file, say text-file, during the session as would

echo -e `cat text-file` | vim -
  • vim is given text-file
  • text-file includes text with escape sequences, such as:

    FIELD 1             TEXT\tOF\n\tFIELD 2\n
    FIELD 1             TEXT OF FIELD 2
    
  • vim to interpret escape sequences, so that text-file in the example above would be displayed in vim as:

    FIELD 1             TEXT    OF
        FIELD 2
    
    FIELD 1             TEXT OF FIELD 2
    

2 Answers 2

2

A workaround would be to invoke the command from within Vim, allowing you to display escaped characters:

:%!echo -e "$(cat)"

Here, % is a range equal to the entire file, and cat is used to pass the standard input to echo -e.

The problem is that you won't be able to determine which tabs or newlines were delimiters and which were not, thus loosing the ability to revert the file to its original state.

If you have to save the file, you will need to apply a method similar to what garyjohn proposed, that is replacing with substitute (:%s) either delimiters themselves or those found in the data before and after editing.

3
  • thanks LEI. before I read more about Vim, considering the problem you mentioned: can Vim direct its display-text through a pipe, such as the mentioned echo -e... yet meanwhile: the editing/working copy that Vim processes would remain the original? this would be like the preview-window of the stack-exchange post user interface. the text would be passed through the pipe automatically after every command entered. editing would be done as ed-commands in the bottom line (because the cursor would be another problem here).
    – dani2c4
    Sep 22, 2017 at 19:04
  • edit - replace parenthesis at end of comment (because ... here) with: (because the cursor would indicate the wrong location)
    – dani2c4
    Sep 22, 2017 at 19:24
  • As I mentioned I think you can't reliably get back to the original state once echo -e transformed \t and \n, unless your data follows some consistent pattern (then the task might still be complex). You could edit your question to clarify your goal, I am not sure to get it. Also check out :redir to control the output of Vim commands.
    – LEI
    Sep 22, 2017 at 21:16
1

One solution would be to put this line in your ~/.vimrc file,

set listchars+=tab:>-

and when you want to edit a data file, execute

:set list

This will display most special characters so that they can be easily recognized without affecting the layout of the file.

If you really want newlines to appear as \n and tabs to appear as \t, you could execute the following substitutions.

:%s/\n/\\n/g
:%s/\t/\\t/g

When you're done editing, you can convert them back to actual newlines and tabs with the following.

:%s/\\n/\r/g
:%s/\\t/\t/g

Note that Vim represents newlines as \n in patterns and as \r in replacement strings.

For more, see:

:help 'list'
:help 'listchars'
:help /\n
:help sub-replace-special
2
  • thanks, your answer gives a way to display a tab as >---... no? that's useful too, but I intended to ask the the reverse -- how to cause that the esape sequences "\n" and "\t" be displayed as space and tab, respectively. (maybe that wasn't clear: I need practice in editing)
    – dani2c4
    Sep 15, 2017 at 20:03
  • I suppose I focused too much on your line beginning with "solution:" and didn't read the rest closely. In any case, I thought the Vim conceal feature might solve this. It can be used to make the string \n appear as a space, but the replacement character cannot be a control character, so it can't be used to make the string \t appear as a literal tab. So I guess I don't have the answer you were looking for.
    – garyjohn
    Sep 15, 2017 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.