vim / position markers

vim / position markers

  • Written by
    Walter Doekes
  • Published on

Did you ever wonder what the '<,'> characters mean when you CTRL-V visual block select text in vim?

For example: you press CTRL-V and select a bit of text. Then type : (colon). Instead of just the colon, you see: :'<,'>. You append s/^/#/ hit enter. As requested, the selected block is now “commented out”.

That’s a nice feature, but why the funny characters? In order to understand that, we remind you of the % (percent sign) that we use to select the entire file.

Examples:
:%s/[[:blank:]]\+$// to remove all trailing blanks
:%!sort to sort the entire file (you can do this on a CTRL-V selection too)

The percent sign defines the special range everything. The odd '<,'> combination defines a range between two markers. Instead of operating on the whole file, vim operates on a range. When you expanded the CTRL-V selection, you moved the markers to absolute positions in the file. You can now jump to those positions in command mode using '< and '>. If the markers are at lines 5 and 9, the range expression would be as if you had written 5,9.

It gets better. You get custom markers: 26 of them to be exact. Which you can place at will using m[LETTER]. You jump to those lines using '[LETTER].

Is that useful? Yes. Apart from keeping different editing locations in memory, it can come in handy for large ranges.

It happens now and then that I have to look through a large diff, and I only want to keep certain portions of it. What I do now, is place a marker using ma and start scrolling. First when I encounter something I want to keep, I press mb above it. Next: :'a,'bd and voilà the unneeded stuff is deleted (with d). Scroll to the first unneeded bits, press ma again and repeat.


Back to overview Newer post: apt / cherry-pick upgrades / dependencies Older post: vim / reformat textwidth 72