I pasted a large section of python code under vim, but starting from a certain line, all the code was moved back by one more tab. There were more than 100 lines. I manually adjusted them one by one, and I was exhausted.
Is there any way to make it easier?
In vim, it is best to enter paste mode before pasting code, so that automatic indentation will be turned off
Paste the code and then turn off the paste mode
The indentation of a single line of code is two greater than signs '>>'
Retraction is two less than signs '<<'
If you want to indent many lines of code, do the following
:10,100>
Indent line 10 to line 100
:20,80<
Line 20 to line 80 are anti-indented
In normal mode, the cursor is on the starting line that needs to be processed, then: 100>>
1,^V in front of the line that needs to be indented;
2,I;
3. tab
1 Press Esc to enter command line mode
2 Move the cursor to the starting line that needs to be processed
3 Press v to go to view mode and select all the lines that need to be processed
4 Press Ctrl+<That’s it
Be careful when pasting in Vim, if Vim cannot know that you are pasting (non-GUI version, mouse support is not turned on in the terminal or the terminal itself does not support the mouse, because Vim does not have X clipboard support and bypasses Vim, use the The Shift key allows the terminal to paste directly (for example, when pasting into Vim in ssh, usually Vim cannot access the local clipboard, so it can only let the terminal "type")), then you need to set the 'paste' option before pasting . This option disables automatic indentation, mapping, etc. in insert mode. See
for details:h 'paste'
You can play like this:
If you need to delete a tab from line 10 to line 100, you can write like this
:10,100s/^t//
That’s ok!
Define various tabs of python in vimrc, for example:
Then in normal mode, enter directly
=nj
n is the number of lines required to be automatically aligned. No matter how messy the indentation is, it will be automatically aligned
The commands provided by vim below are very flexible, but they are too low-level. At present, all the answers given by the respondents are hard-keyed, which is difficult to remember and inefficient. It is recommended to set them as shortcut keys.
For example, I am used to using tab, selecting the target area and pressing the shortcut key.
Take the subject’s question as an example.
End~
Provide another method without entering V mode.
Move to the first line that needs to be processed, ma
Move to the last line that needs to be processed, <'a