I just discovered the existence of markers in vi. How do you use it, what do you know about them? are they useful, say for a C++ developer?
I use them when I need to jump around in a large file. For example, if I'm working on two interrelated functions, one which is defined near the top of the file and one which is defined near the bottom, I can set markers to quickly jump back and forth between the two locations.
If I'm declaring a class or working with a declaration I'm not familiar with, it's often helpful to mark the spot where things are first explained so that I can jump back for a quick reference.
Markers are useful in general, but I don't think they're any more (or less) useful just becuase you're developing in C++.
These are only some ideas -- I'm sure there will be many other good ones out there.
The most common use is for copy-paste or deleting large blocks.
Move to the first line of the block, type mx
Move to the last line of the block, type y'x
to copy the whole block (to the clipboard), or d'x
to delete (cut) it. In either case, p
or P
can be used to paste it elsewhere.
Markers act as a good way to get back to the beginning of a line or an exact location in a line.
I'll typically use them when I want to extract or copy a portion of code. Consider the following:
int tmp = 0;
while (tmp < 10)
{
doIt(tmp); /* cursor before d */
/* ... */
finishIt(tmp);
tmp++
}
If I want to extract from doIt(tmp)
to finishIt(tmp)
, I'd then set a marker at the cursor (ma
for example), navigate to finishIt, and then delete to mark a with d'a
.
I use them all the time for:
- commenting out blocks of code,
- copying and moving blocks of code,
- yanking and deleting blocks of code into named buffers, and
- Edit: substituting in a block of test.
Commenting out:
- go to the first line of the code you want to comment out,
- mark it, e.g. enter
ma
- go to the end of the block
- enter
:'a,.s/^/#
(or whatever comment character you need)
Copying and moving:
- mark first line as above,
- go to bottom of block you want to copy/move
- enter your second different marker, e.g.
mb
- go to where you want to copy the block and enter
:'a,'bco .
or:'a,'bmo .
to copy or move resp.
Yanking to a named buffer:
- mark first line as above,
- go to bottom of block you want to yank
- enter
:'a,.ya a
will yank the block into buffer a or:'a,.ya A
will append the block onto the current contents of buffer a
Edit: Substituting in a block of text:
- mark first line as above,
- go to the bottom of the block you want to substitute in
- enter
:'a,.s/search_string/replace_string/[gc]
which will subtitute in your text block. Adding 'g' or 'c' after the last slash will invoke the usual global and confirm functionality.
Edit: Forgot to say, remember that 'a
(apostrophe a) refers to the line containing the marker and `a
(backtick a) refers to the character on the line that you marked.
So \
(bactic-a-d-backtic-b) is a useful little snippet to delete the text in a line from the char marked with 'a' up to the char before the char marked with b.ad\
b
By the way, in Vim, entering :reg
will give you the contents of all your registers incl. your delete registers.