Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use markers in vi?

Tags:

c++

c

vim

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?

like image 440
vehomzzz Avatar asked Oct 01 '09 17:10

vehomzzz


3 Answers

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 `ad`b (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.

By the way, in Vim, entering :reg will give you the contents of all your registers incl. your delete registers.

like image 59
Rob Wells Avatar answered Oct 12 '22 01:10

Rob Wells


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.

like image 33
Andrew Song Avatar answered Oct 12 '22 01:10

Andrew Song


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.

like image 34
Erich Kitzmueller Avatar answered Oct 12 '22 02:10

Erich Kitzmueller