I use Vim every day and have been doing so for about 2 years now. I’m nowhere near as efficient as I should be, but the speed at which I can now edit files in comparison to my previous text editor (TextMate, naturally) is pretty astounding to me.
One feature that I use a lot, especially with Python files, is marks.
How to make your life easier
While I’m writing my Python, the number one naviation action I take is when adding imports; moving from the portion of code I’m writing up to the top of the file and back again. Until I found marks, the process was:
- Remember my current line number
- Use C-b to page all the way back through the file
- Add my import at the top
- Forget my previous line number
- Search the file for my previous position
Clearly this is a function of my poor short-term memory, but even if I remembered the line number correctly, that’s still a lot of steps; My thought was “there must be an easier way”.
There is.
Now when I open any Python file, the first thing I do is hit m then i. This sets a mark assigned to i at the top of the file, where i stands for “imports”. Now, when I realise I need to add or remove imports my flow is this:
- Hit m then a. This assigns my current line to the mark at a
- Hit ’ then i. This moves me to the mark at i (i.e. the top of the file)
- Do the import thing
- Hit ’ then a. This jumps me back to my previous position
This seems like the same amount of work, but since there’s no reliance on short-term memory to remember your line number you can learn these keystrokes so they’re assigned to muscle memory; It ends up being a lot quicker.
This trick isn’t just good for Python but for any file where you need to jump around to specific locations regularly.
Despite Vim’s tenure on my system I still feel like I haven’t begun to scratch the surface with this editor. Let me know in the comments if you’ve found any other ways to use marks.