{"id":963607,"date":"2024-12-27T04:19:52","date_gmt":"2024-12-26T20:19:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/963607.html"},"modified":"2024-12-27T04:19:54","modified_gmt":"2024-12-26T20:19:54","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8vim%e5%86%99python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/963607.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528vim\u5199python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180827\/667eba0c-34d9-4484-bb74-55a2bb4a726b.webp\" alt=\"\u5982\u4f55\u4f7f\u7528vim\u5199python\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Vim\u5199Python\u4ee3\u7801\u7684\u5173\u952e\u5728\u4e8e\uff1a\u638c\u63e1Vim\u57fa\u672c\u64cd\u4f5c\u3001\u914d\u7f6eVim\u4e3aPython\u5f00\u53d1\u73af\u5883\u3001\u5229\u7528\u63d2\u4ef6\u589e\u5f3a\u529f\u80fd\u3001\u719f\u7ec3\u4f7f\u7528Python\u76f8\u5173\u5feb\u6377\u952e\u548c\u547d\u4ee4\u3002<\/strong>\u5176\u4e2d\uff0c\u914d\u7f6eVim\u4e3aPython\u5f00\u53d1\u73af\u5883\u662f\u6700\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u5b83\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u901a\u8fc7\u5b89\u88c5\u5408\u9002\u7684\u63d2\u4ef6\uff0cVim\u53ef\u4ee5\u5b9e\u73b0\u8bed\u6cd5\u9ad8\u4eae\u3001\u81ea\u52a8\u8865\u5168\u3001\u4ee3\u7801\u68c0\u67e5\u7b49\u529f\u80fd\uff0c\u4f7f\u5176\u6210\u4e3a\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5f00\u53d1\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001VIM\u57fa\u672c\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Vim\u7f16\u5199Python\u4ee3\u7801\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u719f\u6089Vim\u7684\u57fa\u672c\u64cd\u4f5c\u3002Vim\u662f\u4e00\u6b3e\u5177\u6709\u5f3a\u5927\u529f\u80fd\u7684\u6587\u672c\u7f16\u8f91\u5668\uff0c\u4f46\u5b83\u4e0e\u5176\u4ed6\u7f16\u8f91\u5668\u7684\u64cd\u4f5c\u65b9\u5f0f\u7565\u6709\u4e0d\u540c\u3002Vim\u4e3b\u8981\u6709\u4e09\u79cd\u6a21\u5f0f\uff1a\u6b63\u5e38\u6a21\u5f0f\u3001\u63d2\u5165\u6a21\u5f0f\u548c\u547d\u4ee4\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6b63\u5e38\u6a21\u5f0f<\/strong>\uff1a\u8fd9\u662fVim\u542f\u52a8\u540e\u7684\u9ed8\u8ba4\u6a21\u5f0f\u3002\u5728\u8fd9\u4e2a\u6a21\u5f0f\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u5feb\u6377\u952e\u6765\u6d4f\u89c8\u548c\u7f16\u8f91\u6587\u6863\uff0c\u6bd4\u5982\u4f7f\u7528<code>hjkl<\/code>\u79fb\u52a8\u5149\u6807\u3001<code>dd<\/code>\u5220\u9664\u884c\u3001<code>yy<\/code>\u590d\u5236\u884c\u3001<code>p<\/code>\u7c98\u8d34\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d2\u5165\u6a21\u5f0f<\/strong>\uff1a\u6309\u4e0b<code>i<\/code>\u8fdb\u5165\u63d2\u5165\u6a21\u5f0f\uff0c\u6b64\u65f6\u53ef\u4ee5\u50cf\u5728\u666e\u901a\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u4e00\u6837\u8f93\u5165\u6587\u672c\u3002\u6309\u4e0b<code>Esc<\/code>\u952e\u53ef\u4ee5\u9000\u51fa\u63d2\u5165\u6a21\u5f0f\uff0c\u8fd4\u56de\u5230\u6b63\u5e38\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u547d\u4ee4\u6a21\u5f0f<\/strong>\uff1a\u5728\u6b63\u5e38\u6a21\u5f0f\u4e0b\uff0c\u6309\u4e0b<code>:<\/code>\u8fdb\u5165\u547d\u4ee4\u6a21\u5f0f\uff0c\u53ef\u4ee5\u6267\u884c\u5404\u79cd\u547d\u4ee4\uff0c\u5982\u4fdd\u5b58\u6587\u4ef6\uff08<code>:w<\/code>\uff09\u3001\u9000\u51faVim\uff08<code>:q<\/code>\uff09\u3001\u67e5\u627e\u66ff\u6362\uff08<code>:%s\/foo\/bar\/g<\/code>\uff09\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u914d\u7f6eVIM\u4e3aPYTHON\u5f00\u53d1\u73af\u5883<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7f16\u5199Python\u4ee3\u7801\uff0cVim\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u914d\u7f6e\uff0c\u4f7f\u5176\u9002\u5408Python\u5f00\u53d1\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Vimrc\u6587\u4ef6<\/strong>\uff1aVim\u7684\u914d\u7f6e\u4e3b\u8981\u901a\u8fc7<code>.vimrc<\/code>\u6587\u4ef6\u5b9e\u73b0\u3002\u4f60\u53ef\u4ee5\u5728\u4e3b\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>.vimrc<\/code>\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u914d\u7f6e\u6765\u4f18\u5316Python\u5f00\u53d1\u4f53\u9a8c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u542f\u7528\u8bed\u6cd5\u9ad8\u4eae\u3001\u8bbe\u7f6e\u7f29\u8fdb\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vim\">syntax on<\/p>\n<p>set nu<\/p>\n<p>set tabstop=4<\/p>\n<p>set shiftwidth=4<\/p>\n<p>set expandtab<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Python\u63d2\u4ef6<\/strong>\uff1a\u4e3a\u4e86\u589e\u5f3aVim\u7684Python\u5f00\u53d1\u529f\u80fd\uff0c\u53ef\u4ee5\u5b89\u88c5\u4e00\u4e9b\u63d2\u4ef6\u3002\u4f8b\u5982\uff0c<code>Vim-Plug<\/code>\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u63d2\u4ef6\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u5b89\u88c5\u548c\u7ba1\u7406\u63d2\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u63a8\u8350\u7684Python\u63d2\u4ef6\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>YouCompleteMe<\/strong>\uff1a\u63d0\u4f9b\u4ee3\u7801\u8865\u5168\u529f\u80fd\u3002<\/li>\n<li><strong>Syntastic<\/strong>\uff1a\u63d0\u4f9b\u8bed\u6cd5\u68c0\u67e5\u529f\u80fd\u3002<\/li>\n<li><strong>jedi-vim<\/strong>\uff1a\u63d0\u4f9bPython\u4ee3\u7801\u8865\u5168\u548c\u8df3\u8f6c\u529f\u80fd\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5229\u7528\u63d2\u4ef6\u589e\u5f3a\u529f\u80fd<\/p>\n<\/p>\n<p><p>\u63d2\u4ef6\u662fVim\u5f3a\u5927\u529f\u80fd\u7684\u6765\u6e90\uff0c\u901a\u8fc7\u5b89\u88c5\u548c\u914d\u7f6e\u63d2\u4ef6\uff0c\u53ef\u4ee5\u6781\u5927\u63d0\u9ad8Vim\u7684Python\u5f00\u53d1\u80fd\u529b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4ee3\u7801\u8865\u5168<\/strong>\uff1a<code>YouCompleteMe<\/code>\u548c<code>jedi-vim<\/code>\u90fd\u662f\u975e\u5e38\u6d41\u884c\u7684\u4ee3\u7801\u8865\u5168\u63d2\u4ef6\u3002<code>YouCompleteMe<\/code>\u652f\u6301\u591a\u79cd\u8bed\u8a00\uff0c\u800c<code>jedi-vim<\/code>\u4e13\u6ce8\u4e8ePython\u8865\u5168\u3002\u5b89\u88c5\u8fd9\u4e9b\u63d2\u4ef6\u540e\uff0c\u53ef\u4ee5\u5728Vim\u4e2d\u5b9e\u73b0\u667a\u80fd\u8865\u5168\uff0c\u5feb\u901f\u7f16\u5199\u4ee3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bed\u6cd5\u68c0\u67e5<\/strong>\uff1a<code>Syntastic<\/code>\u63d2\u4ef6\u53ef\u4ee5\u5e2e\u52a9\u68c0\u6d4b\u4ee3\u7801\u4e2d\u7684\u8bed\u6cd5\u9519\u8bef\uff0c\u5e76\u5728\u7f16\u8f91\u65f6\u5b9e\u65f6\u63d0\u793a\u3002\u8fd9\u5bf9\u4e8e\u786e\u4fdd\u4ee3\u7801\u8d28\u91cf\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ee3\u7801\u683c\u5f0f\u5316<\/strong>\uff1a<code>autopep8<\/code>\u548c<code>black<\/code>\u662fPython\u7684\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u901a\u8fc7Vim\u63d2\u4ef6\u6765\u81ea\u52a8\u683c\u5f0f\u5316\u4ee3\u7801\uff0c\u4f7f\u5176\u7b26\u5408PEP 8\u89c4\u8303\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u719f\u7ec3\u4f7f\u7528PYTHON\u76f8\u5173\u5feb\u6377\u952e\u548c\u547d\u4ee4<\/p>\n<\/p>\n<p><p>\u5728Vim\u4e2d\uff0c\u6709\u8bb8\u591a\u5feb\u6377\u952e\u548c\u547d\u4ee4\u53ef\u4ee5\u63d0\u9ad8Python\u7f16\u5199\u6548\u7387\u3002\u638c\u63e1\u8fd9\u4e9b\u5feb\u6377\u952e\u548c\u547d\u4ee4\uff0c\u53ef\u4ee5\u8ba9\u4f60\u66f4\u5feb\u901f\u5730\u8fdb\u884c\u7f16\u7801\u548c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5e38\u7528\u5feb\u6377\u952e<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>gg=G<\/code>\uff1a\u683c\u5f0f\u5316\u6574\u4e2a\u6587\u4ef6\u3002<\/li>\n<li><code>%<\/code>\uff1a\u8df3\u8f6c\u5230\u914d\u5bf9\u7684\u62ec\u53f7\u3002<\/li>\n<li><code>:w<\/code>\uff1a\u4fdd\u5b58\u6587\u4ef6\u3002<\/li>\n<li><code>:q<\/code>\uff1a\u9000\u51faVim\u3002<\/li>\n<li><code>:wq<\/code>\uff1a\u4fdd\u5b58\u5e76\u9000\u51fa\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Python\u7279\u5b9a\u547d\u4ee4<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>:!python %<\/code>\uff1a\u5728Vim\u4e2d\u76f4\u63a5\u8fd0\u884c\u5f53\u524dPython\u6587\u4ef6\u3002<\/li>\n<li><code>:make<\/code>\uff1a\u7ed3\u5408<code>makefile<\/code>\u8fdb\u884c\u7f16\u8bd1\u548c\u8fd0\u884c\u3002<\/li>\n<li><code>:Pylint<\/code>\uff1a\u4f7f\u7528Pylint\u8fdb\u884c\u4ee3\u7801\u68c0\u67e5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Vim\u662f\u4e00\u6b3e\u529f\u80fd\u5f3a\u5927\u7684\u7f16\u8f91\u5668\uff0c\u901a\u8fc7\u5408\u7406\u7684\u914d\u7f6e\u548c\u63d2\u4ef6\u652f\u6301\uff0c\u53ef\u4ee5\u6210\u4e3a\u4e00\u4e2a\u9ad8\u6548\u7684Python\u5f00\u53d1\u73af\u5883\u3002\u719f\u6089Vim\u7684\u57fa\u672c\u64cd\u4f5c\u3001\u914d\u7f6e\u9002\u5408Python\u5f00\u53d1\u7684\u73af\u5883\u3001\u5229\u7528\u63d2\u4ef6\u589e\u5f3a\u529f\u80fd\u3001\u4ee5\u53ca\u638c\u63e1Python\u76f8\u5173\u7684\u5feb\u6377\u952e\u548c\u547d\u4ee4\uff0c\u662f\u4f7f\u7528Vim\u7f16\u5199Python\u4ee3\u7801\u7684\u5173\u952e\u3002\u901a\u8fc7\u4e0d\u65ad\u7ec3\u4e60\u548c\u63a2\u7d22\uff0c\u4f60\u53ef\u4ee5\u9010\u6e10\u63d0\u9ad8\u4f7f\u7528Vim\u8fdb\u884cPython\u5f00\u53d1\u7684\u6548\u7387\u548c\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Vim\u4e2d\u8bbe\u7f6ePython\u5f00\u53d1\u73af\u5883\uff1f<\/strong><br \/>\u8981\u5728Vim\u4e2d\u5199Python\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5Vim\u7684Python\u652f\u6301\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u7ec8\u7aef\u8fd0\u884c<code>vim --version<\/code>\u6765\u68c0\u67e5\u662f\u5426\u5305\u542b<code>+python<\/code>\u6216<code>+python3<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u63d2\u4ef6\u5982Vim-Plug\u6216Pathogen\u6765\u7ba1\u7406Python\u76f8\u5173\u7684\u63d2\u4ef6\uff0c\u5982<code>vim-python-pep8-indent<\/code>\u548c<code>jedi-vim<\/code>\uff0c\u4ee5\u4fbf\u83b7\u5f97\u4ee3\u7801\u81ea\u52a8\u8865\u5168\u548c\u683c\u5f0f\u5316\u529f\u80fd\u3002<\/p>\n<p><strong>\u5728Vim\u4e2d\u4f7f\u7528Python\u65f6\uff0c\u5982\u4f55\u8c03\u8bd5\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Vim\u4e2d\u8c03\u8bd5Python\u4ee3\u7801\u53ef\u4ee5\u4f7f\u7528<code>pdb<\/code>\u8c03\u8bd5\u5668\u3002\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u63d2\u5165<code>import pdb; pdb.set_trace()<\/code>\uff0c\u7136\u540e\u5728Vim\u4e2d\u8fd0\u884c\u4ee3\u7801\u5e76\u4f7f\u7528\u8c03\u8bd5\u547d\u4ee4\u8fdb\u884c\u9010\u6b65\u6267\u884c\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>vim-pdb<\/code>\u63d2\u4ef6\u53ef\u4ee5\u5728Vim\u5185\u96c6\u6210\u8c03\u8bd5\u529f\u80fd\uff0c\u4f7f\u8c03\u8bd5\u8fc7\u7a0b\u66f4\u52a0\u76f4\u89c2\u548c\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u5347\u5728Vim\u4e2d\u7f16\u5199Python\u4ee3\u7801\u7684\u6548\u7387\uff1f<\/strong><br \/>\u63d0\u9ad8\u5728Vim\u4e2d\u7f16\u5199Python\u4ee3\u7801\u7684\u6548\u7387\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u4e00\u4e9b\u5b9e\u7528\u7684\u63d2\u4ef6\u548c\u5feb\u6377\u952e\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>NERDTree<\/code>\u63d2\u4ef6\u53ef\u4ee5\u65b9\u4fbf\u5730\u6d4f\u89c8\u6587\u4ef6\u7ed3\u6784\uff0c\u4f7f\u7528<code>ale<\/code>\u63d2\u4ef6\u53ef\u4ee5\u5b9e\u73b0\u5b9e\u65f6\u8bed\u6cd5\u68c0\u67e5\u3002\u6b64\u5916\uff0c\u5b66\u4e60Vim\u7684\u5e38\u7528\u547d\u4ee4\u548c\u5feb\u6377\u952e\uff0c\u80fd\u591f\u663e\u8457\u63d0\u5347\u7f16\u7801\u7684\u6d41\u7545\u6027\u548c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Vim\u5199Python\u4ee3\u7801\u7684\u5173\u952e\u5728\u4e8e\uff1a\u638c\u63e1Vim\u57fa\u672c\u64cd\u4f5c\u3001\u914d\u7f6eVim\u4e3aPython\u5f00\u53d1\u73af\u5883\u3001\u5229\u7528\u63d2\u4ef6\u589e\u5f3a\u529f\u80fd [&hellip;]","protected":false},"author":3,"featured_media":963611,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963607"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=963607"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963607\/revisions"}],"predecessor-version":[{"id":963613,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963607\/revisions\/963613"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/963611"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=963607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=963607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=963607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}