Tip – Vim Indent Solution

Some time back we wrote about a tip to fix vim indentation getting problematic when copy-pasting. That works great – but only if the original code that you are copying is indented properly. It may be indented properly but with different indentation settings (tabs instead of spaces, indent of 2 or 8 spaces instead of your favourite of 4 spaces). When added to the your code, it stands out awkwardly from the rest of the code.

Here’s another use case – as programmers, we often encounter badly indented code and if its a new piece of code that you are looking at trying to comprehend before you make changes, bad indenting is a big downer.

So what do you do? Indent the code to match the rest of the code? Manually?

Here’s a faster way -

1. Visually select the offending lines of code
2. Press = (equal)

Magic! All this is required to be done to align a piece of code copied with a different indentation to the current code’s indentation.

Following are the before and after snapshots that illustrate how it works.

Before

vim indent before Tip   Vim Indent Solution

After

vim indent after Tip   Vim Indent Solution

This is a guest post by one of the regular readers of Inficone – Srivats P . He is also the developer of the open source network traffic generator tool called Ostinato.

Please try the above and let us know your feedback in the comments section. You can always submit useful tools web services and tips to us.






Related posts:

  1. Tip – Vim Indent Paste Solution
  2. Linux – Search Text In Files And Open Result In VIM
  3. WordPress Missed Schedule Solution
  4. How To Display CSS/HTML/PHP/Ruby Code Snippets Inside WordPress Post
  5. Rails – Private Method `gsub!’ called for – Solution

Inficone runs on the Genesis Framework

Genesis Framework
Genesis empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go. It's that simple - start using Genesis now!
Follow On Twitter
Follow On Facebook
Follow Using RSS
Follow Using Email
Tweet This Post
Share Post On Facebook
Digg This Post
Add To Delicious
Stumble Upon

Comments

  1. I have not tried ‘VIM”. I found your article interesting, so I thought may I would give VIM a try. I am a PC use so I was happy to see there was a version available for Windows. I downloaded it and will give it. I am not much of a coder, but I do have to edit code quite often. Thanks for the tip!

    • Larry – Trust me – when you will start using vim, you will not go back to any other editor. Note that you need to give yourself a bit of time to use it properly. But once you get comfortable with it, there will be no looking back. You can download it from vim.org

Speak Your Mind

*

Spam Protection by WP-SpamFree