• Home
Name Date Size #Lines LOC

..--

vim/03-May-2024-7669

README.rstD03-May-20242.9 KiB11681

pre-commit.shD03-May-20242.5 KiB8736

README.rst

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