{"id":1905,"date":"2020-12-05T12:57:12","date_gmt":"2020-12-05T12:57:12","guid":{"rendered":"https:\/\/linuxdigest.com\/?p=1905"},"modified":"2020-12-13T18:11:43","modified_gmt":"2020-12-13T18:11:43","slug":"how-to-autoindent-in-vim","status":"publish","type":"post","link":"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/","title":{"rendered":"How to autoindent in vim"},"content":{"rendered":"\n<p>When you are working with a file type vim doesn&#8217;t recognize and doesn&#8217;t indent automatically, autoindent can be helpful. So on to how to autoindent in vim.<\/p>\n\n\n\n<p><strong>How do you autoindent? Autoindent tells vim to copy the indentation of the current line to a new line. This is enabled with &#8220;:set autoindent&#8221;. The command to turn it off is &#8220;:set noautoindent&#8221;. However, there are more efficient methods available.<\/strong><\/p>\n\n\n\n<p>Read on to better understand how vim handles automatic indentation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Smart indentation is slightly smarter<\/h2>\n\n\n\n<p>Auto indent only copies the indentation of the current line and applies that to the next line you create. There is another option called smartindent. Smartindent will try to detect how the code should be indented by the style of the code. This option is mostly only helpful with C like programs.<\/p>\n\n\n\n<p>To enable smartindent, all that is needed is to run the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set smartindent<\/code><\/pre>\n\n\n\n<p>To disable smart indentation run the opposite command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set nosmartindent<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">For C code use cindent<\/h2>\n\n\n\n<p>If you are writing C code, cindent might be a better choice than smartindent. Cindent will insert the amount of indenting that is required by C indenting rules.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set cindent<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Vim handles indentation automatically for most popular file types.<\/h2>\n\n\n\n<p>Smartindent and autoindent are both very helpful features. Especially if the file type is not very common. But there is a better way. This better way is a feature that is enabled by default in many installations. This method is much more powerful and flexible than the methods mentioned above.<\/p>\n\n\n\n<p>Vim has a built-in feature for file type detection. The file type detection will also enable automatic indentation for that file type if enabled. To reveal if file type detection is enabled and how it is set up, type in the command <strong>:filetype<\/strong>. This will display whether file type detection is on and also if that file types plugin is loaded and indentation is enabled.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"558\" height=\"161\" src=\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/12\/filetype-detection.png\" alt=\"\" class=\"wp-image-1908\" srcset=\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/12\/filetype-detection.png 558w, https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/12\/filetype-detection-300x87.png 300w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><\/figure><\/div>\n\n\n\n<p>If file type detection is not on, you can turn it on in your vimrc file. Your vimrc file should be either in your home directory as .vimrc or under \/etc\/vim\/vimrc. set the following parameter in your vimrc file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set filetype on<\/code><\/pre>\n\n\n\n<p>To enable vim to load a plugin based on the file type that was detected, set the filetype plugin parameter in your vimrc:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set filetype plugin on<\/code><\/pre>\n\n\n\n<p>You may also need to enable loading of the indent file for the particular file type. Do that like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set filetype indent on<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How file type detection works<\/h3>\n\n\n\n<p>File type detection uses several vim scripts that are stored in the vim runtime directory. A common location for this directory is \/usr\/share\/vim\/vim[xy]\/ where [xy] is version number of vim. <\/p>\n\n\n\n<p>The first of these scripts is filetype.vim. This script is run each time you open or create a new file. It will try to detect the type of file that is being opened by its file extension and in some cases the content of the file. <\/p>\n\n\n\n<p>If a filetype is successfully detected, vim will look for a plugin for that file type. The plugins are located in a subdirectory named ftplugins\/.<\/p>\n\n\n\n<p>Likewise, if indentation is enabled, vim will load indentation files that are located in the subdirectory indent\/.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Indent using spaces<\/h2>\n\n\n\n<p>Let&#8217;s not get into a long discussion on tabs vs. spaces. But suffice to say, many people prefer to use 2 or 4 spaces instead of a single tab. This can be a good way to ensure that the code is consistently formatted in what ever editor it is opened in. But, who would want to hit space 4 times instead of hitting the tab key once?<\/p>\n\n\n\n<p>There is a simple solution to this, expandtab. Expandtab does exactly what it sounds like. It expands each tab to a certain number of spaces. To turn this option on, just type:<\/p>\n\n\n\n<p> <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set expandtab<\/code><\/pre>\n\n\n\n<p>And again to turn it of just add no in front of the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set noexpandtab<\/code><\/pre>\n\n\n\n<p>Now, you just have to set how many spaces you want each tab to expand to. To do this, you will need two options, tabstop and shiftwidth. The value of tabstop will determine how many spaces are inserted for every tab. Shiftwidth will determine how many spaces make up one level of indentation. So if you want to auto indent 4 spaces instead of one tab enter the following in your vim session or put it in your vimrc file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set expandtab\n:set tabstop=4\n:set shiftwidth=4<\/code><\/pre>\n\n\n\n<p>You could also do all of this with just line of commands. For example, if you want to automatically auto indent by 2 spaces. You can enter all of the lines above like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set tabstop=2 shiftwidth=2 expandtab<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This has gone on a bit longer than you would expect for a simple option like autoindent. The reason being that autoindent is not always going to be the best option. If you are trying to find the best way to auto indent your code in vim, you are most likely going to want to use the indenting feature of vim&#8217;s file type detection.<\/p>\n\n\n\n<p>Now, you should have all the information you need to choose the code formatting method that fits your workflow the best.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you are working with a file type vim doesn&#8217;t recognize and doesn&#8217;t indent automatically, autoindent&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1924,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[38],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to autoindent in vim - Linux Digest<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to autoindent in vim - Linux Digest\" \/>\n<meta property=\"og:description\" content=\"When you are working with a file type vim doesn&#8217;t recognize and doesn&#8217;t indent automatically, autoindent&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Digest\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-05T12:57:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-13T18:11:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/12\/Vimlogo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"200\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"The Linux Digest Guy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"The Linux Digest Guy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/\"},\"author\":{\"name\":\"The Linux Digest Guy\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\"},\"headline\":\"How to autoindent in vim\",\"datePublished\":\"2020-12-05T12:57:12+00:00\",\"dateModified\":\"2020-12-13T18:11:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/\"},\"wordCount\":840,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"articleSection\":[\"Vim\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/\",\"url\":\"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/\",\"name\":\"How to autoindent in vim - Linux Digest\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/#website\"},\"datePublished\":\"2020-12-05T12:57:12+00:00\",\"dateModified\":\"2020-12-13T18:11:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/linuxdigest.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to autoindent in vim\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/linuxdigest.com\/#website\",\"url\":\"https:\/\/linuxdigest.com\/\",\"name\":\"Linux Digest\",\"description\":\"Linux tutorials for everyone\",\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/linuxdigest.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/linuxdigest.com\/#organization\",\"name\":\"Linux Digest\",\"url\":\"https:\/\/linuxdigest.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"contentUrl\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"width\":1102,\"height\":170,\"caption\":\"Linux Digest\"},\"image\":{\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\",\"name\":\"The Linux Digest Guy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"caption\":\"The Linux Digest Guy\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to autoindent in vim - Linux Digest","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/","og_locale":"en_US","og_type":"article","og_title":"How to autoindent in vim - Linux Digest","og_description":"When you are working with a file type vim doesn&#8217;t recognize and doesn&#8217;t indent automatically, autoindent&hellip;","og_url":"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/","og_site_name":"Linux Digest","article_published_time":"2020-12-05T12:57:12+00:00","article_modified_time":"2020-12-13T18:11:43+00:00","og_image":[{"width":200,"height":200,"url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/12\/Vimlogo.jpg","type":"image\/jpeg"}],"author":"The Linux Digest Guy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"The Linux Digest Guy","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/#article","isPartOf":{"@id":"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/"},"author":{"name":"The Linux Digest Guy","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2"},"headline":"How to autoindent in vim","datePublished":"2020-12-05T12:57:12+00:00","dateModified":"2020-12-13T18:11:43+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/"},"wordCount":840,"commentCount":0,"publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"articleSection":["Vim"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/","url":"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/","name":"How to autoindent in vim - Linux Digest","isPartOf":{"@id":"https:\/\/linuxdigest.com\/#website"},"datePublished":"2020-12-05T12:57:12+00:00","dateModified":"2020-12-13T18:11:43+00:00","breadcrumb":{"@id":"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/linuxdigest.com\/howto\/how-to-autoindent-in-vim\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxdigest.com\/"},{"@type":"ListItem","position":2,"name":"How to autoindent in vim"}]},{"@type":"WebSite","@id":"https:\/\/linuxdigest.com\/#website","url":"https:\/\/linuxdigest.com\/","name":"Linux Digest","description":"Linux tutorials for everyone","publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxdigest.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxdigest.com\/#organization","name":"Linux Digest","url":"https:\/\/linuxdigest.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","contentUrl":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","width":1102,"height":170,"caption":"Linux Digest"},"image":{"@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2","name":"The Linux Digest Guy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","caption":"The Linux Digest Guy"}}]}},"_links":{"self":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1905"}],"collection":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/comments?post=1905"}],"version-history":[{"count":4,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1905\/revisions"}],"predecessor-version":[{"id":1915,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1905\/revisions\/1915"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media\/1924"}],"wp:attachment":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media?parent=1905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/categories?post=1905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/tags?post=1905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}