VIm function that runs file through shfmt

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I've written a Vim function that formats the file using shfmt. I'd like to know if there is a way I can improve this script.



I am bothered by the fact that I can't think of a way to do this without editing the file in the subprocess and reloading the file. Is there a way to do the equivalent of %!shfmt -options?



if exists("g:loaded_shfmt") || &compatible || ! executable('shfmt')
finish
endif

let s:save_cpoptions = &cpoptions
set cpoptions&vim

let s:switches = join(['-w', '-s', '-i', '2', '-bn', '-ci', '-sr'])

function! s:shFormat()
let l:filename = @%
let l:out = system('shfmt ' . s:switches . ' ' . l:filename)
edit!
endfunction

nnoremap <silent> <Leader>fb :call <SID>shFormat()<CR>

let &cpoptions = s:save_cpoptions

let g:loaded_shfmt = 1






share|improve this question



























    up vote
    2
    down vote

    favorite












    I've written a Vim function that formats the file using shfmt. I'd like to know if there is a way I can improve this script.



    I am bothered by the fact that I can't think of a way to do this without editing the file in the subprocess and reloading the file. Is there a way to do the equivalent of %!shfmt -options?



    if exists("g:loaded_shfmt") || &compatible || ! executable('shfmt')
    finish
    endif

    let s:save_cpoptions = &cpoptions
    set cpoptions&vim

    let s:switches = join(['-w', '-s', '-i', '2', '-bn', '-ci', '-sr'])

    function! s:shFormat()
    let l:filename = @%
    let l:out = system('shfmt ' . s:switches . ' ' . l:filename)
    edit!
    endfunction

    nnoremap <silent> <Leader>fb :call <SID>shFormat()<CR>

    let &cpoptions = s:save_cpoptions

    let g:loaded_shfmt = 1






    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I've written a Vim function that formats the file using shfmt. I'd like to know if there is a way I can improve this script.



      I am bothered by the fact that I can't think of a way to do this without editing the file in the subprocess and reloading the file. Is there a way to do the equivalent of %!shfmt -options?



      if exists("g:loaded_shfmt") || &compatible || ! executable('shfmt')
      finish
      endif

      let s:save_cpoptions = &cpoptions
      set cpoptions&vim

      let s:switches = join(['-w', '-s', '-i', '2', '-bn', '-ci', '-sr'])

      function! s:shFormat()
      let l:filename = @%
      let l:out = system('shfmt ' . s:switches . ' ' . l:filename)
      edit!
      endfunction

      nnoremap <silent> <Leader>fb :call <SID>shFormat()<CR>

      let &cpoptions = s:save_cpoptions

      let g:loaded_shfmt = 1






      share|improve this question













      I've written a Vim function that formats the file using shfmt. I'd like to know if there is a way I can improve this script.



      I am bothered by the fact that I can't think of a way to do this without editing the file in the subprocess and reloading the file. Is there a way to do the equivalent of %!shfmt -options?



      if exists("g:loaded_shfmt") || &compatible || ! executable('shfmt')
      finish
      endif

      let s:save_cpoptions = &cpoptions
      set cpoptions&vim

      let s:switches = join(['-w', '-s', '-i', '2', '-bn', '-ci', '-sr'])

      function! s:shFormat()
      let l:filename = @%
      let l:out = system('shfmt ' . s:switches . ' ' . l:filename)
      edit!
      endfunction

      nnoremap <silent> <Leader>fb :call <SID>shFormat()<CR>

      let &cpoptions = s:save_cpoptions

      let g:loaded_shfmt = 1








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 30 at 5:31









      200_success

      123k14143398




      123k14143398









      asked Jul 30 at 0:36









      harleypig

      1185




      1185

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200562%2fvim-function-that-runs-file-through-shfmt%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200562%2fvim-function-that-runs-file-through-shfmt%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation