Now that we have vim all set up, it's time to learn some more command line shortcuts. The first thing we will be looking at is indentation. indentation can be done in vim by going in visual mode and typing V for selecting portions of text or uppercase V for selecting full lines, followed by greater than or less than to indent right or left after words press.to repeat the last operation, and your operation can be undone by hitting you and then can be redone by hitting control error as in Undo and Redo. This is the equivalent of control set and control shifts that in most popular editors, when in visual mode, we have the option of changing the case of letters by hitting bu, to make all text uppercase, or smaller for lowercase until the for reverting current case Other handy shortcuts are uppercase g go to end the fight.
GG good game. No, No really, except for the gamers out there. GG means go to start the file and select all. This is not really a shortcut but the combination of commands, GG, uppercase V, uppercase G, as in go to start the file, select Full line and move to the end. vim also has a handy shortcut for opening main pages for the word under the cursor. Just hit uppercase K, and the main page will show up for that specific word.
If there is one that is finding text in vim is as easy as hitting slash. Just type slash plus the text to find and hit enter to start searching. vim will go to the first occurrence of that text hit enter for next occurrence and uppercase and for previous occurrence. Our favorite editor has a powerful story. Find and Replace feature similar to the set command. Let's say we want to replace all occurrences of the string, cw D with the string d r. For this, just type the following command.
So the first part until before the s, it's a start from line one until the end of the file, then it's s that comes from substitute slash CWD slash dir slash to replace CWD with dir and small G, for global to replace all occurrences. Let's do another common example that often comes up in programming, commenting lines of code. Let's say that we want to comment out lines 10 to 20 in a shell script. To do this, just type the following command. This means substitute the beginning of the line with hash and space for deleting rows lines of text, type the following command. This will delete everything from line 30 until the end.
More information about regular expressions can be found in the chapter dedicated to regular expressions. Also check out the chapter on site. For more text manipulation examples, these commands are some of the longest in vim, and oftentimes we get them wrong. To edit the command we just wrote and run it again. We can open the command history by hitting Q, colon, and navigate to the line containing the command to edit, then press insert, update the line, press escape and enter to run the command. It's as simple as that.
Another operation that is often useful is sorting. Let's create a file with unsorted lines of text from the classic lorem ipsum text open source dot txt and Ron Collins. We see that the lines are all sorted alphabetically. Now let's move forward to Window management. vim has the option to split the screen for editing files in parallel, just right column split for horizontal split and colon split for vertical split. When vim splits the screen, it opens the same file in the other pane.
To open another file, just hit colon, ie. The nice thing here is that we have autocomplete, so we can just hit tab, and vim will start writing filenames for us. If we don't know what files we want to choose, we can just run any arbitrary shell command directly from vim, and come back once we finished. For example, when you type this command, the shell opens shows us the output of the command and wait until we hit enter to come back to the fight. When you mode, press Ctrl W to move between windows. To close a window press Collin Q.
If you want to save a file under a different name, think of the Save As command from other editors just hit colon w followed by the new file name. Say my copy dot txt. vim also has the option of opening multiple files at once. just specify a list of files after the vim command. After the Pfizer open use colon bn to move to the next file, and to close all the files hit colon QA. vim also has an explorer built in just open vim and hit colon explore.
After this we can navigate through the directory layout and open new files. It also has a diff option. Let's open a file delete one of the lines and save it under a new name. Exit and open the two files with vim diff. Now we can see the differences between them visually. This applies to all sorts of changes and is always better than the plain old diff command output.
Keep keyboard shortcuts really make a difference and opened a whole new world of possibilities when using vim. It's kind of hard to remember in the beginning, but once you start using them, it will be as simple as clicking a button