> So I guess my question is: Why is vim so amazing?

Vim visual mode is incredibly powerful. It allows you to select a
portion of text and operate on the selection. There are two visual
modes: visual line and visual block. The former allows you to select
complete lines, the latter a block of text, usually a column, but
maybe just a portion of one line.

When I am processing a long report, I like to delete lines that I
"approve" so that I am left with only the lines that need attention.
You can use "dd" (delete line) and the "." (repeat last command) but
you have a lot of sequential lines to delete, you might get ahead of
yourself and hit the "." too many times, deleting lines you have not
processed yet. I use visual line to select the lines, and once I am
happy, I delete them all in one shot. Here's how: move the cursor to
the first line you want to delete and then press SHIFT-v. The beauty
of this is that you can use some of the other navigation commands to
move around without having to commit any changes. For example, you can
press SHIFT-g to move to the end of the file (or "1 SHIFT-g" to move
to the beginning) and then continue navigating to a specific point
(i.e. you want to delete all line from the current position to
somewhere near the bottom of the file, but you're not quite sure). You
can also use "/" to search for a string and jump to that point, too.

Visual block is quite powerful, too. Let's say your text file contains
the output of "ls -l" and you want trim it to list only the first and
last column. Move to the first line of the output to the beginning of
where you want to delete and press CTRL-v. Next, navigate to the end
point so that you highlight the text you wish to delete and press 'x'.
Presto, it's gone. If you wanted to replace the highlighted text with
something else then instead of 'x', press 'c' and type in the new
text. Press ESC when you're done and vim will automatically fill in
each line with that text.

Visual mode is also handy for doing search and replaces on a small
portion of your file. We have some client-specific exports on our
filer that are mostly similar to each other. That is, the only
difference is the client name. When I need to add new exports for a
client, I use visual line mode to select another client's exports and
paste them into the file: move to first line, SHIFT-v, down arrow as
needed, and then the 'y' key to "yank" (i.e. copy to vim's clipboard)
the lines. Next, I move to where I want to paste those lines and press
the 'p' key. I select the newly pasted text using visual line mode and
perform a search and replace: ":s/foo/bar/g".

By the way, if you want to exit visual mode without performing any
actions, just hit ESC.

This is just the tip of the iceberg with respect to visual mode.

Bonus tip: some additional handy deletion commands that do not require
visual mode (you know exactly what you want to delete):

dG - delete from cursor to end of file
d1G - delete from cursor to beginning of file
dw - delete from cursor to end of the word
dE - delete from cursor to end of the line


Chris
_______________________________________________
mlug mailing list
[email protected]
https://listes.koumbit.net/cgi-bin/mailman/listinfo/mlug-listserv.mlug.ca

Reply via email to