0

I open vim and have the following code:

1 a = range(10)
2 for b in a:
3     print(b)

I then open an ipython within vim by typing:

:term ipython

I yank the code by doing :1,2y and then go over to the ipython terminal and do "" to paste the yanked code.

The problem I have is that the spaces are not respected as shown below. Notice how print(a) is a lot further to the right compared to before.

In [2]: for a in b:
   ...:         print(a)
   ...:         

I've seen this behaviour before when pasting things into a normal vim using -V however usually some version of set paste, set nopaste works. Though in this case it didn't.

Does anyone know how I can make this work?

thank you

I'm running ubuntu 18.04.2 LTS. Vim installation info in case helpful

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Apr 10 2018 21:31:58)
Included patches: 1-1453
Modified by pkg-vim-maintainers@lists.alioth.debian.org
Compiled by pkg-vim-maintainers@lists.alioth.debian.org
Huge version without GUI.  Features included (+) or not (-):
+acl               +comments          +file_in_path      +linebreak         +mouse_urxvt       +quickfix          +terminal          +windows
+arabic            +conceal           +find_in_path      +lispindent        +mouse_xterm       +reltime           +terminfo          +writebackup
+autocmd           +cryptv            +float             +listcmds          +multi_byte        +rightleft         +termresponse      -X11
-autoservername    +cscope            +folding           +localmap          +multi_lang        -ruby              +textobjects       -xfontset
-balloon_eval      +cursorbind        -footer            -lua               -mzscheme          +scrollbind        +timers            -xim
+balloon_eval_term +cursorshape       +fork()            +menu              +netbeans_intg     +signs             +title             -xpm
-browse            +dialog_con        +gettext           +mksession         +num64             +smartindent       -toolbar           -xsmp
++builtin_terms    +diff              -hangul_input      +modify_fname      +packages          +startuptime       +user_commands     -xterm_clipboard
+byte_offset       +digraphs          +iconv             +mouse             +path_extra        +statusline        +vertsplit         -xterm_save
+channel           -dnd               +insert_expand     -mouseshape        -perl              -sun_workshop      +virtualedit
+cindent           -ebcdic            +job               +mouse_dec         +persistent_undo   +syntax            +visual
-clientserver      +emacs_tags        +jumplist          +mouse_gpm         +postscript        +tag_binary        +visualextra
-clipboard         +eval              +keymap            -mouse_jsbterm     +printer           +tag_old_static    +viminfo
+cmdline_compl     +ex_extra          +lambda            +mouse_netterm     +profile           -tag_any_white     +vreplace
+cmdline_hist      +extra_search      +langmap           +mouse_sgr         -python            -tcl               +wildignore
+cmdline_info      +farsi             +libcall           -mouse_sysmouse    +python3           +termguicolors     +wildmenu
   system vimrc file: "$VIM/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   -Wdate-time  -g -O2 -fdebug-prefix-map=/build/vim-NQEcoP/vim-8.0.1453=. -fstack-protector-strong -Wformat -Werror=form
at-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc   -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim        -lm -ltinfo -lnsl  -lselinux  -lacl -lattr -lgpm -ldl     -L/usr/lib/pytho
n3.6/config-3.6m-x86_64-linux-gnu -lpython3.6m -lpthread -ldl -lutil -lm

migrated from superuser.com Jun 8 at 15:29

This question came from our site for computer enthusiasts and power users.

  • My guess is when ipython hit the colon newline it put in the indent. And then the next pasted like also had indent. Or its a tab and not actually spaces. – D. Ben Knoble Jun 8 at 16:17
  • hm.. this seems possible, do you think there is a fix for this? Otherwise seems like a hacky solution would be to remove 4 spaces? btw the copied over extra space is spaces not a tab so everything is `spaces – evan54 Jun 8 at 23:56
0

I found a few workarounds.

I think what @d-ben-knoble mentioned is the reason for the strange indentation.

  • %autoindent
    • Benefit almost does what I want, ie allows pasting code from vim
    • Drawback some code still executes before having pasted the entire block if there is a space being pasted as well
    • Drawback have to type %autoindent before / after
  • %cpaste
    • Benefit allows pasting exact code and executing
    • Drawback need to type -- after I've pasted
    • Drawback can't edit once pasted
  • %edit
    • Benefit allows full pasting and editing of code
    • Drawback due to vimception in order to avoid vim's autoindent once entered in edit mode you need to do :set paste. Also, there exists a pitfall that you edit the main vim process rather than the one started by IPython which itself lives in a vim subprocess.

Ultimately I think %cpaste or %edit are probably good enough workarounds for me.

I'll leave this answer sitting around in case anyone comes up with a better solution.

Your Answer

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

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