{"id":130,"date":"2007-03-09T10:36:17","date_gmt":"2007-03-09T18:36:17","guid":{"rendered":"http:\/\/outflux.net\/blog\/archives\/2007\/03\/09\/detecting-space-vs-tab-indentation-type-in-vim\/"},"modified":"2007-03-09T10:38:18","modified_gmt":"2007-03-09T18:38:18","slug":"detecting-space-vs-tab-indentation-type-in-vim","status":"publish","type":"post","link":"https:\/\/outflux.net\/blog\/archives\/2007\/03\/09\/detecting-space-vs-tab-indentation-type-in-vim\/","title":{"rendered":"detecting space-vs-tab indentation type in vim"},"content":{"rendered":"<p>I edit a lot of other people&#8217;s code.  Dealing with indenting depth has always plagued me, and I&#8217;ve tried all sorts of things to try to address it, but the &#8220;real&#8221; problems I have are when tabs are mixed into code.<\/p>\n<p>I personally use &#8220;4 spaces&#8221; for code indentation, and if I&#8217;m working on code that uses 8, I just hit &#8220;tab&#8221; twice, and if I&#8217;m working on code that uses 2, I can just backspace over the 2-too-many spaces.  When the code has actual tabs, things break.  When the code has a <em>mix<\/em> of tabs and spaces, it becomes a serious head-ache.<\/p>\n<p>I wrote some vim insanity to detect which indentation type was being used &#8220;the most&#8221; in a given source file.  If anyone has a simpler way to solve this (without switching to a different editor), I&#8217;m all ears.  What follows are some bits from my .vimrc.<\/p>\n<p>First, my space-indentation defaults:<\/p>\n<pre>\r\nset noai ts=4 sw=8 expandtab\r\n<\/pre>\n<p>Next, Makefiles and debian\/rules files always use tabs, so I have a base set of overrides:<\/p>\n<pre>\r\n\" Makefile sanity\r\nautocmd BufEnter ?akefile* set noet ts=8 sw=8\r\nautocmd BufEnter *\/debian\/rules set noet ts=8 sw=8\r\n<\/pre>\n<p>Finally, define a function that compares the number of lines that start with a tab to those that start with a space.  If the tabs outnumber the spaces, disable my defaults, and don&#8217;t expand tabs:<\/p>\n<pre>\r\nfunction Kees_settabs()\r\n    if len(filter(getbufline(winbufnr(0), 1, \"$\"), 'v:val =~ \"^\\\\t\"')) &gt; len(filter(getbufline(winbufnr(0), 1, \"$\"), 'v:val =~ \"^ \"'))\r\n        set noet ts=8 sw=8\r\n    endif\r\nendfunction\r\nautocmd BufReadPost * call Kees_settabs()\r\n<\/pre>\n<p style='text-align:left'>&copy; 2007, <a href=\"https:\/\/outflux.net\/blog\/\">Kees Cook<\/a>. This work is licensed under a <a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\">Creative Commons Attribution-ShareAlike 4.0 License<\/a>.<br \/><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><img decoding=\"async\" alt=\"CC BY-SA 4.0\" style=\"border-width:0\" src=\"https:\/\/i.creativecommons.org\/l\/by-sa\/4.0\/88x31.png\" \/><\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I edit a lot of other people&#8217;s code. Dealing with indenting depth has always plagued me, and I&#8217;ve tried all sorts of things to try to address it, but the &#8220;real&#8221; problems I have are when tabs are mixed into code. I personally use &#8220;4 spaces&#8221; for code indentation, and if I&#8217;m working on code [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,14],"tags":[],"_links":{"self":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/130"}],"collection":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":0,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"wp:attachment":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}