Question

I'm trying to make VIM as my ruby/rails editor. So fat I'm really impress with its features and I was able to install following plug ins to give me a better IDE experience

  • auto-pairs
  • better-snipmate-snippets
  • nerdcommenter
  • nerdtree
  • tabular
  • vim-endwise
  • vim-nerdtree-tabs
  • vim-rails
  • vim-surround

However I'm still having difficulties in finding a way to format/align my code. As an example sometimes I might copy and paste code from another place and then the entire code is scattered.

Ex: items.css.scss

.throw {
  float:left; width: 100%; 
                   margin-bottom: 2px; 
                        border: solid gray 1px; 
                                border-radius: 10px; 
                                            .cell { 
                                                            float: left; 
                                                                            padding-left: 6px; 
                                                                                            padding-right: 6px; 
                                                                                                         } 
                                                                                                             } 

I want it to be

.throw {
  float:left; width: 100%; 
  margin-bottom: 2px; 
  border: solid gray 1px; 
  border-radius: 10px; 
     .cell { 
       float: left; 
       padding-left: 6px; 
       padding-right: 6px; 
                                                                                                } 
                                                                                           } 

What would be the best plugin/method to align/format code in VIM

Was it helpful?

Solution 2

Try pasting it with ]p or issue :set paste before pasting and :set nopaste afterwards to put vim into paste mode when you're pasting code. Also you can map that to some key combination of your choosing so that you won't have to type it every time. You can get more info on both methods from vim's help (:h ]p, :h paste).

OTHER TIPS

There are two things that should help you:

  • the == command, which reindents anything currently selected (e.g. try V to select lines visually -- a useful combination is {V}= which will indent the current 'paragraph')

  • the :set paste command. Turn this on before you paste code into your file (and turn it back off again with :set nopaste when you're done).

If you are interested in formatting code in general, checkout this vim plugin called vim-autoformat: https://github.com/vim-autoformat/vim-autoformat

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top