{"id":142,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:25:59","modified_gmt":"2020-08-22T20:25:59","slug":"this-is-the-page-title-toplevel-42","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=142","title":{"rendered":"Command Output in vi"},"content":{"rendered":"\n<title>Command Output in vi<\/title>\n<concept id=\"\" description=\"A 'shell escape' from 'vi' enables you to insert the output of commands into your file.\" \/>\n<question id=\"\" type=\"mc\" text=\"What key combination would you use to read an existing file in 'vi'?\" \/>\n<p>\nIt often happens that we want to insert the output of <glossary>Linux<\/glossary>\ncommands into the file we are editing. The sledgehammer approach is to run the command and redirect it to a file, then edit that file. However, if that file containing the commands output already exists, we never have to leave <command>vi<\/command>. Instead, we can use the <keyinput>:r<\/keyinput> from\n<command>ex<\/command>-mode to read it directly into our current file. But, what if it doesn&#8217;t yet exist?\nFor example, I often want the date in text\nfiles as a log of when I input things. This can be done with a combination of the <keyinput>:r<\/keyinput> (for read) from ex-mode and a <i>shell-escape<\/i>.\n<\/p>\n<concept id=\"\" description=\"A shell-escape is when we start from one program and jump out of it (escape) to a shell.\" \/>\n<concept id=\"\" description=\"When you do a 'shell escape' (e.g in vi) the original program is still in memory.\" \/>\n<question id=\"\" type=\"tf\" text=\"When you do a 'shell escape' (e.g in vi) the original program is still in memory.\" \/>\n<question id=\"\" type=\"tf\" text=\"When you do a 'shell escape' the original program is no longer in memory.\" \/>\n<question id=\"\" type=\"mc\" text=\"What is a 'shell escape'?\" \/>\n<p>\nA shell-escape is when we start from one program and jump out of it (escape) to a <glossary>shell<\/glossary>.\nOur original program is still running, but we are now working in a shell that is a\n<glossary>child process<\/glossary> of that program.\n<\/p>\n<concept id=\"\" description=\"A shell escape in 'vi' is done by pressing ':!' followed by the command you want to run.\" \/>\n<question id=\"\" type=\"mc\" text=\"What keys would you use to do a 'shell escape' in 'vi' to execute an external command?\" \/>\n<p>\nTo do a <glossary>shell-escape<\/glossary>, we need to be in <command>ex<\/command>-mode. Next, press the exclamation mark (!) followed by the command. For example, to see what time it is, type <keyinput>:!date<\/keyinput>. We then get the date at the bottom of the\nscreen with the message to press any key to continue. Note that this did <b>not<\/b> change our original text. It just showed us the output of the <command>date<\/command> command.\n<\/p>\n<concept id=\"\" description=\"To read the output of a command directly into your file in 'vi' you would use the key combination ':r!'.\" \/>\n<question id=\"\" type=\"mc\" text=\"What key combination would you use to ead the output of a command directly into your file in 'vi'?\" \/>\n<p>\nTo read in a command&#8217;s output, we need to include the :r command, as in :r!date. Now, the output of <command>date<\/command> is read into the file (that is, it is <i>inserted<\/i> into the file). We could also have the output replace the current line by pressing ! twice, as in <keyinput>!!date<\/keyinput>. Note that we are actually brought down to the last line on the screen, where there is a single !.\n<\/p>\n<p>\nIf we want, we can also read in other commands. What is happening is that <command>vi<\/command> is seeing the output of the command as a file. Remember that :r &lt;file_name&gt; will read a file into the one we are editing. Why not read from the output of a file? With pipes and <glossary>redirection<\/glossary>,\nboth <glossary>stdin<\/glossary>\nand <glossary>stdout<\/glossary> can be files.\n<\/p>\n<concept id=\"\" description=\"You can use a shell-escape in 'vi' to read the output of a command.\" \/>\n<concept id=\"\" description=\"You can use a shell-escape in 'vi' to process all or some of the lines in your current file.\" \/>\n<question id=\"\" type=\"tf\" text=\"You can use a shell-escape in 'vi' to read the contents of a file, but nott the output of a command.\" \/>\n<p>\nWe can also take this one step further. Imagine that we are editing a file containing a long list. We know that\nmany lines are duplicated and we also want the list sorted. We could do <keyinput>:{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}!sort<\/keyinput>, which, if we remember from our earlier discussion, is a special symbol meaning all the lines in the file. These are then sent through the command on the other side of the !. Now we can type\n<\/p>\n<p>\n<keyinput>:{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}!uniq <\/keyinput>\n<\/p>\n<p>\nto remove all the duplicate lines.\n<\/p>\n<concept id=\"\" description=\"When doing a 'shell escape' from 'vi' you can combine commands with pipes just like from the shell.\" \/>\n<question id=\"\" type=\"tf\" text=\"When doing a 'shell escape' from 'vi' you can combine commands with pipes just like from a 'real' shell.\" \/>\n<p>\nRemember that this is a shell-escape. From the <glossary>shell<\/glossary>,\n we can combine multiple commands using pipes. We can do it here as well. So to save time, we could enter\n<\/p>\n<p>\n<keyinput>:{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}!sort | uniq<\/keyinput>\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What shell-escape command in 'vi' might you use to sort only lines 33 to 69 in the current file?\" \/>\n<question id=\"\" type=\"\" text=\"What shell-escape command in 'vi' might you use to replace the contents of line 16 to 46 with the contents an external file?\" \/>\n<question id=\"495\" text=\"In vi, what would you input to sort lines 42 to 58?\" \/>\n<p>\nwhich would sort all the lines and remove all duplicate lines. If we only wanted to sort a set of lines, we\ncould do it like this\n<\/p>\n<p>\n<keyinput>:45,112!sort<\/keyinput>\n<\/p>\n<p>\nwhich would sort lines 45 through 112. We can take this one step further by either writing lines 45-112 to\na new file with <keyinput>:45,112w<\/keyinput> file_name or reading in a whole file to replace lines 45-112 with :45,112r file_name.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Command Output in vi It often happens that we want to insert the output of Linux commands into the file we are editing. The sledgehammer approach is to run the command and redirect it to a file, then edit that &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=142\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-142","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=142"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/142\/revisions"}],"predecessor-version":[{"id":523,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/142\/revisions\/523"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}