9

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

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

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

4

2 回答 2

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

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

于 2015-04-23T09:26:36.257 回答
5

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

于 2015-12-08T22:44:07.667 回答