:w !python When I open gvim to edit a...">

Python auto start leads to "EOF while reading a string"

I will add a line to my .vimrc

map <F4> :w !python<cr>

When I open gvim to edit a Python file without a name, it has only two lines

x=input("which one do you like ? ")
print(x)

I click F4and get EOF when reading a linehow to solve it?

When you add map <F4> :w<cr>:!python %<cr>or imap <F4> <Esc>:w <cr>:!python %<cr>, it can only make a named file, if it is not a named file, the card will not work, how can I do without a file name?

enter image description here

+4
source share
4 answers
Answer to

@benjifisher is correct. Input (function) is a problem.

:w !python passes a python program through stdin (basically the same as

echo 'input("x")' | python

, ). input() stdin, , python stdin stdin . . input() EOF.

, python stdin, :h :w_c, , stdin cmd, python.

                                                        :w_c :write_c
:[range]w[rite] [++opt] !{cmd}
                        Execute {cmd} with [range] lines as standard input
                        (note the space in front of the '!').  {cmd} is
                        executed like with ":!{cmd}", any '!' is replaced with
                        the previous command :!.

-, stdin, .

,

print(42)

:w !python vim- 42.

, , . , , . , vim. python.

+5

:w , , script , , .

vim <bar> i.e. (|) <CR> ( ) .

.vimrc, F4, , .

, map:

map <F4> :w<cr>:!python %<cr>

imap Esc, :

imap <F4> <Esc>:w <cr>:!python %<cr>
+4

, input(). Python . , (, ), EOF.

+3

- - ( ):

function! GetContentsForPython()
    let contents = join(getline(1,'$'), "\n")
    let res = ''
    for l in split(contents, '\n')
        if len(l)
            let res = res . l . ';' 
        endif
    endfor
    let res = '"' . escape(res, '"') . '"'
    return res
endfunction

noremap <f4> :!python -c <c-r>=GetContentsForPython()<cr><cr>

,

python -c "print 'hello'"

, , , .

+1

Source: https://habr.com/ru/post/1531571/


All Articles