search

Home  >  Q&A  >  body text

How to move a large section of Python code forward by one tab under vim?

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?

伊谢尔伦伊谢尔伦2742 days ago1665

reply all(11)I'll reply

  • 漂亮男人

    漂亮男人2017-05-16 16:43:40

    In vim, it is best to enter paste mode before pasting code, so that automatic indentation will be turned off

    set paste
    

    Paste the code and then turn off the paste mode

    set nopaste
    

    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

    1 //在这里按下'v'进入选择模式
    1
    1
    1
    1
    1
    1//光标移动到这里,再按一次大于号'>'缩进一次,按'6>'缩进六次,按'<'回缩。以下同理
    
    function helo{//将光标移动到'{',在按下'%',光标将会移动到匹配的括号
    
    //这里省略1000行
    
    }//光标会移动到这里,再按一次大于号'>'就可以缩进
    

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 16:43:40

    :10,100>
    Indent line 10 to line 100
    :20,80<
    Line 20 to line 80 are anti-indented

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 16:43:40

    In normal mode, the cursor is on the starting line that needs to be processed, then: 100>>

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 16:43:40

    1,^V in front of the line that needs to be indented;
    2,I;
    3. tab

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-16 16:43:40

    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

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 16:43:40

    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 :h 'paste'

    for details

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 16:43:40

    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!

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 16:43:40

    Define various tabs of python in vimrc, for example:

    autocmd FileType python set ts=4 | set sw=4 | set expandtab    
    

    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

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 16:43:40

    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.

    vimlvnoremap <tab> >gv
    vnoremap <s-tab> <gv
    

    Take the subject’s question as an example.

    1. Move to the first line of the target area
    2. Enter v to enter selection mode
    3. Enter 100j to move down 100 lines. At this time, the target area of ​​100 lines has been selected
    4. Enter the < tab > key.

    End~

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 16:43:40

    Provide another method without entering V mode.

    1. Move to the first line that needs to be processed, ma

    2. Move to the last line that needs to be processed, <'a

    reply
    0
  • Cancelreply