Advertisement
FocusedWolf

VIM: Running Python code in Vim

Nov 10th, 2019
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 2.62 KB | None | 0 0
  1. Running Python code in Vim
  2.  
  3. This will execute python code in the current window and display the results in a new window. If that window is left open then this code will update its content the next time you execute the Python code with this function:
  4.  
  5. " Bind F5 to save file if modified and execute python script in a buffer.
  6. nnoremap <silent> <F5> :call SaveAndExecutePython()<CR>
  7. vnoremap <silent> <F5> :<C-u>call SaveAndExecutePython()<CR>
  8.  
  9. function! SaveAndExecutePython()
  10.     " SOURCE [reusable window]: https://github.com/fatih/vim-go/blob/master/autoload/go/ui.vim
  11.  
  12.     " save and reload current file
  13.     silent execute "update | edit"
  14.  
  15.     " get file path of current file
  16.     let s:current_buffer_file_path = expand("%")
  17.  
  18.     let s:output_buffer_name = "Python"
  19.     let s:output_buffer_filetype = "output"
  20.  
  21.     " reuse existing buffer window if it exists otherwise create a new one
  22.     if !exists("s:buf_nr") || !bufexists(s:buf_nr)
  23.         silent execute 'botright new ' . s:output_buffer_name
  24.         let s:buf_nr = bufnr('%')
  25.     elseif bufwinnr(s:buf_nr) == -1
  26.         silent execute 'botright new'
  27.         silent execute s:buf_nr . 'buffer'
  28.     elseif bufwinnr(s:buf_nr) != bufwinnr('%')
  29.         silent execute bufwinnr(s:buf_nr) . 'wincmd w'
  30.     endif
  31.  
  32.     silent execute "setlocal filetype=" . s:output_buffer_filetype
  33.     setlocal bufhidden=delete
  34.     setlocal buftype=nofile
  35.     setlocal noswapfile
  36.     setlocal nobuflisted
  37.     setlocal winfixheight
  38.     setlocal cursorline " make it easy to distinguish
  39.     setlocal nonumber
  40.     setlocal norelativenumber
  41.     setlocal showbreak=""
  42.  
  43.     " clear the buffer
  44.     setlocal noreadonly
  45.     setlocal modifiable
  46.     %delete _
  47.  
  48.     " add the console output
  49.     silent execute ".!python " . shellescape(s:current_buffer_file_path, 1)
  50.  
  51.     " resize window to content length
  52.     " Note: This is annoying because if you print a lot of lines then your code buffer is forced to a height of one line every time you run this function.
  53.     "       However without this line the buffer starts off as a default size and if you resize the buffer then it keeps that custom size after repeated runs of this function.
  54.     "       But if you close the output buffer then it returns to using the default size when its recreated
  55.     "execute 'resize' . line('$')
  56.  
  57.     " make the buffer non modifiable
  58.     setlocal readonly
  59.     setlocal nomodifiable
  60. endfunction
  61.  
  62. This took a lot of effort to make so if you're feeling generous then feel free to send a [paypal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=3HVWGXQ9U9F5W&currency_code=USD&source=url)
  63. donation :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement