--Execution by pressing <F5>
only for python files
--The execution result is displayed in a new buffer
--If executed multiple times, it will be added.
--The file with the cursor is executed when multiple python files are open
This content was written in vimrc
. vimrc
is
:edit $MYVIMRC
You can open it with. If you have not changed the settings
:vnew $HOME/_vimrc
You can also open it here. You can check the settings with : version
.
vimrc
help: h vimrc
Recommended: All Vim configuration files should be in the
$ HOME / .vim /
directory ($ HOME / vimfiles /
on MS-Windows). This will make it easier to copy the configuration file to another system.
You can check $ HOME
with: echo $ HOME
. If you want to create vimrc
under $ HOME / vimfiles
, the name is vimrc
, but under $ HOME
. If you put it, it will be named _vimrc
.
:!python %
The Ex command :! Hoge
will execute cmd hoge
, assigning it to <F5>
.
autocmd BufRead,BufNewFile *.py inoremap <F5> <Esc>:w<CR>:! python %<CR>
autocmd BufRead,BufNewFile *.py nnoremap <F5> :w<CR>:! python %<CR>
Now you can run the open file by pressing <F5>
.
The execution result disappears by pressing ʻENTER`.
I've set it to open a new buffer to solve the above problem, except if it's already open, it will multiply every time I press <F5>
, so I need to separate cases to solve this. Because it became long, I made a function called Pyexe ()
.
At this time, since the :! Hoge
used earlier is only executed, usesystem (hoge)
that can obtain the output as a character string.
"Define function
function Pyexe()
"Preparing to return after inputting the result to the output buffer
"Get the current position of the cursor
:let pos=getpos(".")
"Get the id of the current window
:let cwinid=win_getid()
"Get the filename of the buffer in the current window
:let fileName=expand('%')
"Buffer settings for output
"Decide the file name to display
:let outFileName="~pyOut"
"Preparing to check if the output window is already open
"Get the buffer number if a buffer for output exists.If it does not exist-1.
:let bnr=bufnr(outFileName)
"Get the window id of the buffer for output(List format).Empty list if it does not exist.
:let wids=win_findbuf(bnr)
"If there is a buffer for output, change the window id to outWindid.Make if it doesn't exist.
if bnr == -1 || len(wids)==0
"If it does not exist
"Allow the output buffer to be closed at the same time when the python file is closed
:autocmd QuitPre <buffer> exe(':bwipeout!') g:bnr
"Split the window to create a buffer for output
:exe 'vertical rightbelow new' outFileName
"Set the buffer type to nofile as the user does not write
:set buftype=nofile
"Set to exit by simply pressing q in normal mode.
"<buffer>The option should be applied only to the output buffer created this time..
:nmap <buffer> q <C-u>:bwipeout!<CR>
"Adjust window size
:vertical resize 70
"Get the buffer buffer number for output
:let bnr=bufnr(outFileName)
else
"If there
"Fetch the output buffer window id from the list
:let outWinid=wids[0]
"Move to the output buffer window
:call win_gotoid(outWinid)
endif
"output
"Move to the end of the output buffer
:call setpos(bnr,"$")
"Call cmd to run python
:let @r=system('python '.fileName)
"Put to the output buffer
:put r
"Re-depiction
:redraw!
"Return to start position
"Go to python file window
:call win_gotoid(cwinid)
"Move the cursor to the start position
:call setpos(".",pos)
endfunction
After that, assign this to <F5>
only for python.
"Only for python files<F5>Make it possible to execute by pressing a key
autocmd BufRead,BufNewFile *.py inoremap <F5> <Esc>:w<CR>:call Pyexe()<CR>
autocmd BufRead,BufNewFile *.py nnoremap <F5> :w<CR>:call Pyexe() <CR>