from __future__ import print_function
import sys, os, time
for x in range(0,10):print(x, sep=' ', end='')# No need for sep here, but okay :)
time.sleep(1)
First of all, from __future__ import print_function needs to be the first line of code in your script (aside from some exceptions mentioned below). Second of all, as other answers have said, you have to use print as a function now. That’s the whole point of from __future__ import print_function; to bring the printfunction from Python 3 into Python 2.6+.
from __future__ import print_function
import sys, os, time
for x in range(0,10):
print(x, sep=' ', end='') # No need for sep here, but okay :)
time.sleep(1)
__future__ statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning. From the documentation:
A future statement is recognized and treated specially at compile
time: Changes to the semantics of core constructs are often
implemented by generating different code. It may even be the case that
a new feature introduces new incompatible syntax (such as a new
reserved word), in which case the compiler may need to parse the
module differently. Such decisions cannot be pushed off until runtime.
The documentation also mentions that the only things that can precede a __future__ statement are the module docstring, comments, blank lines, and other future statements.
Personally I can’t convince myself to litter my code with the markers. I’ve become pretty used to (and efficient) at using indent-folding. Together with my mapping of space bar (see below) to open/close folds and the zR and zM commands, I’m right at home. Perfect for Python!
set foldmethod=indent
nnoremap <space> za
vnoremap <space> zf
Python is well suited for folding on indent, bit for writing my own code I use markers as they can crunch a document down the way you want it and can serve as a kind of a table of contents. I have this in my vimrc to flip between the two when I’m viewing someone elses code.
#Toggle fold methods \fo
let g:FoldMethod = 0
map <leader>fo :call ToggleFold()<cr>
fun! ToggleFold()
if g:FoldMethod == 0
exe 'set foldmethod=indent'
let g:FoldMethod = 1
else
exe 'set foldmethod=marker'
let g:FoldMethod = 0
endif
endfun
#Add markers (trigger on class Foo line)
nnoremap ,f2 ^wywO#<c-r>0 {{{2<esc>
nnoremap ,f3 ^wywO#<c-r>0 {{{3<esc>
nnoremap ,f4 ^wywO#<c-r>0 {{{4<esc>
nnoremap ,f1 ^wywO#<c-r>0 {{{1<esc>
syn match pythonDefStatement /^\s*\%(def\|class\)/
\ nextgroup=pythonFunction skipwhite
syn region pythonFunctionFold start="^\z(\s*\)\%(def\|class\)\>"
\ end="\ze\%(\s*\n\)\+\%(\z1\s\)\@!." fold transparent
hi link pythonDefStatement Statement
For me the ideal folding is to fold just the class and def blocks, indent folding is too much for my taste. I think one elegant solution is to use the syntax system like this one mentioned by Tomas. However, this one is meant to replace the original syntax file and it may end being older than the original (i.e. that script doesn’t mention Python 3 syntax).
My solution is to place in the ~/.vim/syntax folder a file named python.vim with just the important lines (taken from the above script):
syn match pythonDefStatement /^\s*\%(def\|class\)/
\ nextgroup=pythonFunction skipwhite
syn region pythonFunctionFold start="^\z(\s*\)\%(def\|class\)\>"
\ end="\ze\%(\s*\n\)\+\%(\z1\s\)\@!." fold transparent
hi link pythonDefStatement Statement
Then simply activate the folding with :set foldmethod=syntax.
I really like this little vim script i wrote for .vimrc. It maps alt+1 to fold the first python indent level (class definitions and functions), alt+2 to fold the second level (class methods), and alt+0 to unfold everything. It makes sure it only folds one level and doesn’t fold any of the nested sub levels. You can still use za to toggle folding for the current block. Note that in ^[0, the ^[ is alt for my terminal. Don’t have a lot of experience in vim script so feel free to make suggestions on the function :)
I am writing Python code using Vim, and every time I want to run my code, I type this inside Vim:
:w !python
This gets frustrating, so I was looking for a quicker method to run Python code inside Vim. Executing Python scripts from a terminal maybe?
I am using Linux.
then you could press <F9> to execute the current buffer with python
Explanation:
autocmd: command that Vim will execute automatically on {event} (here: if you open a python file)
[i]map: creates a keyboard shortcut to <F9> in insert/normal mode
<buffer>: If multiple buffers/files are open: just use the active one
<esc>: leaving insert mode
:w<CR>: saves your file
!: runs the following command in your shell (try :!ls)
%: is replaced by the filename of your active buffer. But since it can contain things like whitespace and other “bad” stuff it is better practise not to write :python %, but use:
shellescape: escape the special characters. The 1 means with a backslash
TL;DR: The first line will work in normal mode and once you press <F9> it first saves your file and then run the file with python.
The second does the same thing, but leaves insert mode first
" Bind F5 to save file if modified and execute python script in a buffer.
nnoremap <silent> <F5> :call SaveAndExecutePython()<CR>
vnoremap <silent> <F5> :<C-u>call SaveAndExecutePython()<CR>
function! SaveAndExecutePython()
" 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"
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=""" clear the buffer
setlocal noreadonly
setlocal modifiable
%delete _
" add the console output
silent execute ".!python ". shellescape(s:current_buffer_file_path,1)" resize window to content length
"Note:Thisis 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." 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.
"Butif you close the output buffer then it returns to using the default size when its recreated
"execute 'resize' . line('$')
" make the buffer non modifiable
setlocal readonly
setlocal nomodifiable
endfunction
I prefer Python output redirected to a new Vim window (and if that window is left open then update its content the next time you execute Python code with this function):
" Bind F5 to save file if modified and execute python script in a buffer.
nnoremap <silent> <F5> :call SaveAndExecutePython()<CR>
vnoremap <silent> <F5> :<C-u>call SaveAndExecutePython()<CR>
function! SaveAndExecutePython()
" 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"
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=""
" clear the buffer
setlocal noreadonly
setlocal modifiable
%delete _
" add the console output
silent execute ".!python " . shellescape(s:current_buffer_file_path, 1)
" resize window to content length
" 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.
" 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.
" But if you close the output buffer then it returns to using the default size when its recreated
"execute 'resize' . line('$')
" make the buffer non modifiable
setlocal readonly
setlocal nomodifiable
endfunction
Using vert in the second line runs the code in vertical split instead of horizontal.
The negative of it is that if you don’t close the split-window where the code ran you will have many splits after multiple runs (which doesn’t happen in original python IDLE where the same output window is reused).
Keep in mind that you’re able to repeat the last used command with @:, so that’s all you’d need to repeat are those two character.
Or you could save the string w !python into one of the registers (like "a for example) and then hit :<C-R>a<CR> to insert the contents of register a into the commandline and run it.
Or you can do what I do and map <leader>z to :!python %<CR> to run the current file.
For generic use (run python/haskell/ruby/C++… from vim based on the filetype), there’s a nice plugin called vim-quickrun. It supports many programming languages by default. It is easily configurable, too, so one can define preferred behaviours for any filetype if needed. The github repo does not have a fancy readme, but it is well documented with the doc file.
A simple method would be to type : while in normal mode, and then press the up arrow key on the keyboard and press Enter. This will repeat the last typed commands on VIM.
If you want to quickly jump back through your :w commands, a cool thing is to type :w and then press your up arrow. It will only cycle through commands that start with w.
Think about using shebang line, so you will be able to use it with still any language, not only Python.
Adding shebang:
Add this to first line of your script:
#!/usr/bin/env python3
or this, if you are using Python 2:
#!/usr/bin/env python2
Vim keymap:
Add this to your ~/.vimrc:
nmap <F7> :w<cr>:!clear;"%:p"<cr>
Make file executable:
Type in Vim:
:!chmod +x %
or in terminal:
chmod +x script_name.py
Explanation:
When F7 is pressed in normal mode, Vim will try to execute current file as bash script. Then bash interpreter will see shebang line and understand, that this file should be passed to Python (or any other programm if needed).
Also you will be able to run your script from terminal using it’s name:
Put your cursor in the code somewhere. Right click and choose one of the “Select” choices to highlight your code. Then press Ctrl : and you will see the new prompt ‘<, >’
Now type !python and see if that works.
I just spend days trying to figure out the same problem!!! I used the coding:
s='My name'
print (s)
After I pulled out all my hair, I finally got it right!
Instead of putting the command mapping in your .vimrc, put the mapping in your ~/.vim/ftplugin/python.vim file (Windows $HOME\vimfiles\ftplugin\python.vim). If you don’t have this file or directories, just make them. This way the key is only mapped when you open a .py file or any file with filetype=python, since you’ll only be running this command on Python scripts.
For the actual mapping, I like to be able to edit in Vim while the script runs. Going off of @cazyas’ answer, I have the following in my ftplugin\python.vim (Windows):
noremap <F5> <Esc>:w<CR>:!START /B python %<CR>
This will run the current Python script in the background. For Linux just change it to this:
noremap <F5> <Esc>:w<CR>:!python % &<CR>
回答 19
" run current python file to new buffer
function! RunPython()
let s:current_file = expand("%")
enew|silent execute ".!python " . shellescape(s:current_file, 1)
setlocal buftype=nofile bufhidden=wipe noswapfile nowrap
setlocal nobuflisted
endfunction
autocmd FileType python nnoremap <Leader>c :call RunPython()<CR>
I was wondering if there was any key mapping in Vim to allow me to indent certain lines of code (whether those lines have been selected in visual mode, or n lines above/below current cursor position).
So basically something that converts the following
def my_fun(x, y):
return x + y
to
#def my_fun(x, y):
# return x + y
I am okay with using either # or """ for commenting out the relevant lines. Ideally, I would also like the same keymapping to uncomment the lines if the given lines have been commented out.
Step 1: Go to the the first column of the first line you want to comment.
Step 2: Press: Ctrl+v and select the lines you want to comment:
Step 3:Shift–I#space (Enter Insert-at-left mode, type chars to insert. The selection will disappear, but all lines within it will be modified after Step 4.)
Comment the selected block out with: :norm i# (lower case i)
To uncomment, highlight your block again, and uncomment with: :norm ^x
The :norm command performs an action for every selected line. Commenting will insert a # at the start of every line, and uncommenting will delete that #.
I usually sweep out a visual block (<C-V>), then search and replace the first character with:
:'<,'>s/^/#
(Entering command mode with a visual block selected automatically places ‘<,’> on the command line) I can then uncomment the block by sweeping out the same visual block and:
Frankly I use a tcomment plugin for that link. It can handle almost every syntax. It defines nice movements, using it with some text block matchers specific for python makes it a powerful tool.
NERDcommenter is an excellent plugin for commenting which automatically detects a number of filetypes and their associated comment characters. Ridiculously easy to install using Pathogen.
Comment with <leader>cc. Uncomment with <leader>cu. And toggle comments with <leader>c<space>.
There’s a lot of comment plugins for vim – a number of which are multi-language – not just python. If you use a plugin manager like Vundle then you can search for them (once you’ve installed Vundle) using e.g.: