1
:set et tabstop=2 shiftwidth=2

Above was my initial setting of ~/.vimrc when I created a python(.py) file. I have 'expanded' all the tabs to spaces. My file has grown quite large now. Now I want all the tabs(that already exist in the file and those that I will add further), to have 4 spaces (which was 2 initially). So I tried:

:set et tabstop=4 shiftwidth=4 

in both, the current .py file and also by adding(editing) in my ~/.vimrc file. This only affects the changes that I make now. The rest of the file remains as such. Any help appreciated. Help me accept vim I might be on the verge of quitting vim:)

Amiay Narayan
  • 461
  • 9
  • 8

1 Answers1

0

This is Python, :retab and/or gg=G won't be enough.

May be (untested)... you could try to

  1. force setlocal noexpandtab,
  2. :retab! to force the use of tabs -- notice the bang (!)
  3. change setlocal sw=4 and setlocal tabstop=4,
  4. re-setlocal expandab
  5. :retab
  6. restore tabstop to 8 -- i.e. just leave the default Python settings as they are.
Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
  • Thanks for reply. The above works if the initially the expandtab was set to off i.e. , `:setlocal noexpandtab` and then the file was written. So, the tabs were registered as `^I` , can be checked - `:set list`. In my case tabs were registered as spaces(2 spaces). So `:retab` is not working in my case( I think). Once again thanks for your reply – Amiay Narayan Sep 01 '20 at 12:59
  • If the file is indented with spaces, with a shift of 2 each time, if you set &sw and &ts to 2, expandtab to true, then with `:retab` the file shall be converted to use tab characters to indent. And the other way around when changing to noexpandtab. – Luc Hermitte Sep 01 '20 at 13:37
  • My mistake. It should be `:retab!` to change from spaces to tabs. And `:retab` for the other way around – Luc Hermitte Sep 01 '20 at 13:52
  • [https://stackoverflow.com/a/9105889/13056176], here I found @Johnsyweb 's comment, this solved my problem. `:set noet ts=2 |%retab!` ; I think you meant the same. after that I changed `:set ts=4` as required. Thanks. – Amiay Narayan Sep 02 '20 at 07:12