Giter VIP home page Giter VIP logo

vim-yapf's Introduction

vim-yapf

Join the chat at https://gitter.im/mindriot101/vim-yapf

ATTENTION: you probably don't need this plugin! See Why you may not need this plugin.

vim-yapf is a Vim plugin that applies yapf to your current file. yapf automatically formats Python code, based on improved syntax styles.

Required

yapf can be installed either with pip:

pip install yapf

or by installing with conda (OSX and linux64 only) with my binstar channel:

conda install -c https://conda.binstar.org/srwalker101 yapf

TL;DR

To format only your changes, use vim-yapf with vim-gitgutter. For example, add following to your vimrc:

call plug#begin('~/.vim/plugged')
" yapf formater
Plug 'dy93/vim-yapf', { 'for': ['python'] }

" gitgutter
Plug 'airblade/vim-gitgutter'
call plug#end()

function! MyAutoFormat()
  let g:gitgutter_async = 0
  execute "GitGutterAll"
  let hunks = GitGutterGetHunks()
  if len(hunks) > 0
    let lines = []
    for hunk in GitGutterGetHunks()
      if hunk[2] <= hunk[2]+hunk[3]-1
        call add(lines, '-l '.hunk[2].'-'.(hunk[2]+hunk[3]-1))
      else
        call add(lines, '-l '.(hunk[2]+hunk[3]-1).'-'.hunk[2])
      endif
    endfor
    echom "Yapf --style ~/.style.yapf ".join(lines, ' ')
    let save = winsaveview()
    execute "Yapf --style ~/.style.yapf ".join(lines, ' ')
    call winrestview(save)
  else
    execute "Yapf --style ~/.style.yapf"
  endif
  let g:gitgutter_async = 1
endfunction

" formater
autocmd BufWritePre *.py :call MyAutoFormat()

and generate your yapf config:

$ yapf --style-help > ~/.style.yapf

Installation

Either use a plugin manager and add Plug[in] 'dy93/vim-yapf' to your vimrc, or use pathogen.

Bindings

The plugin does not create any bindings by default, this is left up to the user. An example could be:

:nnoremap <leader>y :call Yapf()<cr>

or

:nnoremap <leader>y :Yapf<cr>

Usage

call function

:Yapf

with arguments

:Yapf --style google

or

:call Yapf(" --style pep8")

Customization

The yapf style can be globally set, in your vimrc:

let g:yapf_style = "google"

or

let g:yapf_style = "pep8"

Why you may not need this plugin

The plugin itself is very simple. It handles user options granted, but at its core it uses ex commands to perform its magic. yapf behaves like a good unix command: it takes text on stdin and spits the altered result to stdout, which is exactly what vim expects.

At its core, this plugin runs the ex command:

0,$!yapf

This pipes the range 0,$ i.e. the whole file through a shell command yapf and replaces the range with the altered result.

Instead of installing this plugin, one could add a mapping e.g.:

autocmd FileType python nnoremap <leader>y :0,$!yapf<Cr><C-o>

Alternatively yapf could be set as the formatprg for the python filetype, and reformatting can be performed with the gq{motion} operator (e.g. with visual selection) to reformat a part of the file. Alternatively alternatively yapf could be set as the equalprg:

setlocal equalprg=yapf

and reformat the whole file with gg=G or a single line with =.

vim-yapf's People

Contributors

tell-k avatar simonrw avatar jseabold avatar dy93 avatar liuyang1 avatar adamtheturtle avatar maximbaz avatar gitter-badger avatar tocer avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.