繁体   English   中英

如何将jscs autofix功能集成到vim中?

[英]How can I integrate jscs autofix feature into vim?

我正在尝试获取一个命令,我可以在vim中运行,以便在我的代码中使jscs自动纠正格式化问题。 到目前为止,我想出了:

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

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

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

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 ,您可以JscsFix它运行:JscsFix 要将其绑定到键(在本例中为<leader>g ),请使用noremap <leader>g :JscsFix<cr>

vim-autoformat支持JSCS开箱即用。 调用其:Autoformat命令仅修复当前文件。 请注意,它会编辑当前缓冲区中的文件,因此只会出现更改; 系统不会提示您重新加载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM