DRINK
DRINK

Reputation: 452

vim printing unexpected output

in my script i run vim like this:

$EDITOR hello.c

then i execute it:

./script.sh hello.c <stdin >>stdout 2>>stderr; echo $? >>errcode

Does anybody know why the output is like:

    [?1049h[?1h=[1;24r[?12;25h[?12l[?25h[27m[m[H[2J[?25l[24;1H"/home/sandbox/ios-15-1/desc1_file/xvysta02/hello.c" 6L, 77C[1;1H[35m#include [m[31m<stdio.h>[m
[32mint[m main()
{
    printf([31m"Hello world[m[35m\n[m[31m"[m);
    [33mreturn[m [31m0[m;
}
[1m[34m~                                                                               [8;1H~                                                                               [9;1H~                                                                               [10;1H~                                                                               [11;1H~                                                                               [12;1H~                                                                               [13;1H~                                                                               [14;1H~                                                                               [15;1H~                                                                               [16;1H~                                                                               [17;1H~                                                                               [18;1H~                                                                               [19;1H~                                                                               [20;1H~                                                                               [21;1H~                                                                               [22;1H~                                                                               [23;1H~                                                                               [m[24;63H1,1[11CAll[1;1H[?12l[?25h[24;1H[?1l>[?1049lVim: Error reading input, exiting...
Vim: Finished.
[24;1H

Instead of this:

running editor with: /.sanitized./hello.c (hash da427fd)

seems like it printout content of file hello.c

It's my school project so the output is from testing script.

Upvotes: 0

Views: 405

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172768

You cannot launch Vim interactively inside a script with its output redirected. Vim prints out various ANSI Escape sequences to control the terminal, and that's what you're seeing. You didn't mention what you're using Vim for, so here's a general introduction to automating text manipulation in a script:

Alternatives

Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed, awk, or Perl / Python / Ruby / your favorite scripting language here.

That said, you can use Vim non-interactively:

Silent Batch Mode

For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute command), use Ex-mode.

REM Windows
call vim -N -u NONE -n -i NONE -es -S "commands.ex" "filespec"

Note: silent batch mode (:help -s-ex) messes up the Windows console, so you may have to do a cls to clean up after the Vim run.

# Unix
vim -T dumb --noplugin -n -i NONE -es -S "commands.ex" "filespec"

Attention: Vim will hang waiting for input if the "commands.ex" file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the - argument.

Full Automation

For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:

vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"

Here's a summary of the used arguments:

-T dumb           Avoids errors in case the terminal detection goes wrong.
-N -u NONE        Do not load vimrc and plugins, alternatively:
--noplugin        Do not load plugins.
-n                No swapfile.
-i NONE           Ignore the |viminfo| file (to avoid disturbing the
                user's settings).
-es               Ex mode + silent batch mode -s-ex
                Attention: Must be given in that order!
-S ...            Source script.
-c 'set nomore'   Suppress the more-prompt when the screen is filled
                with messages or output to avoid blocking.

Upvotes: 1

Walter A
Walter A

Reputation: 20032

vim is using control characters to make an interactive screen. When you want to use vim with redirecting, try something like

vi hello.c >/dev/null 2>@1 <<end
:1,$ s/world/StackOverflow/
:wq
end

The line with end must start at the first column and without spaces after it. When you want to play more with this construction, you can go into insert mode, type something and "hit the escape-key". Put an escaped escape-key using CTRL-V ESC.

Upvotes: 0

Related Questions