Search code examples
ruby-on-railscodemirror

Codemirror tabs to spaces


I'm using Codemirror in an app. Has anyone found a way to make it uses spaces when using tab? When I press enter to go to a new line it uses spaces. However, if I backspace to the beginning of the line, and use a tab instead, it inserts it as a tab instead of 2 or 4 spaces or whatever I have it set to. Then, when I view the rendered view, my code has like 8 spaces for indentations in the areas where I used the tab key. Is there some option or way to convert actual tabs to spaces in Codemirror?

I'm also using the codemirror-rails gem, which may not be as up-to-date as the actual Codemirror version.


Solution

  • I would override the default tab key functionality and replace it with this function. Add this to your Codemirror configurations.

    extraKeys: {
                "Tab": function(cm){
                  cm.replaceSelection("   " , "end");
                }
               }
    

    Demo jsfiddle