Mastering Vim: Motions and marks

This time I’d like to talk about some some useful tips how to move through a file. Vim motions and marks cannot only be helpful to jump to a specific location, but also to select, copy-paste and delete text passages.

Basic Vim motions

Of course, there are some simply and basic motions, such as:

  • 0 jumps to the first character in line
  • ^ jumps to the first non-blank character in line (nice for indent text)
  • $ jumps to the last character in line
  • gg jumps to the first line in the buffer
  • G jumps to the last line in the buffer
  • :42 or 42gg jumps to line# 42

Of course these are some basic motions and you might already know them very well 😉 But there’s more!

What you see

Something I’ve learned in the past and what I’m using regularly today, is the use of the predefined “marks” of the window. In my opinion, these three normal command keys are really useful if applied correctly:

  • H jumps to the top of the window
  • M jumps to the middle of the window
  • L jumps to the bottom of the window

Now that’s nice, but only used for jumping it’s not very powerful. The nice thing about motions is, you can combine them – and this is where the magic happens.
Let’s say you want to cut some bits from a file and you don’t know if it’s 40, 41 or even 42 lines. If you’re a newbie, you might press dd (or . afterwards) several times until you’ve deleted all the lines in question. That might work for deleting a couple of lines, but isn’t very sexy. What about cutting the whole block so you can paste it later?

All of this works much better if you…

  • Align the last line of the block on the end of the window
  • Put the cursor on the first line of the block
  • Press dL

Our fancy little Vim now cuts the text from your current cursor position until the end of the window.

When I wrote “align the last line of the block on the end of the window“, then you can use your cursor keys to move to the bottom of the window to scroll down. Unfortunately, that takes a lot of time because suddenly you’ve one line too much, so you’ve to scroll a line up again, means moving the cursor all through the window to the top again. To avoid those cursor movements, you can use the following keystrokes:

  • Ctrl-d scroll down half a screen (default)
  • Ctrl-u scroll up half a screen (default)
  • Ctrl-e scroll one line down (extra line)
  • Ctrl-y scroll one line up

With those keystrokes in mind, you can quickly align the last line on the end of the window and then go for the L thingy.

Custom marks

If there’s no visual Vim available, then marks become one of my best friends in Vim, especially when I’ve to code a lot in a crappy enterprise environment without proper editor.

As mentioned above, there are some predefined marks such as the start & end of the file or the window positions. But did you know you can define your own marks? Defining own marks and jumping to them is quite simple:

  • m{a-zA-Z} Set mark {a-zA-Z} at cursor position
  • ‘{a-z} Jump to the mark {a-z} in the buffer
  • ‘{A-Z} Jump to the mark {A-Z} in the file where it was set
  • :marks List all marks

And again, just defining and jumping to marks isn’t very fancy. However, you can combine them again to do awesome stuff like:

  • d’a Delete lines from current cursor position to mark a
  • >’a Indent lines from current cursor position to mark a

Did you ever wanted to jump between two positions in a single file, for example to compare something? Easy, just use  and you’ll jump to your last position in file.

82 Comments

  • geometry dash Reply

    In my opinion, these three normal command keys are really useful if applied correctly.

  • bathroom remodel Reply

    I think it is very complimentary to navigation and helps out a lot!

  • shell shockers Reply

    It looks very good, I always have to ask, thank you for your information

  • Manifestation Magic Reply

    I Personally Like Your Post; You Have Shared Good Insights And Experiences. Keep It Up.

  • Window Washing Cincinnati Reply

    A bit tricky to memorize at first but very useful! Thanks a lot for this. 🙂

  • tidewater breaking news Reply

    I love that you have shared this to help others achieve this too. Love it!

  • [url=https://www.tidewaternews.com/write-for-us-marketing-submit-a-guest-post/]tidewater breaking news[/url] Reply

    I love that you have shared this to help others achieve this too. Love it! 🙂

  • criminal case Reply

    I’m also glad to be give tjos command lists such as this.

  • run 3 Reply

    The information is very special, I will have to follow you, the information you bring is very real, reflecting correctly and objectively, it is very useful for society to grow together.

  • sam database Reply

    Wow! This is great info, I’ve been searching for this and glad I see this post. Thanks for the share.

  • Massage Reply

    Great information. Thanks for sharing this, it’s a useful one.

  • in-home massage Reply

    I believe it has a great information and motion of works.

  • fortcollinsstumpgrinding.com Reply

    Thank you for sharing this Dominique. I highly appreciate the tips for these motions. Cheers from the best stump removal company in Fort Collins, Colorado.

  • mesquiteconcreteservice.com Reply

    Motions and marks have always been a struggle for me. Thank you for sharing this blog post. It helped me a lot. Best regards from the best concrete services in Mesquite, Texas.

  • 24hourtowtruckqueens Reply

    whats the key you guys use the most? I’m always using the copy paste key mostly.

  • desktop themes Reply

    I recently needed to edit the apache configuration file and I used vi. At the very beginning, I could not understand how to enter the input mode, and then save the changes. Thanks to your site, your tips really helped me.

  • Website Reply

    Your work is really inspiring us. In fact, your creativity abilities were amazing.

  • Website Reply

    Your work is really inspiring us.

  • Your tutorials are great and very helpful. I have a carpet cleaning business and please feel free to give us a call if you need our help.

  • article writer Reply

    This makes one who has adopted aware of what lies ahead in his adoption challenges as this mixed races one is viewed as minority group.

  • epoxy concrete uses Reply

    I never thought that I can make use of your tutorial. Thank you for sharing.

  • Click here Reply

    The motion commands can be used after an operator command, to have the command
    operate on the text that was moved over. That is the text between the cursor
    position before and after the motion. Operators are generally used to delete
    or change text.

  • treeservicehuntington.com Reply

    Devoted to motion marks and how the case law has developed so far and how it can give useful guidance for future applicants. Thanks for sharing this blog.

  • concrete foundation Reply

    Easy to follow instructions. This is somehow useful just in case you’re mouse pointer or hardware doesn’t work.

  • auroraillandscaping.com Reply

    Our qualified team of landscape architects is prepared to handle any of your toughest requests. They are prepared to assist with every aspect of your landscaping: planning, maintenance, and installation.

  • Robert Reply

    Progressive aims to optimise your office space for success. We’ve built our reputation on delivering remarkable, tailored corporate furniture solutions to business from a variety of different industries.

  • melbourne office fitouts Reply

    Our approach to an office fitout in Melbourne is that of a key turn solution. Our team focuses on you. We understand that every project is unique and that a business needs to continue operating whilst a commercial fitout is being undertaken. Our simple Key Turn Solution provides for a total services package where Progressive looks after the entire scope of the project.

  • Roof Replacement Reply

    Your blog is very nice. Wish to see much more like this. Thanks for sharing your information

  • Tree Removal Reply

    Fabulous, what a weblog it is! This blog provides valuable information to us, keep it up.

  • Visit us Reply

    Vim was just released last 2 years and now it is getting stable as time goes by

  • Preventative Maintenance Reply

    This is helpful especially if you have an enormous amount of files that you stored on your computer. The navigation would be easy. But if you need any question about your A/C or HVAC, just visit our site.

  • Helpful information. Lucky me I found your site by accident, and I’m stunned why this accident didn’t took place earlier! I bookmarked it.

  • gyms in tacoma wa Reply

    Loved the content here! I learned a lot from this article. Thanks!

  • Residential Dumpster Reply

    I know what to do, thank you! Actually this Blog post helped me a lot. I hope you continue writing about this kind of entry.

  • Outdoor Living Reply

    I learn something more challenging on your different blogs everyday. Thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.