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

Wil*_*unn 9 javascript vim jscs

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

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

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

act*_*imp 8

每当您保存文件时,这将通过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
Run Code Online (Sandbox Code Playgroud)

它还会创建一个命令JscsFix,您可以随时运行该命令:JscsFix.将它绑定到一个键(在这种情况下<leader>g)使用noremap <leader>g :JscsFix<cr>.


Blu*_*luu 5

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