{"id":161,"date":"2020-09-01T21:54:00","date_gmt":"2020-09-01T21:54:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=161"},"modified":"2020-12-02T21:57:46","modified_gmt":"2020-12-02T21:57:46","slug":"vi-editor","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/vi-editor\/","title":{"rendered":"vi editor"},"content":{"rendered":"\n<p>The VI editor is a screen-based editor. It is very powerful and has been around for more than a decade. Programmers are especially fond of the VI editor due to its powerful features to aid programmers. VI is often overwhelming for beginners. It takes some time to get used to VI but it is often well worth the effort. The key to mastering VI is to learn sequentially, getting used to the commands you learn at each step.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Starting the VI Editor<\/h3>\n\n\n\n<p>VI editor lets you create new files or edit existing files. The command to start is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vi filename<\/code><\/pre>\n\n\n\n<p>Then you hit return. If the file exists, it will open for editing. Otherwise it vi will create a new file with that name.<\/p>\n\n\n\n<p>At the bottom of your screen, you will see the filename and the filesize. Something like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"filename\" 234 lines, 3685 characters<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using vi for multiple files<\/h3>\n\n\n\n<p>You can work on multiple files using vi using a single terminal. Suppose I have three files:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Experiment.php<\/li><li>Spectrum.php<\/li><li>Tag.php<\/li><\/ul>\n\n\n\n<p>To open all three files at once, I would type the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vi Experiment.php Spectrum.php Tag.php<\/code><\/pre>\n\n\n\n<p>This would open all three files show Experiment.php on my terminal. To see the files which are open, I type the following command in vi:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:ls<\/code><\/pre>\n\n\n\n<p>This would list all the open files. To open yet another file, e.g. Peak.php, I type the following vi command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:e Peak.php<\/code><\/pre>\n\n\n\n<p>To verify:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:ls<\/code><\/pre>\n\n\n\n<p>You should see the Peak.php in this list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 # \"Experiment.php\" line 154\n2 \"Spectrum.php\" line 0\n3 \"Tag.php\" line 0\n4 %a \"Peak.php\" line 1<\/code><\/pre>\n\n\n\n<p>A number is listed to the left of each file. This can be used to switch between files. To switch to Tag.php, type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:bu 3<\/code><\/pre>\n\n\n\n<p>You can also switch back to the command line without closing any of the files. To do so, type&nbsp;<em>ctrl-z<\/em>. To switch back, type&nbsp;<em>fg<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Autoindent Problem<\/h3>\n\n\n\n<p><strong>Problem<\/strong>&nbsp;When you paste code from clipboard to vi, an extra tab is added for every line as following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Line 1\n Line 2\n   Line 3\n     Line 4\n<\/pre>\n\n\n\n<p><strong>Solution<\/strong>&nbsp;1. In command mode, type the following to switch to paste mode:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set paste<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>Switch to insert mode and paste you text<\/li><li>In command mode, type the following to switch back to nopaste mode::set nopaste<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Getting rid of ^M<\/h3>\n\n\n\n<p><strong>Problem<\/strong>&nbsp;You copy pasted text from some editor and you ^M was inserted on every line. ^M is a carriage return in Microsoft Windows environment.<\/p>\n\n\n\n<p><strong>Solution<\/strong>&nbsp;Find out how you can reset your editor to unix character encoding. This way you will not continue to have this problem moving forward.<\/p>\n\n\n\n<p>Open the document in vi and type the following in command mode<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:s ctrl + shift + V ctrl + shift + M<\/code><\/pre>\n\n\n\n<p>This should appears as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:%s\/^M\/\/g&lt;\/<\/code><\/pre>\n\n\n\n<p>Hit return and the characters will be removed<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using vi for multiple files<\/h3>\n\n\n\n<p>You can work on multiple files using vi using a single terminal. Suppose I have three files:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Experiment.php<\/li><li>Spectrum.php<\/li><li>Tag.php<\/li><\/ul>\n\n\n\n<p>To open all three files at once, I would type the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ vi Experiment.php Spectrum.php Tag.php<\/code><\/pre>\n\n\n\n<p>This would open all three files show Experiment.php on my terminal. To see the files which are open, I type the following command in vi:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:ls<\/code><\/pre>\n\n\n\n<p>This would list all the open files. To open yet another file, e.g. Peak.php, I type the following vi command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:e Peak.php<\/code><\/pre>\n\n\n\n<p>To verify:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:ls<\/code><\/pre>\n\n\n\n<p>You should see the Peak.php in this list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 1 #    \"Experiment.php\"               line 154\n 2      \"Spectrum.php\"                 line 0\n 3      \"Tag.php\"                      line 0\n 4 %a   \"Peak.php\"                     line 1\n<\/code><\/pre>\n\n\n\n<p>A number is listed to the left of each file. This can be used to switch between files. To switch to Tag.php, type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:bu 3<\/code><\/pre>\n\n\n\n<p>You can also switch back to the command line without closing any of the files. To do so, type&nbsp;<strong>ctrl-z<\/strong>&nbsp;To switch back, type&nbsp;<strong>fg<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Converting tabs to spaces<\/h3>\n\n\n\n<p>Tabs often breakup when the source code is moved between different system. In certain cases, tabs are not recognized and you get ugly looking symbols. The best way to avoid this frustration is to insert spaces instead of tabs. Obviously, you wouldn&#8217;t want to hit the space tab 20 times on each line. The solution is to set the tab to insert predefined number of spaces instead of a tab. In vi, you can do this by setting expandtab as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set expandtab<\/code><\/pre>\n\n\n\n<p>To control the number of spaces, you still need to use tabstop as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set tabstop=4<\/code><\/pre>\n\n\n\n<p>To convert all existing tabs to spaces, use retab:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:retab<\/code><\/pre>\n\n\n\n<p>To set tab spaces inserted for indentation, use the shiftwidth as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:set shiftwidth=4<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The VI editor is a screen-based editor. It is very powerful and has been around for more than a decade. Programmers are especially fond of the VI editor due to its powerful features to aid programmers. VI is often overwhelming for beginners. It takes some time to get used to VI but it is often [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=161"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/161\/revisions"}],"predecessor-version":[{"id":162,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/161\/revisions\/162"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}