Auto indent / format code for Vim?











up vote
27
down vote

favorite
17












I'm trying to use Vim more and more when I can. One of my biggest grip between Vim and an IDE like Aptana is the ability to auto indent.



Is there a means of auto formatting code (HTML, CSS, PHP) so it is properly indented?



If so how do you install this into vim? I don't understand plugins very much.
I tried reviewing this thread and it confused me more: How to change vim auto-indent behavior?










share|improve this question
























  • Could you clarify what language you are trying to indent? I would expect that if it's supported by vim already it should already auto-indent without further effort. If not, you should be able to get a plugin.
    – Edd Steel
    Sep 1 '11 at 22:37










  • ideally Html/css and php
    – chrisjlee
    Sep 1 '11 at 23:50















up vote
27
down vote

favorite
17












I'm trying to use Vim more and more when I can. One of my biggest grip between Vim and an IDE like Aptana is the ability to auto indent.



Is there a means of auto formatting code (HTML, CSS, PHP) so it is properly indented?



If so how do you install this into vim? I don't understand plugins very much.
I tried reviewing this thread and it confused me more: How to change vim auto-indent behavior?










share|improve this question
























  • Could you clarify what language you are trying to indent? I would expect that if it's supported by vim already it should already auto-indent without further effort. If not, you should be able to get a plugin.
    – Edd Steel
    Sep 1 '11 at 22:37










  • ideally Html/css and php
    – chrisjlee
    Sep 1 '11 at 23:50













up vote
27
down vote

favorite
17









up vote
27
down vote

favorite
17






17





I'm trying to use Vim more and more when I can. One of my biggest grip between Vim and an IDE like Aptana is the ability to auto indent.



Is there a means of auto formatting code (HTML, CSS, PHP) so it is properly indented?



If so how do you install this into vim? I don't understand plugins very much.
I tried reviewing this thread and it confused me more: How to change vim auto-indent behavior?










share|improve this question















I'm trying to use Vim more and more when I can. One of my biggest grip between Vim and an IDE like Aptana is the ability to auto indent.



Is there a means of auto formatting code (HTML, CSS, PHP) so it is properly indented?



If so how do you install this into vim? I don't understand plugins very much.
I tried reviewing this thread and it confused me more: How to change vim auto-indent behavior?







vim






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 13 '17 at 12:36









Community

1




1










asked Sep 1 '11 at 20:47









chrisjlee

2,437123350




2,437123350












  • Could you clarify what language you are trying to indent? I would expect that if it's supported by vim already it should already auto-indent without further effort. If not, you should be able to get a plugin.
    – Edd Steel
    Sep 1 '11 at 22:37










  • ideally Html/css and php
    – chrisjlee
    Sep 1 '11 at 23:50


















  • Could you clarify what language you are trying to indent? I would expect that if it's supported by vim already it should already auto-indent without further effort. If not, you should be able to get a plugin.
    – Edd Steel
    Sep 1 '11 at 22:37










  • ideally Html/css and php
    – chrisjlee
    Sep 1 '11 at 23:50
















Could you clarify what language you are trying to indent? I would expect that if it's supported by vim already it should already auto-indent without further effort. If not, you should be able to get a plugin.
– Edd Steel
Sep 1 '11 at 22:37




Could you clarify what language you are trying to indent? I would expect that if it's supported by vim already it should already auto-indent without further effort. If not, you should be able to get a plugin.
– Edd Steel
Sep 1 '11 at 22:37












ideally Html/css and php
– chrisjlee
Sep 1 '11 at 23:50




ideally Html/css and php
– chrisjlee
Sep 1 '11 at 23:50










7 Answers
7






active

oldest

votes

















up vote
43
down vote



accepted










To indent the whole file automatically:



gg
=G


Explained:





  • g - go to


  • gg - go to beginning of the file


  • G - go to end of the file


  • = - indent






share|improve this answer



















  • 1




    Could you break it down? What is g typically by itself? and =G?
    – chrisjlee
    Sep 2 '11 at 5:47










  • @Chris see the edit
    – takeshin
    Sep 2 '11 at 5:53






  • 1




    Nice hint, I never knew this. However, it does a rubbish job with bash.
    – Sparhawk
    Aug 8 '14 at 4:49










  • How to execute gg=G from shell without opening the file?
    – W.M.
    Aug 28 '16 at 16:18










  • @takeshin can you please explain it much further, do we need to type it in the file or do we need to run it in the terminal
    – Kasun Siyambalapitiya
    Dec 1 '16 at 11:22


















