Putting these all together gives us this vim command which opens vim, makes all 3 of the changes, then saves and quits. This will open up vim and immediately do a string replacement to replace 'Nunc' with 'ABC' on line 28: vim a -c '28s/Nunc/ABC/'ĭon't forget that saving and quitting vim is an Ex command too! Therefore, you can do this to open up vim and exit it immediately: vim a -c 'wq' You can also do the same thing with string replacement Ex commands. Similarly, you can use this command to open up vim and them immediately copy or 'transfer' lines 4 to 7 to after line 15 using this command: vim a -c '4,7t15' For example, if you want to open up the file 'a' in vim and immediately delete lines 10-20, you can use this command: vim a -c '10,20d' The cool thing is, you can actually run any 'Ex' command automatically as soon as you open up vim from the command-line by using the '-c' flag. What you probably didn't know is that those ':' commands are actually called 'Ex' commands. If you've used vim before, you've most likely typed 'Esc' followed by ':' more than a few times. Vim As An 'ed' CommandĬomparing 'vim' with 'ed' is where things should really start to make sense. It's not always the case that every 'sed' command will work exactly the same as an 'ed' command like this, but by now you should hopefully see the similarities. In my case, the above command shows no differences. Now that we've edited the file 'lorem' using two different methods, let's diff the two resulting files '1' and '2' to see if there are any differences: vim -d 1 2 With the 'ed' command version, we're basically just saying "Open the file 'lorem' with 'ed', run the replacement rule on all lines in the file to replace 'ss' with 'tt', then print out every line of the file, then exit". In the case of 'sed', the string replacement will run on every line and then immediately output the result. One difference in the command above is that we need to also echo a newline followed by another 'ed' command to actually print out the current contents of the file from inside the 'ed' editor. Here is a grep command that will search for all lines that contain a sequence of two 's' characters: grep "s\/tt/g'"\n"'1,$p' | ed lorem > 1 However, this also works with most BRE-like regular expressions (the default regular expression type with grep). Since the last example was just a simple string search, you might assume the similarity is just a coincidence. Grep (With Regular Expressions) As An 'ed' Command The only minor difference is the character count that 'ed' prints out to stderr, which could be separately piped to /dev/null to ignore it: echo "g/que/p" | ed lorem 2>/dev/null We now have a way to produce output that is effectively as what we get from grep, but with 'ed'. The above command will open up 'ed' on the file 'lorem', search through the files and print the matching lines, and then exit back to the shell prompt for us. Since we only wanted to use 'ed' for its ability to search for lines with matching text, we can make this process a bit faster by just echoing an 'ed' command and piping it directly into 'ed' like this: echo "g/que/p" | ed lorem We're now done working with 'ed' at this point, so we can type the 'q' command to exit, or just press Ctrl d. Will cause 'ed' to find all lines that contain 'que' and print them out, followed by returning to the 'ed' command prompt. Typing the following 'ed' command: g/que/p This will bring you to the command prompt of 'ed' where it will wait until you type a command that's specific to the 'ed' editor. You can do that with grep using this command: grep "que" loremīut, you could also do this by opening up the file 'lorem' with the 'ed' editor: ed lorem Let's say that we want to find all lines in the file 'lorem' that contain the sequence of letters 'que'. This article will attempt to explain the origins of these tools by showing how they use a number of syntax features that are identical to an equivalent command that uses the ancient 'ed' editor which was written in the late 1960s.įor the purposes of demonstration, some example text (included at the end of this article) is contained in a file named 'lorem' which will be referenced by the commands found later in this article. Vim, sed and grep are three very popular command-line tools for Linux platforms. Can You Use 'ed' As A Drop-in Replacement For vim, grep
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |