4

I am writing a Python script which writes some formatted data into a YAML file. I am using tabs for formatting the text, but I want the tabs to be converted into spaces when written into the YAML file. This is because my YAML file does not take tabs as valid indentation tokens.

I have tried this:

Step 1: Go to your home directory

cd ~

Step 2: Create the file

vim .vimrc

Step 3: Add the configuration stated below

set smartindent

set tabstop=4

set shiftwidth=4

set expandtab

:retab

But this does not work. The YAML file created still has tabs. The tabs are not converted to spaces.

Please suggest what changes should I make into my .vimrc file so that tabs are converted into spaces for valid indentation.

The Python script:

template = open("/home/stack/horizon/openstack_dashboard/dashboards/mydashboard/mypanel/extracted_template.yaml","w")

networks = api.neutron.network_list_for_tenant(self.request,tenant_id,params={}) 

for n in range(0,len(networks)):
      n_name = networks[n]['name']
      print>>template,"\tprivate_net%d:"%n,"\n\t type: OS::Neutron::Net","\n\t properties:","\n\t  name:",n_name          
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
user189942
  • 351
  • 1
  • 3
  • 12
  • Do you use any plugins that could modify the tabs settings at run time? – svlasov Apr 28 '15 at 09:29
  • This is not a vim problem. This is a problem in Python if you want the initial output to contain spaces instead of tabs. Please show us the Python source. – merlin2011 Apr 29 '15 at 00:14
  • I have successfully converted tabs into spaces by following what @byaruhaf has suggested. But the problem is still the same. The converted tabs are still treated as tabs which is not a valid indentation token for YAML files. – user189942 Apr 29 '15 at 10:20

3 Answers3

6

To convert tabs to spaces use the commands below in the order below:

:set noexpandtab
:retab!
:set expandtab
:retab!

I go this answer from watching this vim cast. Tidying Whitespace

Vim can already detect the file type yaml so you can use that to replace all TAB's in your YAML file add this your .vimrc

:autocmd FileType yaml execute  ':silent! %s#^\t\+#\=repeat(" ", len(submatch(0))*' . &ts . ')'
byaruhaf
  • 4,128
  • 2
  • 32
  • 50
  • What I am looking for is not that I open the file and then enter the command :retab! to convert tabs into spaces. I need to make this automated. I want when I open the files, the tabs are already converted to spaces. For this I need to edit the .vimrc file for permanent settings but I don't know how to do it. – user189942 Apr 28 '15 at 11:16
  • modified the answer to include an autocmd for your vimrc – byaruhaf Apr 28 '15 at 16:01
  • Even this is not working for me. I have added these lines to .vimrc but the tab is not converted to spaces. – user189942 Apr 29 '15 at 05:31
  • Changed the autocmd to search and replace all tabs for a yaml file, can you try this and let me know if it works. – byaruhaf Apr 29 '15 at 08:38
4

Create a .vimrc file in your home directory if it does not exist. Edit the file and add following lines

set tabstop=4
set shiftwidth=4
set expandtab
karthik
  • 7,041
  • 1
  • 13
  • 12
0

To manage YAML files correctly, edit /etc/vim/vimrc.local or .vimrc or other VIM configuration file adding the following line:

autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab nocindent smartindent