up vote
11
down vote













I don't know about auto-formatting existing code, but if you are writing code and need auto indent:





  • :set autoindent (or :set ai) will turn on auto-indent


  • Ctrl-d to un-indent (AKA outdent) your code

  • Tab or spaces to indent -- I personally use tab


  • :set tabwidth=4 (or :set tw=4) will control how many spaces a tab should indent code

  • The >> command will indent the current line. If you prefix it by a number, say 4>> then it will indent 4 lines, starting with the current line.

  • Likewise the << command will un-indent a line


I hope this gives you a good start.






share|improve this answer



















  • 7




    To auto-indent existing code, use ={motion} in normal mode -- e.g. =G will indent all code from the current line to the end of the file. == will indent the current line.
    – Edd Steel
    Sep 1 '11 at 22:36






  • 1




    also make sure indent plugins are being loaded (e.g. with :filetype indent on)
    – jw013
    Sep 2 '11 at 0:50






  • 2




    set tw=4 sets the TEXTWIDTH to 4
    – Steffen Roller
    Apr 25 '17 at 2:01


















up vote
4
down vote













Auto Indent *.sh



Just add the following lines in ~/.vimrc



filetype indent on
set smartindent
autocmd BufRead,BufWritePre *.sh normal gg=G





share|improve this answer























  • Does this apply only to editing with vim or all editor tools?
    – W.M.
    Aug 28 '16 at 16:18






  • 1




    ~./vimrc only applicable for vim editor.
    – Rahul Patil
    Aug 29 '16 at 3:54


















up vote
2
down vote













This plugin makes it easier to perform formatting on your code. It integrates external formatters, and has a fallback on vim's indent functionality.



https://github.com/Chiel92/vim-autoformat



Also, notice the difference between formatting and indenting. Indenting only corrects the whitespace before every line, while formatting also deals with any other thing, such as whitespace around operators etc.






