如何将jscs autofix功能集成到vim中? [英] How can I integrate jscs autofix feature into vim?

查看:117
本文介绍了如何将jscs autofix功能集成到vim中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个命令,我可以在vim中运行以获取 jscs在我的代码中自动更正格式问题。到目前为止,我已经提出:

I'm trying to get a command I can run within vim to get jscs auto correct formatting issues in my code. So far I've come up with :

:nmap< F5> :!jscs -x。< CR>

这是好的,但它在整个目录上运行它我需要确认vim,我想重新加载缓冲区。有没有办法让vim只修复当前文件并在没有重新加载的情况下显示更改?

which is ok, but it runs it on the entire directory and I need to confirm to vim that I want to reload the buffer. Is there a way to get vim to fix the current file only and didsplay the changes without reloading?

推荐答案

这将管道当前文件每当你保存文件时通过jscs的修复模式进行归档(在实践中你的里程可能会有所不同!):

This will pipe the current file through jscs's fix mode whenever you save the file (your mileage may vary using this in practice!):

function! JscsFix()
    "Save current cursor position"
    let l:winview = winsaveview()
    "Pipe the current buffer (%) through the jscs -x command"
    % ! jscs -x
    "Restore cursor position - this is needed as piping the file"
    "through jscs jumps the cursor to the top"
    call winrestview(l:winview)
endfunction
command! JscsFix :call JscsFix()

"Run the JscsFix command just before the buffer is written for *.js files"
autocmd BufWritePre *.js JscsFix

它还会创建一个命令 JscsFix ,您可以随时使用<$ c运行该命令$ C>:JscsFix 。
要将其绑定到密钥(在本例中为< leader> g ),请使用 noremap< leader> g:JscsFix< cr> ;

It also creates a command JscsFix which you can run whenever you want with :JscsFix. To bind it to a key (in this case <leader>g) use noremap <leader>g :JscsFix<cr>.

这篇关于如何将jscs autofix功能集成到vim中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