0

I am having the hardest time indenting things consistently in vim, and get no end of IndentationError in python. Even setting all these tab-related settings: https://stackoverflow.com/a/234578/651174 makes things no better.

How would I do the following in vim to normalize all indentation for a block of text? https://gyazo.com/a333f05e8b4d8034967029005f77ea27

enter image description here

I've tried doing the ctrl v visual mode, but can't seem to figure out how to use that for my purpose, or if there's something else I should be doing here. What would be the best way to do the above?

David542
  • 104,438
  • 178
  • 489
  • 842
  • After selecting with `ctrl v`, hit `I` (uppercase 'I') and insert the desired number of spaces (or tabs), and `Esc` when done. Then, one can use `:set list` to check the result. `:set nolist` turns the setting back off. – bert Apr 14 '20 at 03:20
  • @bert -- thanks, what does `:set (no)list` do? – David542 Apr 14 '20 at 03:27
  • 1
    `:set list` allows spaces, tabs, eols and other hidden characters to be displayed in an obvious way. For example, tabs are shown as `^I`. Take a look at `:help 'list'` and `:help 'listchars'` for more. – bert Apr 14 '20 at 03:31
  • For future use, take a look at indentLine plugin which will ease your indentation problem :) https://github.com/Yggdroot/indentLine.git – PathSeeker Apr 14 '20 at 13:28

1 Answers1

0

use visual mode to select a block of text and hit '=' to indent the code correctly. To effect the whole file use gg=G, which translates to go to the beginning of the file and indent correctly to the bottom of the file. Put the following in your vimrc and use :ReformatFile for easy re-indenting.

function ReformatFile()
    exec "normal! mqHmw"
    exec "normal! gg=G"
    exec "normal! 'wzt`q"
endfunction
command ReformatFile silent call ReformatFile()

mqHmw marks your current position and and the top of your current window see :h marks

"'wzt`q" moves your cursor to the mark w, moves the line to the top of the file and than to the line and column of mark q

Sry for my bad english, but try to type out the commands in vim and read the docs. The marks q/w are chosen arbitrary.

Chelz
  • 437
  • 1
  • 4
  • 9