-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunpython.vim
More file actions
126 lines (98 loc) · 3.74 KB
/
runpython.vim
File metadata and controls
126 lines (98 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
" Python 3:
autocmd FileType python noremap <silent> <C-b> :call SaveAndExecutePython3()<CR>
autocmd FileType python vnoremap <silent> <C-b> :<C-u>call SaveAndExecutePython3()<CR>
function! SaveAndExecutePython3()
" Save the window where you are currently
let l:currentWindow=winnr()
" SOURCE [reusable window]: https://github.com/fatih/vim-go/blob/master/autoload/go/ui.vim
" save and reload current file
silent execute "update | edit"
" get file path of current file
let s:current_buffer_file_path = expand("%")
let s:output_buffer_name = "Python 3"
let s:output_buffer_filetype = "output"
" reuse existing buffer window if it exists otherwise create a new one
if !exists("s:buf_nr") || !bufexists(s:buf_nr)
silent execute 'botright new ' . s:output_buffer_name
let s:buf_nr = bufnr('%')
elseif bufwinnr(s:buf_nr) == -1
silent execute 'botright new'
silent execute s:buf_nr . 'buffer'
elseif bufwinnr(s:buf_nr) != bufwinnr('%')
silent execute bufwinnr(s:buf_nr) . 'wincmd w'
endif
silent execute "setlocal filetype=" . s:output_buffer_filetype
setlocal bufhidden=delete
setlocal buftype=nofile
setlocal noswapfile
setlocal nobuflisted
setlocal winfixheight
setlocal cursorline " make it easy to distinguish
setlocal nonumber
setlocal norelativenumber
setlocal showbreak=""
setlocal laststatus=0 " turns off lightline for buffer
setlocal cc=0 " disables ruler for buffer
resize 10
" clear the buffer
setlocal noreadonly
setlocal modifiable
%delete _
" add the console output
silent execute ".!python3 " . shellescape(s:current_buffer_file_path, 1)
" make the buffer non modifiable
setlocal readonly
setlocal nomodifiable
:$
" Go back to the original window
exe l:currentWindow . "wincmd w"
endfunction
" Python 2
autocmd FileType python noremap <silent> <C-d> :call SaveAndExecutePython2()<CR>
autocmd FileType python vnoremap <silent> <C-d> :<C-u>call SaveAndExecutePython2()<CR>
function! SaveAndExecutePython2()
" Save the window where you are currently
let l:currentWindow=winnr()
" SOURCE [reusable window]: https://github.com/fatih/vim-go/blob/master/autoload/go/ui.vim
" save and reload current file
silent execute "update | edit"
" get file path of current file
let s:current_buffer_file_path = expand("%")
let s:output_buffer_name = "Python 2"
let s:output_buffer_filetype = "output"
" reuse existing buffer window if it exists otherwise create a new one
if !exists("s:buf_nr") || !bufexists(s:buf_nr)
silent execute 'botright new ' . s:output_buffer_name
let s:buf_nr = bufnr('%')
elseif bufwinnr(s:buf_nr) == -1
silent execute 'botright new'
silent execute s:buf_nr . 'buffer'
elseif bufwinnr(s:buf_nr) != bufwinnr('%')
silent execute bufwinnr(s:buf_nr) . 'wincmd w'
endif
silent execute "setlocal filetype=" . s:output_buffer_filetype
setlocal bufhidden=delete
setlocal buftype=nofile
setlocal noswapfile
setlocal nobuflisted
setlocal winfixheight
setlocal cursorline " make it easy to distinguish
setlocal nonumber
setlocal norelativenumber
setlocal showbreak=""
setlocal laststatus=0 " turns off lightline for buffer
setlocal cc=0 " disables ruler for buffer
resize 10
" clear the buffer
setlocal noreadonly
setlocal modifiable
%delete _
" add the console output
silent execute ".!python " . shellescape(s:current_buffer_file_path, 1)
" make the buffer non modifiable
setlocal readonly
setlocal nomodifiable
:$
" Go back to the original window
exe l:currentWindow . "wincmd w"
endfunction