Note that I don't use Visual Studio, and know little about the available features in it. The following are examples of what I find useful in Vim, not a list of missing features in Visual Studio.
Macros
It's easy to create macros for complex (but repetitive) operations. To illustrate with a simple example, let's say we start with:
Line1
Line2
Line3
Line4
Line5
Now we want to envelop each line in a print("");
statement.
Place the cursor on the first line, and enter:
qx
to start recording a macro to the register x
- Shift+I
print("
Esc to insert text at the beginning of the line
- Shift+A
");
Esc to append text at the end of the line
j
to go down one line
q
to stop recording the macro
4@x
to execute the macro in register x
4 times
See :help complex-repeat
for more info on Vim macros.
Text objects
Note that this is one of the improvements Vim has over the traditional Vi. If it doesn't work, you're probably running in Vi compatibility mode; use :set nocompatible
to enable the full functionality of Vim.
Text objects allow you to easily select regions of text. Let's say we start with the following text, and place the cursor on some text:
<b><i>some text</i></b>
Now we want to delete everything between <i>
and </i>
. This can be done by simply typing the command dit
(d'elete i'nner t'ag)! Or if we want to include the tags themselves in our selection, use dat
(d'elete a t'ag). To delete everything inside the <b>
tags, use d2it
(d'elete two i'nner t'ags).
You can similarly use daw
(delete a word), dap
(delete a paragraph), di"
(delete inside double-quotes), etc; see :help text-objects
for the complete list.
Another useful example of text objects:
v2ap"+y
v
toggles visual mode. This makes it easier to see what you're selecting, and lets you adjust your selection with a series of multiple motions before you execute a command.
2ap
selects this paragraph and the next one
"+
selects the system clipboard as register for the next operation
y
yanks the selection to the given register
In other words, that command would copy two paragraphs from your text to the system clipboard (e.g. for pasting them here at StackOverflow).
Global editing
The global
command is used to apply an Ex command to all lines matching a given regular expression. Examples:
:global/test/print
or :g/test/p
would print all lines containing the phrase test
:global/test/delete
or :g/test/d
would delete said lines
:global/test/substitute/^/#/
or :g/test/s/^/#/
would search for lines containing the phrase test, and comment them out by substituting the regexp anchor ^
(beginning-of-line) with the symbol #
.
You can also do some cool stuff by passing the search motions /pattern
or ?pattern
as ranges:
:?test?move .
searches backwards for a line containing test, and moves it to your current position in the file
:/test/copy .
searches forwards for a line containing test, and copies it to the current position in the file
Good luck and have fun learning Vim!