{"id":1471,"date":"2016-12-30T18:36:34","date_gmt":"2016-12-30T18:36:34","guid":{"rendered":"http:\/\/cdblog.cdstealer.com\/?p=1471"},"modified":"2016-12-31T06:46:18","modified_gmt":"2016-12-31T06:46:18","slug":"vim-autocmd","status":"publish","type":"post","link":"https:\/\/cdblog.cdstealer.com\/?p=1471","title":{"rendered":"VIM autocmd"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1472\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/12\/Vimlogo.svg_-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/12\/Vimlogo.svg_-300x301.png 300w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/12\/Vimlogo.svg_-150x150.png 150w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/12\/Vimlogo.svg_.png 1022w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Want Vim to automatically do things to new or existing files? \u00a0Autocmd to the rescue! \u00a0Here I will demo a very simple example that I use for when I create\/edit bash\/python files.<\/p>\n<p>I created the directory .vim in the root of my home directory and then created the \"header\" files that I wanted.<\/p>\n<pre>$ cat .vim\/python_header\r\n:insert\r\n#!\/usr\/bin\/python3\r\n################################################################################\r\n####\r\n#### Filename:\r\n####\r\n#### Purpose:\r\n####\r\n#### Created on:\r\n####\r\n#### Author:\r\n####\r\n#### Last Modified:\r\n####\r\n################################################################################\r\n.<\/pre>\n<p>The first line\u00a0<strong>must<\/strong> be\u00a0<strong>:insert<\/strong> and the last line\u00a0<strong>must<\/strong> be a period '<strong>.<\/strong>'<\/p>\n<p>I then added the following lines to the .vimrc file in the root of my home directory.<\/p>\n<pre>\" Python file header\r\nautocmd bufnewfile *.py so \/home\/cdstealer\/.vim\/python_header\r\nautocmd bufnewfile *.py exe \"4\" \"g\/Filename:.*\/s\/\/Filename:\\t\\t\\t\" .expand(\"%\")\r\nautocmd bufnewfile *.py exe \"6\" \"g\/Purpose:.*\/s\/\/Purpose:\\t\\t\\t\"\r\nautocmd bufnewfile *.py exe \"8\" \"g\/Created on:.*\/s\/\/Created on:\\t\\t\" .strftime(\"%c\")\r\nautocmd bufnewfile *.py exe \"10\" \"g\/Author:.*\/s\/\/Author:\\t\\t\\t\" .$USER\r\nautocmd Bufwritepre,filewritepre *.py execute \"normal ma\"\r\nautocmd BufWritePre,FileWritePre *.py exe \"12\" \"g\/Last Modified:.*\/s\/Last Modified:.*\/Last Modified:\\t\\t\" .strftime(\"%c\") . \" by \" . $USER\r\nautocmd bufwritepost,filewritepost *.py execute \"normal `a\"<\/pre>\n<p>Note: Lines that start with a double quote are treated as comments.<\/p>\n<p>The official docs can be found <a href=\"http:\/\/vimdoc.sourceforge.net\/htmldoc\/autocmd.html\" target=\"_blank\">here<\/a>.<\/p>\n<p>Explanation:<\/p>\n<p>The first autocmd line specifies the file. \u00a0Due to the :insert, this is then inserted into the new file.<\/p>\n<p>bufnewfile: means take the following action on a new file.<br \/>\nBufwritepre,filewritepre: means update the file only if editing not creating.<\/p>\n<p>Lines 2, 3, 4 &amp; 5 are basic search and replace for the header file being inserted.<\/p>\n<p>The number in quotes after the exe is the line number to work on. \u00a0You can set this to a range instead of being specific. \u00a0The syntax is:<\/p>\n<pre>\"&lt;starting line number&gt;,\" . &lt;end line number&gt; .<\/pre>\n<p>Once saved, you can test straight away by starting a new file:<\/p>\n<pre>$ vi python_test.py\r\n\r\n1 #!\/usr\/bin\/python3\r\n2 ################################################################################\r\n3 ####\r\n4 #### Filename:\u2003python_test.py\r\n5 ####\r\n6 #### Purpose:\r\n7 ####\r\n8 #### Created on: Fri 30 Dec 2016 19:18:58 GMT\r\n9 ####\r\n10 #### Author: cdstealer\r\n11 ####\r\n12 #### Last Modified:\r\n13 ####\r\n14 ################################################################################<\/pre>\n<p>Save the file and then re-open, make a small change and the \"Last Modified\" line will now contain a date stamp.<\/p>\n<pre>1 #!\/usr\/bin\/python3\r\n2 ################################################################################\r\n3 ####\r\n4 #### Filename: python_test.py\r\n5 ####\r\n6 #### Purpose: To test\r\n7 ####\r\n8 #### Created on: Fri 30 Dec 2016 19:18:58 GMT\r\n9 ####\r\n10 #### Author: cdstealer\r\n11 ####\r\n12 #### Last Modified: Fri 30 Dec 2016 19:20:38 GMT by cdstealer\r\n13 ####\r\n14 ################################################################################<\/pre>\n<p>Very very useful ;)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Want Vim to automatically do things to new or existing files? \u00a0Autocmd to the rescue! \u00a0Here I will demo a very simple example that I use for when I create\/edit bash\/python files. I created the directory .vim in the root of my home directory and then created the \"header\" files that I wanted. $ cat &hellip; <a href=\"https:\/\/cdblog.cdstealer.com\/?p=1471\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">VIM autocmd<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[258,18,257,256],"class_list":["post-1471","post","type-post","status-publish","format-standard","hentry","category-gentoo","tag-autocmd","tag-linux","tag-vi","tag-vim"],"_links":{"self":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1471"}],"version-history":[{"count":7,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1471\/revisions"}],"predecessor-version":[{"id":1487,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1471\/revisions\/1487"}],"wp:attachment":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}