share|improve this answer




























    up vote
    0
    down vote













    In bash I do this:



    source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
    declare -f Zibri| cut -c 5-|head --lines=-1|tail --lines=+3


    this eliminates comments and reindents the script "bash way".
    it will not work if the script contains HEREDOCS but if you do this:



    source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
    declare -f Zibri|head --lines=-1|tail --lines=+3


    it will work with any script but the whole script will be indented by 4 spaces. feel free to modify but cite my name in your script and post it! :D






    share|improve this answer





















    • I believe this may be missing the point of the question; rather than indenting a set of lines, I believe the OP wished to auto-indent within VIM for a variety of languages. As an aside, you may be able to achieve this a little more directly with sed if you so wish: cat script_to_be_reindented.sh | sed 's#^# #g' (er, well, SO markdown is replacing the spaces with a tab, but you get the point)
      – eacousineau
      Mar 2 '17 at 18:29




















    up vote
    0
    down vote













    Create/edit the ~/.vimrc file and add the following line:



    set autoindent





    share|improve this answer

















    • 2




      This adds nothing to the existing answers...
      – jasonwryan
      Jan 30 '17 at 6:11










    • @jasonwryan Actually Hai Vu's answer mentions :set autoindent which enables auto indent only for the current file. Adding it in the .vimrc file enables auto indent permanently.
      – Akshat Maheshwari
      Jan 30 '17 at 7:12










    • your piece of comment is already mentioned by Rahul's answer, so think before posting
      – Indrajeet Gour
      May 8 '17 at 9:25


















    up vote
    0
    down vote













    vim's autoformat/indent works pretty well. First, put this line in your ~/.vimrc:



    filetype plugin indent on


    Then open a file in vim and type gg=G



    (gg moves cursor to the first line. = runs the indent command. G tells indent command to run from here to the last line.)



    If the autoformat looks really bad, like every line is just left indented, then run :scriptnames and check if .../indent/html.vim (or whatever language you're using) is in the list. If not, then make sure your ~/.vimrc is correct. Or if you ran :filetype plugin indent on from the vim command line, you will need to re-open the file :e






    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      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: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f19945%2fauto-indent-format-code-for-vim%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      7 Answers
      7






      active

      oldest

      votes








      7 Answers
      7






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      43
      down vote



      accepted










      To indent the whole file automatically:



      gg
      =G


      Explained:





      • g - go to


      • gg - go to beginning of the file


      • G - go to end of the file


      • = - indent






      share|improve this answer



















      • 1




        Could you break it down? What is g typically by itself? and =G?
        – chrisjlee
        Sep 2 '11 at 5:47










      • @Chris see the edit
        – takeshin
        Sep 2 '11 at 5:53






      • 1




        Nice hint, I never knew this. However, it does a rubbish job with bash.
        – Sparhawk
        Aug 8 '14 at 4:49










      • How to execute gg=G from shell without opening the file?
        – W.M.
        Aug 28 '16 at 16:18










      • @takeshin can you please explain it much further, do we need to type it in the file or do we need to run it in the terminal
        – Kasun Siyambalapitiya
        Dec 1 '16 at 11:22















      up vote
      43
      down vote



      accepted










      To indent the whole file automatically:



      gg
      =G


      Explained:





      • g - go to


      • gg - go to beginning of the file


      • G - go to end of the file


      • = - indent






      share|improve this answer



















      • 1




        Could you break it down? What is g typically by itself? and =G?
        – chrisjlee
        Sep 2 '11 at 5:47










      • @Chris see the edit
        – takeshin
        Sep 2 '11 at 5:53






      • 1




        Nice hint, I never knew this. However, it does a rubbish job with bash.
        – Sparhawk
        Aug 8 '14 at 4:49










      • How to execute gg=G from shell without opening the file?
        – W.M.
        Aug 28 '16 at 16:18










      • @takeshin can you please explain it much further, do we need to type it in the file or do we need to run it in the terminal
        – Kasun Siyambalapitiya
        Dec 1 '16 at 11:22













      up vote
      43
      down vote



      accepted







      up vote
      43
      down vote



      accepted






      To indent the whole file automatically:



      gg
      =G


      Explained:





      • g - go to


      • gg - go to beginning of the file


      • G - go to end of the file


      • = - indent






      share|improve this answer














      To indent the whole file automatically:



      gg
      =G


      Explained:





      • g - go to


      • gg - go to beginning of the file


      • G - go to end of the file


      • = - indent







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 2 days ago









      Max Coplan

      1034




      1034










      answered Sep 2 '11 at 5:46









      takeshin

      716811




      716811








      • 1




        Could you break it down? What is g typically by itself? and =G?
        – chrisjlee
        Sep 2 '11 at 5:47










      • @Chris see the edit
        – takeshin
        Sep 2 '11 at 5:53






      • 1




        Nice hint, I never knew this. However, it does a rubbish job with bash.
        – Sparhawk
        Aug 8 '14 at 4:49










      • How to execute gg=G from shell without opening the file?
        – W.M.
        Aug 28 '16 at 16:18










      • @takeshin can you please explain it much further, do we need to type it in the file or do we need to run it in the terminal
        – Kasun Siyambalapitiya
        Dec 1 '16 at 11:22














      • 1




        Could you break it down? What is g typically by itself? and =G?
        – chrisjlee
        Sep 2 '11 at 5:47










      • @Chris see the edit
        – takeshin
        Sep 2 '11 at 5:53






      • 1




        Nice hint, I never knew this. However, it does a rubbish job with bash.
        – Sparhawk
        Aug 8 '14 at 4:49










      • How to execute gg=G from shell without opening the file?
        – W.M.
        Aug 28 '16 at 16:18










      • @takeshin can you please explain it much further, do we need to type it in the file or do we need to run it in the terminal
        – Kasun Siyambalapitiya
        Dec 1 '16 at 11:22








      1




      1




      Could you break it down? What is g typically by itself? and =G?
      – chrisjlee
      Sep 2 '11 at 5:47




      Could you break it down? What is g typically by itself? and =G?
      – chrisjlee
      Sep 2 '11 at 5:47












      @Chris see the edit
      – takeshin
      Sep 2 '11 at 5:53




      @Chris see the edit
      – takeshin
      Sep 2 '11 at 5:53




      1




      1




      Nice hint, I never knew this. However, it does a rubbish job with bash.
      – Sparhawk
      Aug 8 '14 at 4:49




      Nice hint, I never knew this. However, it does a rubbish job with bash.
      – Sparhawk
      Aug 8 '14 at 4:49












      How to execute gg=G from shell without opening the file?
      – W.M.
      Aug 28 '16 at 16:18




      How to execute gg=G from shell without opening the file?
      – W.M.
      Aug 28 '16 at 16:18












      @takeshin can you please explain it much further, do we need to type it in the file or do we need to run it in the terminal
      – Kasun Siyambalapitiya
      Dec 1 '16 at 11:22




      @takeshin can you please explain it much further, do we need to type it in the file or do we need to run it in the terminal
      – Kasun Siyambalapitiya
      Dec 1 '16 at 11:22












      up vote
      11
      down vote













      I don't know about auto-formatting existing code, but if you are writing code and need auto indent:





      • :set autoindent (or :set ai) will turn on auto-indent


      • Ctrl-d to un-indent (AKA outdent) your code

      • Tab or spaces to indent -- I personally use tab


      • :set tabwidth=4 (or :set tw=4) will control how many spaces a tab should indent code

      • The >> command will indent the current line. If you prefix it by a number, say 4>> then it will indent 4 lines, starting with the current line.

      • Likewise the << command will un-indent a line


      I hope this gives you a good start.






      share|improve this answer



















      • 7




        To auto-indent existing code, use ={motion} in normal mode -- e.g. =G will indent all code from the current line to the end of the file. == will indent the current line.
        – Edd Steel
        Sep 1 '11 at 22:36






      • 1




        also make sure indent plugins are being loaded (e.g. with :filetype indent on)
        – jw013
        Sep 2 '11 at 0:50






      • 2




        set tw=4 sets the TEXTWIDTH to 4
        – Steffen Roller
        Apr 25 '17 at 2:01















      up vote
      11
      down vote













      I don't know about auto-formatting existing code, but if you are writing code and need auto indent:





      • :set autoindent (or :set ai) will turn on auto-indent


      • Ctrl-d to un-indent (AKA outdent) your code

      • Tab or spaces to indent -- I personally use tab


      • :set tabwidth=4 (or :set tw=4) will control how many spaces a tab should indent code

      • The >> command will indent the current line. If you prefix it by a number, say 4>> then it will indent 4 lines, starting with the current line.

      • Likewise the << command will un-indent a line


      I hope this gives you a good start.






      share|improve this answer



















      • 7




        To auto-indent existing code, use ={motion} in normal mode -- e.g. =G will indent all code from the current line to the end of the file. == will indent the current line.
        – Edd Steel
        Sep 1 '11 at 22:36






      • 1




        also make sure indent plugins are being loaded (e.g. with :filetype indent on)
        – jw013
        Sep 2 '11 at 0:50






      • 2




        set tw=4 sets the TEXTWIDTH to 4
        – Steffen Roller
        Apr 25 '17 at 2:01













      up vote
      11
      down vote










      up vote
      11
      down vote









      I don't know about auto-formatting existing code, but if you are writing code and need auto indent:





      • :set autoindent (or :set ai) will turn on auto-indent


      • Ctrl-d to un-indent (AKA outdent) your code

      • Tab or spaces to indent -- I personally use tab


      • :set tabwidth=4 (or :set tw=4) will control how many spaces a tab should indent code

      • The >> command will indent the current line. If you prefix it by a number, say 4>> then it will indent 4 lines, starting with the current line.

      • Likewise the << command will un-indent a line


      I hope this gives you a good start.






      share|improve this answer














      I don't know about auto-formatting existing code, but if you are writing code and need auto indent:





      • :set autoindent (or :set ai) will turn on auto-indent


      • Ctrl-d to un-indent (AKA outdent) your code

      • Tab or spaces to indent -- I personally use tab


      • :set tabwidth=4 (or :set tw=4) will control how many spaces a tab should indent code

      • The >> command will indent the current line. If you prefix it by a number, say 4>> then it will indent 4 lines, starting with the current line.

      • Likewise the << command will un-indent a line


      I hope this gives you a good start.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Sep 1 '11 at 21:06









      enzotib

      33.4k710393




      33.4k710393










      answered Sep 1 '11 at 21:02









      Hai Vu

      91148




      91148








      • 7




        To auto-indent existing code, use ={motion} in normal mode -- e.g. =G will indent all code from the current line to the end of the file. == will indent the current line.
        – Edd Steel
        Sep 1 '11 at 22:36






      • 1




        also make sure indent plugins are being loaded (e.g. with :filetype indent on)
        – jw013
        Sep 2 '11 at 0:50






      • 2




        set tw=4 sets the TEXTWIDTH to 4
        – Steffen Roller
        Apr 25 '17 at 2:01














      • 7




        To auto-indent existing code, use ={motion} in normal mode -- e.g. =G will indent all code from the current line to the end of the file. == will indent the current line.
        – Edd Steel
        Sep 1 '11 at 22:36






      • 1




        also make sure indent plugins are being loaded (e.g. with :filetype indent on)
        – jw013
        Sep 2 '11 at 0:50






      • 2




        set tw=4 sets the TEXTWIDTH to 4
        – Steffen Roller
        Apr 25 '17 at 2:01








      7




      7




      To auto-indent existing code, use ={motion} in normal mode -- e.g. =G will indent all code from the current line to the end of the file. == will indent the current line.
      – Edd Steel
      Sep 1 '11 at 22:36




      To auto-indent existing code, use ={motion} in normal mode -- e.g. =G will indent all code from the current line to the end of the file. == will indent the current line.
      – Edd Steel
      Sep 1 '11 at 22:36




      1




      1




      also make sure indent plugins are being loaded (e.g. with :filetype indent on)
      – jw013
      Sep 2 '11 at 0:50




      also make sure indent plugins are being loaded (e.g. with :filetype indent on)
      – jw013
      Sep 2 '11 at 0:50




      2




      2




      set tw=4 sets the TEXTWIDTH to 4
      – Steffen Roller
      Apr 25 '17 at 2:01




      set tw=4 sets the TEXTWIDTH to 4
      – Steffen Roller
      Apr 25 '17 at 2:01










      up vote
      4
      down vote













      Auto Indent *.sh



      Just add the following lines in ~/.vimrc



      filetype indent on
      set smartindent
      autocmd BufRead,BufWritePre *.sh normal gg=G





      share|improve this answer























      • Does this apply only to editing with vim or all editor tools?
        – W.M.
        Aug 28 '16 at 16:18






      • 1




        ~./vimrc only applicable for vim editor.
        – Rahul Patil
        Aug 29 '16 at 3:54















      up vote
      4
      down vote













      Auto Indent *.sh



      Just add the following lines in ~/.vimrc



      filetype indent on
      set smartindent
      autocmd BufRead,BufWritePre *.sh normal gg=G





      share|improve this answer























      • Does this apply only to editing with vim or all editor tools?
        – W.M.
        Aug 28 '16 at 16:18






      • 1




        ~./vimrc only applicable for vim editor.
        – Rahul Patil
        Aug 29 '16 at 3:54













      up vote
      4
      down vote










      up vote
      4
      down vote









      Auto Indent *.sh



      Just add the following lines in ~/.vimrc



      filetype indent on
      set smartindent
      autocmd BufRead,BufWritePre *.sh normal gg=G





      share|improve this answer














      Auto Indent *.sh



      Just add the following lines in ~/.vimrc



      filetype indent on
      set smartindent
      autocmd BufRead,BufWritePre *.sh normal gg=G






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 16 at 18:22









      Jamie S

      32




      32










      answered Dec 20 '13 at 20:12









      Rahul Patil

      14.7k186082




      14.7k186082












      • Does this apply only to editing with vim or all editor tools?
        – W.M.
        Aug 28 '16 at 16:18






      • 1




        ~./vimrc only applicable for vim editor.
        – Rahul Patil
        Aug 29 '16 at 3:54


















      • Does this apply only to editing with vim or all editor tools?
        – W.M.
        Aug 28 '16 at 16:18






      • 1




        ~./vimrc only applicable for vim editor.
        – Rahul Patil
        Aug 29 '16 at 3:54
















      Does this apply only to editing with vim or all editor tools?
      – W.M.
      Aug 28 '16 at 16:18




      Does this apply only to editing with vim or all editor tools?
      – W.M.
      Aug 28 '16 at 16:18




      1




      1




      ~./vimrc only applicable for vim editor.
      – Rahul Patil
      Aug 29 '16 at 3:54




      ~./vimrc only applicable for vim editor.
      – Rahul Patil
      Aug 29 '16 at 3:54










      up vote
      2
      down vote













      This plugin makes it easier to perform formatting on your code. It integrates external formatters, and has a fallback on vim's indent functionality.



      https://github.com/Chiel92/vim-autoformat



      Also, notice the difference between formatting and indenting. Indenting only corrects the whitespace before every line, while formatting also deals with any other thing, such as whitespace around operators etc.






      share|improve this answer

























        up vote
        2
        down vote













        This plugin makes it easier to perform formatting on your code. It integrates external formatters, and has a fallback on vim's indent functionality.



        https://github.com/Chiel92/vim-autoformat



        Also, notice the difference between formatting and indenting. Indenting only corrects the whitespace before every line, while formatting also deals with any other thing, such as whitespace around operators etc.






        share|improve this answer























          up vote
          2
          down vote










          up vote
          2
          down vote









          This plugin makes it easier to perform formatting on your code. It integrates external formatters, and has a fallback on vim's indent functionality.



          https://github.com/Chiel92/vim-autoformat



          Also, notice the difference between formatting and indenting. Indenting only corrects the whitespace before every line, while formatting also deals with any other thing, such as whitespace around operators etc.






          share|improve this answer












          This plugin makes it easier to perform formatting on your code. It integrates external formatters, and has a fallback on vim's indent functionality.



          https://github.com/Chiel92/vim-autoformat



          Also, notice the difference between formatting and indenting. Indenting only corrects the whitespace before every line, while formatting also deals with any other thing, such as whitespace around operators etc.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 20 '13 at 20:01









          Chiel ten Brinke

          1316




          1316






















              up vote
              0
              down vote













              In bash I do this:



              source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
              declare -f Zibri| cut -c 5-|head --lines=-1|tail --lines=+3


              this eliminates comments and reindents the script "bash way".
              it will not work if the script contains HEREDOCS but if you do this:



              source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
              declare -f Zibri|head --lines=-1|tail --lines=+3


              it will work with any script but the whole script will be indented by 4 spaces. feel free to modify but cite my name in your script and post it! :D






              share|improve this answer





















              • I believe this may be missing the point of the question; rather than indenting a set of lines, I believe the OP wished to auto-indent within VIM for a variety of languages. As an aside, you may be able to achieve this a little more directly with sed if you so wish: cat script_to_be_reindented.sh | sed 's#^# #g' (er, well, SO markdown is replacing the spaces with a tab, but you get the point)
                – eacousineau
                Mar 2 '17 at 18:29

















              up vote
              0
              down vote













              In bash I do this:



              source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
              declare -f Zibri| cut -c 5-|head --lines=-1|tail --lines=+3


              this eliminates comments and reindents the script "bash way".
              it will not work if the script contains HEREDOCS but if you do this:



              source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
              declare -f Zibri|head --lines=-1|tail --lines=+3


              it will work with any script but the whole script will be indented by 4 spaces. feel free to modify but cite my name in your script and post it! :D






              share|improve this answer





















              • I believe this may be missing the point of the question; rather than indenting a set of lines, I believe the OP wished to auto-indent within VIM for a variety of languages. As an aside, you may be able to achieve this a little more directly with sed if you so wish: cat script_to_be_reindented.sh | sed 's#^# #g' (er, well, SO markdown is replacing the spaces with a tab, but you get the point)
                – eacousineau
                Mar 2 '17 at 18:29















              up vote
              0
              down vote










              up vote
              0
              down vote









              In bash I do this:



              source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
              declare -f Zibri| cut -c 5-|head --lines=-1|tail --lines=+3


              this eliminates comments and reindents the script "bash way".
              it will not work if the script contains HEREDOCS but if you do this:



              source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
              declare -f Zibri|head --lines=-1|tail --lines=+3


              it will work with any script but the whole script will be indented by 4 spaces. feel free to modify but cite my name in your script and post it! :D






              share|improve this answer












              In bash I do this:



              source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
              declare -f Zibri| cut -c 5-|head --lines=-1|tail --lines=+3


              this eliminates comments and reindents the script "bash way".
              it will not work if the script contains HEREDOCS but if you do this:



              source <(echo "Zibri () {";cat script_to_be_reindented.sh; echo "}")
              declare -f Zibri|head --lines=-1|tail --lines=+3


              it will work with any script but the whole script will be indented by 4 spaces. feel free to modify but cite my name in your script and post it! :D







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 15 '16 at 1:10









              Zibri

              14016




              14016












              • I believe this may be missing the point of the question; rather than indenting a set of lines, I believe the OP wished to auto-indent within VIM for a variety of languages. As an aside, you may be able to achieve this a little more directly with sed if you so wish: cat script_to_be_reindented.sh | sed 's#^# #g' (er, well, SO markdown is replacing the spaces with a tab, but you get the point)
                – eacousineau
                Mar 2 '17 at 18:29




















              • I believe this may be missing the point of the question; rather than indenting a set of lines, I believe the OP wished to auto-indent within VIM for a variety of languages. As an aside, you may be able to achieve this a little more directly with sed if you so wish: cat script_to_be_reindented.sh | sed 's#^# #g' (er, well, SO markdown is replacing the spaces with a tab, but you get the point)
                – eacousineau
                Mar 2 '17 at 18:29


















              I believe this may be missing the point of the question; rather than indenting a set of lines, I believe the OP wished to auto-indent within VIM for a variety of languages. As an aside, you may be able to achieve this a little more directly with sed if you so wish: cat script_to_be_reindented.sh | sed 's#^# #g' (er, well, SO markdown is replacing the spaces with a tab, but you get the point)
              – eacousineau
              Mar 2 '17 at 18:29






              I believe this may be missing the point of the question; rather than indenting a set of lines, I believe the OP wished to auto-indent within VIM for a variety of languages. As an aside, you may be able to achieve this a little more directly with sed if you so wish: cat script_to_be_reindented.sh | sed 's#^# #g' (er, well, SO markdown is replacing the spaces with a tab, but you get the point)
              – eacousineau
              Mar 2 '17 at 18:29












              up vote
              0
              down vote













              Create/edit the ~/.vimrc file and add the following line:



              set autoindent





              share|improve this answer

















              • 2




                This adds nothing to the existing answers...
                – jasonwryan
                Jan 30 '17 at 6:11










              • @jasonwryan Actually Hai Vu's answer mentions :set autoindent which enables auto indent only for the current file. Adding it in the .vimrc file enables auto indent permanently.
                – Akshat Maheshwari
                Jan 30 '17 at 7:12










              • your piece of comment is already mentioned by Rahul's answer, so think before posting
                – Indrajeet Gour
                May 8 '17 at 9:25















              up vote
              0
              down vote













              Create/edit the ~/.vimrc file and add the following line:



              set autoindent





              share|improve this answer

















              • 2




                This adds nothing to the existing answers...
                – jasonwryan
                Jan 30 '17 at 6:11










              • @jasonwryan Actually Hai Vu's answer mentions :set autoindent which enables auto indent only for the current file. Adding it in the .vimrc file enables auto indent permanently.
                – Akshat Maheshwari
                Jan 30 '17 at 7:12










              • your piece of comment is already mentioned by Rahul's answer, so think before posting
                – Indrajeet Gour
                May 8 '17 at 9:25













              up vote
              0
              down vote










              up vote
              0
              down vote









              Create/edit the ~/.vimrc file and add the following line:



              set autoindent





              share|improve this answer












              Create/edit the ~/.vimrc file and add the following line:



              set autoindent






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 30 '17 at 5:10









              Akshat Maheshwari

              1




              1








              • 2




                This adds nothing to the existing answers...
                – jasonwryan
                Jan 30 '17 at 6:11










              • @jasonwryan Actually Hai Vu's answer mentions :set autoindent which enables auto indent only for the current file. Adding it in the .vimrc file enables auto indent permanently.
                – Akshat Maheshwari
                Jan 30 '17 at 7:12










              • your piece of comment is already mentioned by Rahul's answer, so think before posting
                – Indrajeet Gour
                May 8 '17 at 9:25














              • 2




                This adds nothing to the existing answers...
                – jasonwryan
                Jan 30 '17 at 6:11










              • @jasonwryan Actually Hai Vu's answer mentions :set autoindent which enables auto indent only for the current file. Adding it in the .vimrc file enables auto indent permanently.
                – Akshat Maheshwari
                Jan 30 '17 at 7:12










              • your piece of comment is already mentioned by Rahul's answer, so think before posting
                – Indrajeet Gour
                May 8 '17 at 9:25








              2




              2




              This adds nothing to the existing answers...
              – jasonwryan
              Jan 30 '17 at 6:11




              This adds nothing to the existing answers...
              – jasonwryan
              Jan 30 '17 at 6:11












              @jasonwryan Actually Hai Vu's answer mentions :set autoindent which enables auto indent only for the current file. Adding it in the .vimrc file enables auto indent permanently.
              – Akshat Maheshwari
              Jan 30 '17 at 7:12




              @jasonwryan Actually Hai Vu's answer mentions :set autoindent which enables auto indent only for the current file. Adding it in the .vimrc file enables auto indent permanently.
              – Akshat Maheshwari
              Jan 30 '17 at 7:12












              your piece of comment is already mentioned by Rahul's answer, so think before posting
              – Indrajeet Gour
              May 8 '17 at 9:25




              your piece of comment is already mentioned by Rahul's answer, so think before posting
              – Indrajeet Gour
              May 8 '17 at 9:25










              up vote
              0
              down vote













              vim's autoformat/indent works pretty well. First, put this line in your ~/.vimrc:



              filetype plugin indent on


              Then open a file in vim and type gg=G



              (gg moves cursor to the first line. = runs the indent command. G tells indent command to run from here to the last line.)



              If the autoformat looks really bad, like every line is just left indented, then run :scriptnames and check if .../indent/html.vim (or whatever language you're using) is in the list. If not, then make sure your ~/.vimrc is correct. Or if you ran :filetype plugin indent on from the vim command line, you will need to re-open the file :e






              share|improve this answer

























                up vote
                0
                down vote













                vim's autoformat/indent works pretty well. First, put this line in your ~/.vimrc:



                filetype plugin indent on


                Then open a file in vim and type gg=G



                (gg moves cursor to the first line. = runs the indent command. G tells indent command to run from here to the last line.)



                If the autoformat looks really bad, like every line is just left indented, then run :scriptnames and check if .../indent/html.vim (or whatever language you're using) is in the list. If not, then make sure your ~/.vimrc is correct. Or if you ran :filetype plugin indent on from the vim command line, you will need to re-open the file :e






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  vim's autoformat/indent works pretty well. First, put this line in your ~/.vimrc:



                  filetype plugin indent on


                  Then open a file in vim and type gg=G



                  (gg moves cursor to the first line. = runs the indent command. G tells indent command to run from here to the last line.)



                  If the autoformat looks really bad, like every line is just left indented, then run :scriptnames and check if .../indent/html.vim (or whatever language you're using) is in the list. If not, then make sure your ~/.vimrc is correct. Or if you ran :filetype plugin indent on from the vim command line, you will need to re-open the file :e






                  share|improve this answer












                  vim's autoformat/indent works pretty well. First, put this line in your ~/.vimrc:



                  filetype plugin indent on


                  Then open a file in vim and type gg=G



                  (gg moves cursor to the first line. = runs the indent command. G tells indent command to run from here to the last line.)



                  If the autoformat looks really bad, like every line is just left indented, then run :scriptnames and check if .../indent/html.vim (or whatever language you're using) is in the list. If not, then make sure your ~/.vimrc is correct. Or if you ran :filetype plugin indent on from the vim command line, you will need to re-open the file :e







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 22 at 18:40









                  wisbucky

                  558610




                  558610






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f19945%2fauto-indent-format-code-for-vim%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Accessing regular linux commands in Huawei's Dopra Linux

                      Can't connect RFCOMM socket: Host is down

                      Kernel panic - not syncing: Fatal Exception in Interrupt