• Home
Name Date Size #Lines LOC

..--

vim/04-Jul-2025-7669

README.mdD04-Jul-20252.6 KiB10775

pre-commit.shD04-Jul-20252.7 KiB8433

README.md

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