huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
===========
IDE Plugins
===========
 
Emacs
=====
 
The ``Emacs`` plugin is maintained separately. Installation directions can be
found here: https://github.com/paetzke/py-yapf.el
 
VIM
===
 
The ``vim`` plugin allows you to reformat a range of code. Copy ``plugin`` and
``autoload`` directories into your ~/.vim or use ``:packadd`` in Vim 8. Or use
a plugin manager like Plug or Vundle:
 
.. code-block:: vim
 
     " Plug
     Plug 'google/yapf', { 'rtp': 'plugins/vim', 'for': 'python' }
 
     " Vundle
     Plugin 'google/yapf', { 'rtp': 'plugins/vim' }
 
 
You can add key bindings in the ``.vimrc`` file:
 
.. code-block:: vim
 
    map <C-Y> :call yapf#YAPF()<cr>
    imap <C-Y> <c-o>:call yapf#YAPF()<cr>
 
Alternatively, you can call the command ``YAPF``. If you omit the range, it
will reformat the whole buffer.
 
example:
 
.. code-block:: vim
 
    :YAPF       " formats whole buffer
    :'<,'>YAPF  " formats lines selected in visual mode
 
Sublime Text
============
 
The ``Sublime Text`` plugin is also maintained separately. It is compatible
with both Sublime Text 2 and 3.
 
The plugin can be easily installed by using *Sublime Package Control*. Check
the project page of the plugin for more information:
https://github.com/jason-kane/PyYapf
 
===================
git Pre-Commit Hook
===================
 
The ``git`` pre-commit hook automatically formats your Python files before they
are committed to your local repository. Any changes ``yapf`` makes to the files
will stay unstaged so that you can diff them manually.
 
To install, simply download the raw file and copy it into your git hooks
directory:
 
.. code-block:: bash
 
    # From the root of your git project.
    curl -o pre-commit.sh https://raw.githubusercontent.com/google/yapf/master/plugins/pre-commit.sh
    chmod a+x pre-commit.sh
    mv pre-commit.sh .git/hooks/pre-commit
 
==========
Textmate 2
==========
 
Plugin for ``Textmate 2`` requires ``yapf`` Python package installed on your
system:
 
.. code-block:: shell
 
    pip install yapf
 
Also, you will need to activate ``Python`` bundle from ``Preferences >>
Bundles``.
 
Finally, create a ``~/Library/Application
Support/TextMate/Bundles/Python.tmbundle/Commands/YAPF.tmCommand`` file with
the following content:
 
.. code-block:: xml
 
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
      <key>beforeRunningCommand</key>
      <string>saveActiveFile</string>
      <key>command</key>
      <string>#!/bin/bash
 
    TPY=${TM_PYTHON:-python}
 
    "$TPY" "/usr/local/bin/yapf" "$TM_FILEPATH"</string>
      <key>input</key>
      <string>document</string>
      <key>name</key>
      <string>YAPF</string>
      <key>scope</key>
      <string>source.python</string>
      <key>uuid</key>
      <string>297D5A82-2616-4950-9905-BD2D1C94D2D4</string>
    </dict>
    </plist>
 
You will see a new menu item ``Bundles > Python > YAPF``.