3

Over on askubuntu.com I was told how to run python on Vim. I am testing the set up which works with the code 1st or 2nd code python code at using python to solve a non linear equation.

The only thing I have done different was added print(a) as the last line. I ran this yesterday from the shell and it worked perfectly. Could someone let me know what is going wrong?

Ok so I corrected the vimrc with the appropriate question marks,

chmod +x ~/path/to/file/hw6problem2.py

Then from vim I ran

:Shell ./#

but I received the same syntax error again. (Does the file have to be saved as .sh because I can't get any .py files to run?)

dustin@dustin:~$ vim /home/dustin/Documents/School/UVM/Engineering/OrbitalMechanics/hw6problem2.py

File "hw6problem2.py", line 14
a0 = max(s/2, (s - c)/2)
 ^
SyntaxError: invalid syntax

shell returned 1

Press ENTER or type command to continue

vimrc

syntax on
au BufWinLeave * mkview "records settings
au BufWinEnter * silent loadview "reloads settings


set nu "puts line numbers on
set ic "case insensitive
set foldmethod=syntax "for the latex-suite
set autoread "autoload when files in the buffer have been modified
set autochdir "autochange directory


"set wrap
set wrap
" set lines=50 columns=80

" resizes window
:map g1 :set lines=20<CR>:set columns=80<CR>
:map g2 :set lines=50<CR>:set columns=80<CR>
:map g3 :set lines=50<CR>:set columns=170<CR>
:map <F6> :! firefox % &<CR>
:map E Ea

"set autoindent

set tabstop=4
set shiftwidth=2
set expandtab
set smartindent
"
" Stuff for latex-suite 

" REQUIRED. This makes vim invoke Latex-Suite when you open a tex file.
" It also allows you to set different actions for different filetypes
" in ~/.vim/after/ftplugin/*.vim
filetype plugin on

set shellslash

" IMPORTANT: grep will sometimes skip displaying the file name if you
" search in a singe file. This will confuse Latex-Suite. Set your grep
" program to always generate a file-name.
set grepprg=grep\ -nH\ $*

" OPTIONAL: This enables automatic indentation as you type.
filetype indent on

" OPTIONAL: Starting with Vim 7, the filetype of empty .tex files defaults to
" 'plaintex' instead of 'tex', which results in vim-latex not being loaded.
" The following changes the default filetype back to 'tex':
let g:tex_flavor='latex'
let g:Tex_ViewRule_pdf = 'okular'


"""""""""""""""""""""""""""""""""""""""
" => Shell command
"""""""""""""""""""""""""""""""""""""""

command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>)

function! s:RunShellCommand(cmdline)
let isfirst = 1
let words = []
for word in split(a:cmdline)
if isfirst
  let isfirst = 0  " don't change first word (shell command)
 else
  if word[0] =~ '\v[%#<]'
    let word = expand(word)
  endif
  let word = shellescape(word, 1)
endif
call add(words, word)
endfor
let expanded_cmdline = join(words)
rightbelow new
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
call setline(1, 'You entered:  ' . a:cmdline)
call setline(2, 'Expanded to:  ' . expanded_cmdline)
call append(line('$'), substitute(getline(2), '.', '=', 'g'))
silent execute '$read !'. expanded_cmdline
1
endfunction
6
  • Could you post some code around line 14? Commented Mar 28, 2013 at 0:54
  • 1
    This likely has nothing to do with Vim. There's nothing wrong with that one line of Python, either. Please either post the entire Python file (or link to a gist of it if it's large), or more context. I suspect you're just introducing a typo or something. Commented Mar 28, 2013 at 0:54
  • 1
    @PavelAnossov The code is in the second link Commented Mar 28, 2013 at 0:59
  • 1
    @JimStewart The code is in the second link Commented Mar 28, 2013 at 0:59
  • The code in the link has that as line 13, not 14. Commented Mar 28, 2013 at 1:01

1 Answer 1

2

This is likely to be a python issue.

On a side not, there's a great shell function for executing scripts and redirecting output to vim buffer (split window).

Syntax to execute current script (you should have chmod +x and shebang line):

:Shell ./#

In order to add the function, add this to .vimrc:

command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>)

function! s:RunShellCommand(cmdline)
  let isfirst = 1
  let words = []
  for word in split(a:cmdline)
    if isfirst
      let isfirst = 0  " don't change first word (shell command)
    else
      if word[0] =~ '\v[%#<]'
        let word = expand(word)
      endif
      let word = shellescape(word, 1)
    endif
    call add(words, word)
  endfor
  let expanded_cmdline = join(words)
  rightbelow new
  setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
  call setline(1, 'You entered:  ' . a:cmdline)
  call setline(2, 'Expanded to:  ' . expanded_cmdline)
  call append(line('$'), substitute(getline(2), '.', '=', 'g'))
  silent execute '$read !'. expanded_cmdline
  1
endfunction

https://github.com/ruslanosipov/dotfiles/blob/master/.vimrc#L137

Sign up to request clarification or add additional context in comments.

9 Comments

I added the above to my vimrc and then ran the code with the shebang line of #!/usr/bin/ env python but received the following error: Error detected while processing BufWinEnter Auto commands for "*": E32: No file name Press ENTER or type command to continue
No space between bin/ and env: #!/usr/bin/env python. Try doing :Shell ./%
that was a typo. now it says Error detected while processing BufWinEnter Auto commands for "*": E32: No file name Error detected while processing function <SNR>7_RunShellCommand: line 20: E499: Empty file name for '%' or '#', only works with ":p:h": $read !./% Press ENTER or type command to continue
@dustin Are you sure you don't have other entries in .vimrc interfering with the workflow?
@dustin, lines 2 and 3 of your .vimrc, replace * with ?* and hit the :Shell ./# again. ubuntuforums.org/showthread.php?t=1639591
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.