![]() More proficiency in vi translates to more efficiency and productivity. Getting pass the initial hump is really the hardest part. It's hard to believe this is only the tip of iceberg of things that can be accomplished in vi. Most modern systems actually use vim and the binary is either renamed or aliased to vi. NOTE: Visual mode is actually a feature of vim and not original vi. The combination of using visual mode and commands is the most practical and best way to comment and uncomment code. Navigate and block off the //s in lines 11-17. Hit CTRL + v to enter visual block mode again. Hit SHIFT + i (capital i) to enter insert mode. Use j to navigate and block off the beginning of lines 11-17. We first start off by navigating down the beginning of the top row we're interested in. Visual block mode allows us to visually block off a section of the editor to perform actions on it. There is another mode in vi that can be utilized for our task at hand. Phew! That was a lot to cover! This is definitely a very powerful technique, but like many commands involving regular expressions, it can look very ugly. This literally means we are substituting escaped // with nothing, and therefore removing it. This time our first argument is the escaped version of // and the second value is left out. ![]() Hit : to enter escape mode and enter 11,17s/\/\///. We put it all together to get 11,17s/^/\/\//. ![]() Escaping these characters tells the command that we literally want to use / twice and not to interpret it as a separator for the command. What about //? Won't vi get confused since substitution uses / to separate the old value from the new value? This is where we need to use \ (backslash) to escape the double back slashes that represent commenting. So s/^/dog/ would add the word dog to the beginning of each line. Well "nothing" is really the beginning of each line which is represented with ^ (the caret character). We are trying to substitute nothing with //. Now you may have noticed a little problem. S/cat/dog/ takes the first occurrence of the word cat and replaces it with the word dog. The next two arguments are separated by /, the first being the location or old value and the second being the new value. Substitution with regular expressions is a powerful tool in vi.ġ1,17 is the range you are interested in. It takes time to even get to this point of sufficiency, but there are better ways. Hit xx to remove the first two characters of the line (in our case the // chars), hit h to navigate up to the next row, and then repeat until all rows are uncommented.īeing able to navigate around and insert and remove characters like this is definitely not something to be scoffed at. Navigate your cursor the beginning of the last row you want to comment. To uncomment the rows, you do almost the same thing. You hit i to go into insert mode, enter // to comment, hit ESC to go back to command mode, hit j to navigate down to the next row, and then repeat until all the rows are commented out. You start your editor, navigate down to the beginning row of the block you want to comment out. It may be a little unfair to call this the "bad" way, but it's certainly the most inefficient. Have you ever found yourself in vi wanting to comment and uncomment blocks of code? Here are 3 ways to accomplish this. This is where things get real fun and adding more commands to your vi toolbox will make you even more productive. There will come a point where you will be comfortable navigating around using the h j k and l keys and going between command mode and insert mode will be second nature. ![]() The learning curve is a bit high and there are many ways to do many things in this simple yet powerful editor. Learning vi for the first time can be really intimidating.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |