sameera207
sameera207

Reputation: 16629

VIM formatting/align

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

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

Upvotes: 3

Views: 585

Answers (3)

chtenb
chtenb

Reputation: 16214

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

Upvotes: 2

Bonlenfum
Bonlenfum

Reputation: 20195

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).

Upvotes: 5

guessimtoolate
guessimtoolate

Reputation: 8642

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).

Upvotes: 4

Related Questions