Unindenting code snippets
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 d开发者_高级运维oSomething() still has a single tab in front of it.
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.
The actual method to do this in Emacs is with this key combination.
First select the section you want to remove the tabs from.
You could select the entire buffer:
CTRL+x h
Or, if you just want a region simply set the mark CTRL+SPACE where you want to begin and then navigate to where you want to end.
Then remove whitespace, for 10 whitespace characters do this:
CTRL+- 10 CTRL+x TAB
Since this is quite big, I'll break it down for you.
1) First we give the negative-argument command:
(negative-argument ARG)
kbd shortcut: CTRL+-
2) Next supply the numerical argument, in this case the number 10:
10
3) Then we call the indent-rigidly command
(indent-rigidly START END ARG)
kbd shortcut: CTRL+x TAB
So what we are doing is giving the argument of -10 to the command to indent-rigidly and the result is that 10 whitespace characters will be removed from the beginning of each line which we have selected, in this case we have the entire document selected (CTRL+x h), so 10 lines of whitespace will be removed from the beginning of every line of the entire document.
If a particular line has less than 10 whitespace it will only remove as many whitespace as possible, if there are more than 10 whitespace then some may be left over after it is finished.
In your example it looks like you have about 30 leading whitespace, so this should do the trick:
CTRL+- 30 CTRL+x TAB
Try a larger number if you want to remove more.
If you have your emacs c-style settings correct, then simply highlighting the snippet and pressing C-M-\
will indent it properly (if you're in c++-mode).
Simplest answer I think . . to UN-indent selected region by 4:
C-u -4 C-x TAB
That is the simple region indent command with negative argument. e.g this indents region by one:
C-x TAB
https://www.gnu.org/software/emacs/manual/html_node/emacs/Indentation-Commands.html
If you know exactly how many tabs prefix each line (as you state), you could use a simple query-replace-regex
to replace "^\t\t\t...\t"
with ""
.
The method I use in Kedit (which has the capability of doing column editing) is to mark the first character of the first column and row with Alt-B, position my cursor to the last row and character that I want to remove, Alt-B again and then Alt-G. It's Gone and still has all of the indenting beyond the removed columns.
In SQL Server it's easier, mark the block and press shift-tab until the code is where I want it.
Use a regular expression to remove the leading whitespace.
In Emacs:
C-M-%^<TAB><TAB><TAB><ENTER><ENTER>!
In Vim:
:%s/^\t\t\t//g
In vi, it's just <<
to left shift by one indent width, so to unindent 10 lines, you would do 10<<
.
精彩评论