Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unindenting code snippets

Tags:

There are times that I have a code snippet that I want to post on a defect tracker, wiki, forum, etc. but it's deeply indented in the code. I usually have to highlight the code snippet, hit <shift>-<tab> until the indents aren't so bad, then copy/paste and then revert the code. It gets somewhat painful.

Is there a tool that can remove the tabs in front of the each line? Note that I don't want to remove all tabs from all lines, just same preceding number of tabs from each line. I'm thinking some function of Emacs could do this.

Here's an extreme example:

Before:

                             //This is usually some method in an anonymous inner class' anonymous inner class.                             @Override                             public void method(){                                 doSomething();                             } 

After:

 //This is usually some method in an anonymous inner class' anonymous inner class. @Override public void method(){     doSomething(); } 

Notice how doSomething() still has a single tab in front of it.

like image 459
User1 Avatar asked Jan 13 '10 21:01

User1


People also ask

How do you do a Unindent code?

The "Unindent" command corresponds to the Unindent selection entry in the Advanced sub menu of the Edit Menu . It may be called by using the keyboard shortcut "Shift+Tab". Its action is to remove the tabulation in front of each selected line if there is such a character.

What is a code snippet example?

Code snippet: A code snippet is any example in the documentation. It shows how to use a specific member or how to accomplish a specific task. It might be a short snippet that focuses on a specific task (for example, how to cause a button to change color using the onhover event), or a longer tutorial or how-to.

How do you remove indents from multiple lines?

If you're using IDLE, you can use Ctrl+] to indent and Ctrl+[ to unindent.


1 Answers

Rectangle selection is my preferred way of doing this.

Put yourself at the beginning of the first line, C-space, go to the last line, and the end of the indentation you want to remove and C-x r k (rectangular kill). That does it.

like image 157
Bahbar Avatar answered Sep 25 '22 15:09

Bahbar