Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vim snippet to the README.rst #6

Merged
merged 4 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ Example::
print("hi")

Integrating this piping of input and output with your editor is left
as an exercise to the reader. For instance, Vim users can use visual
line mode and type ``:!black-macchiato``, and Emacs users can use
`python-black.el`__.
as an exercise to the reader. For instance, Vim users can add `this
snippet <vim_snippet.vim>`_ to their vimrc file, and Emacs users can
use `python-black.el`__.

__ https://github.com/wbolster/emacs-python-black

Expand Down
21 changes: 21 additions & 0 deletions vim_snippet.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function! s:RunBlackMacchiato() range
let cmd = "black-macchiato"
if !executable(cmd)
echohl ErrorMsg
echom "black-macchiato not found!"
echohl None
return
endif

silent execute a:firstline . "," . a:lastline . "!" . cmd

echo "Done formatting."

endfunction

" Create a command to call the black-macchiato function
command -range BlackMacchiato <line1>,<line2>call <sid>RunBlackMacchiato()

" Optionally add keyboard shortcuts to call the command in normal and visual modes
autocmd FileType python xnoremap <buffer> <Leader>f :'<,'>BlackMacchiato<cr>
autocmd FileType python nnoremap <buffer> <Leader>f :BlackMacchiato<cr>