我通过apt-get安装了clang-format-3.8
。现在我尝试在gVim中使用它,但它不起作用。我检查并且clang-format-3.8存在于文件夹/usr/share/vim/addons/syntax
中。
但是当我在我的vim命令行中输入:pyf /usr/share/vim/addons/syntax/clang-format-3.8.py
时,它返回:
E319: Sorry, the command is not available in this version
。
我在Ubuntu 16.04下使用gVim 7.4。
Ubuntu 16.04附带的Vim二进制文件是用Python 3编译的.cngng格式的vim插件由Python 2编写。
你需要:
Google可以找到使用Python构建vim的说明。
Dahn的回答是正确的,Ubuntu 16.04附带的Vim二进制文件是用Python 3而不是Python 2编译的.Ubuntu 16.04 clang-format-3.8.py
包中的clang-format-3.8
脚本与Python 3不兼容。
但是最新的clang-format.py
可以与Python 3一起使用。你可以在这里得到它:
https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/clang-format.py
我认为这只是围绕print
声明括起来的问题。
将此文件保存在计算机上的某个位置,例如/usr/local/share/vim/addons/syntax/
。
此脚本使用clang-format
作为二进制名称,因此您需要安装clang-format
软件包,该软件包将clang-format
命令安装为clang-format-3.8
的符号链接。
由于Vim现在正在加载Python 3脚本,所以用:pyf
替换你的:py3f
(不可用)命令:
:py3f /usr/local/share/vim/addons/syntax/clang-format.py
我使用bash
和vim
命令的组合以不同的方式解决了这个问题。
首先,我安装了clang-format
包
# apt-get install clang-format-3.5
(我选择版本3.5,但你可以选择另一个)
第二,测试clang-format
是否正常工作
$ clang-format-3.5 -style=Google test.cpp
然后,运行vim
$ vim test.cpp
vim
允许运行外部命令并将其输出打印到当前缓冲区
:r ! clang-format-3.5 -style=Google %
(有关vim https://www.linux.com/learn/vim-tips-working-external-commands中外部命令的更多详细信息)
这会将clang-format
的输出附加到当前缓冲区。要替换当前缓冲区,这是理想的效果,请指定要输出的行
:%! clang-format-3.5 -style=Google %
(第一个%
表示当前文件中的所有行)
通过在vim(使用command
)中为视觉和命令行模式定义新命令,可以提高此过程的效率。
除了上面的答案,我还要做更多的事情。我下载了一个新的python文件,并将clang python文件中建议的键映射更改为我的.vimrc中的以下内容:
map <C-I> :py3file <path-to-this-file>/clang-format.py<cr>
imap <C-I> <c-o>:py3file <path-to-this-file>/clang-format.py<cr>
这解决了我遇到的E319问题。