如何让Vim识别Python3语法?

时间:2017-09-17 15:36:23

标签: python python-3.x vim

我已经搜索了相关的帖子并尝试了几种方法,但在使用Python3语法时,我仍然在Vim中收到语法错误。

收到错误的代码:

types_of_people = 10; f"There are {types_of_people} types of people."

运行$ vim --version的输出:

$ vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Sep 17 2017 10:39:50)
MacOS X (unix) version
Included patches: 1-1100
Compiled by Homebrew
Huge version without GUI.  Features included (+) or not (-):
+acl             +file_in_path    +mouse_sgr       +tag_old_static
+arabic          +find_in_path    -mouse_sysmouse  -tag_any_white
+autocmd         +float           +mouse_urxvt     -tcl
-balloon_eval    +folding         +mouse_xterm     +termguicolors
-browse          -footer          +multi_byte      +terminal
++builtin_terms  +fork()          +multi_lang      +terminfo
+byte_offset     -gettext         -mzscheme        +termresponse
+channel         -hangul_input    +netbeans_intg   +textobjects
+cindent         +iconv           +num64           +timers
-clientserver    +insert_expand   +packages        +title
+clipboard       +job             +path_extra      -toolbar
+cmdline_compl   +jumplist        +perl            +user_commands
+cmdline_hist    +keymap          +persistent_undo +vertsplit
+cmdline_info    +lambda          +postscript      +virtualedit
+comments        +langmap         +printer         +visual
+conceal         +libcall         +profile         +visualextra
+cryptv          +linebreak       -python          +viminfo
+cscope          +lispindent      +python3         +vreplace
+cursorbind      +listcmds        +quickfix        +wildignore
+cursorshape     +localmap        +reltime         +wildmenu
+dialog_con      -lua             +rightleft       +windows
+diff            +menu            +ruby            +writebackup
+digraphs        +mksession       +scrollbind      -X11
-dnd             +modify_fname    +signs           -xfontset
-ebcdic          +mouse           +smartindent     -xim
+emacs_tags      -mouseshape      +startuptime     -xpm
+eval            +mouse_dec       +statusline      -xsmp
+ex_extra        -mouse_gpm       -sun_workshop    -xterm_clipboard
+extra_search    -mouse_jsbterm   +syntax          -xterm_save
+farsi           +mouse_netterm   +tag_binary      
system vimrc file: "$VIM/vimrc"
 user vimrc file: "$HOME/.vimrc"
2nd user vimrc file: "~/.vim/vimrc"
  user exrc file: "$HOME/.exrc"
   defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/local/share/vim"
Compilation: clang -c -I. -Iproto -DHAVE_CONFIG_H   -DMACOS_X_UNIX  -g -O2 -
U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1       
Linking: clang   -L. -fstack-protector -L/usr/local/lib -
L/usr/local/opt/libyaml/lib -L/usr/local/opt/openssl/lib -
L/usr/local/opt/readline/lib  -L/usr/local/lib -o vim        -lncurses -liconv 
-framework Cocoa   -mmacosx-version-min=10.11 -fstack-protector-strong -
L/usr/local/lib  -L/usr/local/Cellar/perl/5.26.0/lib/perl5/5.26.0/darwin-
thread-multi-2level/CORE -lperl -lm -lutil -lc  -
L/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-
darwin -lpython3.6m -framework CoreFoundation  -lruby.2.4.2 -lobjc 

您可能会注意到,Vim包含python3但不包括python

我使用以下方法重新安装了Vim:

$ brew uninstall vim
$ brew install vim --with-python3

我也尝试将python别名化为我的Python3版本:

$ alias python='python3.6'

我的路径:

$ echo $PATH  
/Library/Frameworks/Python.framework/Versions/3.6/bin:/Users/garythegoat/.rbenv/shims:/usr/bin/python:/usr/local/heroku/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/local/go/bin:/Users/garythegoat/golang/bin

:echo has('python')返回1

:echo has('python3')返回0

任何建议赞赏

1 个答案:

答案 0 :(得分:0)

这是一个难题,因为这与操作系统配置的默认python有关。

我发现最有用的相关问题是this one

两种解决方案都对我有用:

  1. 使用virtualenv设置虚拟环境。这太累了,因为每次需要打开vim时都必须启动虚拟环境。

  2. let g:syntastic_python_python_exec = 'python3'添加到.vimrc配置文件中。我不确定您的配置是否基于syntastic插件,但这是最糟糕的事情。