0

Supposing I have the following python script open in Vim:

a=1
b=2
c=a+b

I know that I can type

:! python3

in normal mode either for the whole script or by selecting the rows that I want to run in visual mode. But I can't run the first two lines and the third line separately without error as a and b won't be remembered by the interpreter.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

I did find this proposed solution but it doesn't solve the problem for me. Using :python3 (without the "!") results in "E471: Argument required". Someone at work implied from this that I need to compile Vim with Python3 support, but from researching this, it seems that I have it - see the output from the terminal command which has the "+" for python3/dyn

vim --version

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Apr 29 2022 00:00:00) Included patches: 1-4845 Modified by bugzilla@redhat.com Compiled by bugzilla@redhat.com Huge version without GUI. Features included (+) or not (-): +acl +file_in_path +mouse_urxvt -tag_any_white +arabic +find_in_path +mouse_xterm -tcl +autocmd +float +multi_byte +termguicolors +autochdir +folding +multi_lang +terminal -autoservername -footer -mzscheme +terminfo -balloon_eval +fork() +netbeans_intg +termresponse +balloon_eval_term +gettext +num64 +textobjects -browse -hangul_input +packages +textprop ++builtin_terms +iconv +path_extra +timers +byte_offset +insert_expand +perl/dyn +title +channel +ipv6 +persistent_undo -toolbar +cindent +job +popupwin +user_commands -clientserver +jumplist +postscript +vartabs -clipboard +keymap +printer +vertsplit +cmdline_compl +lambda +profile +vim9script +cmdline_hist +langmap -python +viminfo +cmdline_info +libcall +python3/dyn +virtualedit +comments +linebreak +quickfix +visual +conceal +lispindent +reltime +visualextra +cryptv +listcmds +rightleft +vreplace +cscope +localmap +ruby/dyn +wildignore +cursorbind +lua/dyn +scrollbind +wildmenu +cursorshape +menu +signs +windows +dialog_con +mksession +smartindent +writebackup +diff +modify_fname +sodium -X11 +digraphs +mouse -sound -xfontset -dnd -mouseshape +spell -xim -ebcdic +mouse_dec +startuptime -xpm +emacs_tags +mouse_gpm +statusline -xsmp +eval -mouse_jsbterm -sun_workshop -xterm_clipboard +ex_extra +mouse_netterm +syntax -xterm_save +extra_search +mouse_sgr +tag_binary
-farsi -mouse_sysmouse -tag_old_static system vimrc file: "/etc/vimrc" user vimrc file: "$HOME/.vimrc" 2nd user vimrc file: "~/.vim/vimrc" user exrc file: "$HOME/.exrc" defaults file: "$VIMRUNTIME/defaults.vim" fall-back for $VIM: "/usr/share/vim" Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -DSYS_VIMRC_FILE=/etc/vimrc -D_REENTRANT -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 Linking: gcc -L. -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -fstack-protector-strong -rdynamic -Wl,-export-dynamic -Wl,--enable-new-dtags -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -L/usr/local/lib -o vim -lm -lselinux -lncurses -lsodium -lacl -lattr -lgpm -Wl,--enable-new-dtags -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -fstack-protector-strong -L/usr/local/lib -L/usr/lib64/perl5/CORE -lperl -lpthread -lresolv -ldl -lm -lcrypt -lutil -lc

I would be very grateful for any help. My OS is Fedora 35.

  • 2
    You could look into using two tmux panes (or GNU screen) - one running vim, and the other running python. The with a vim plugin vim slime you can 'send' whatever lines you want over to the python process. – mattb May 13 '22 at 23:04
  • 1
    when you say having vim "remember" the code that was run... do you mean precisely like a repl? – alec May 14 '22 at 00:27
  • 1
    :python3 requires a statement to run; see :help python3. You might also be interested in :py3file or something like :execute 'python3' getline('.') – D. Ben Knoble May 14 '22 at 01:48
  • @mattb Thanks, will give that a go. – Damask_Rose May 14 '22 at 21:32
  • @D.BenKnoble Many thanks, that is exactly what I was looking for and clears up a lot of questions I had. Just one more, with :py3file the contents of :help py3file gives :[range]py3f[ile] {file} but when I enter, say :1,2 py3file myscript.py the whole script is executed, not just lines 1 and 2 (same if I try selecting those lines in visual mode first) - have I misunderstood or misused [range]? – Damask_Rose May 14 '22 at 21:37
  • @Damask_Rose frankly I’m mot sure what [range] does for any except :pydo :( it’s under-documented. – D. Ben Knoble May 14 '22 at 22:34
  • I recommend using https://github.com/bfredl/nvim-ipy , the vim counterpart is https://github.com/ivanov/vim-ipython – eyal karni May 14 '22 at 23:13
  • I use Vim-slime for this sort of thing. Googling for vim slime or vim repl will turn up a bunch of alternatives. – Rich Oct 14 '22 at 09:42

1 Answers1

1

If you want vim to execute the code internally, :python3 requires a statement to run; see :help python3. You might also be interested in :py3file or something like :execute 'python3' getline('.'). (Unfortunately the [range] specifier for these commands is undocumented.)


On the other hand, you may want a different workflow (say, using a tmux pane and :Twrite from tbone) to send code to an actual python instance.

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65