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.

111 Comments

  • geometry dash

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

  • shell shockers

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

  • Manifestation Magic

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

  • Window Washing Cincinnati

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

  • tidewater breaking news

    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]

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

  • criminal case

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

  • run 3

    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

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

  • Massage

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

  • in-home massage

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

  • change of address usps

    Glad I came here! because I got your article so informative. Thanks!

  • Best Spy cameras

    We are providing best ever camera products you will like

  • bee consulting

    thanks for posting. This thread is helpful

  • fortcollinsstumpgrinding.com

    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.

  • flatbed towing

    Impressive post! Thanks for the share.

  • Thank you for sharing this with everyone. Best regards from the best deck and fence installation in Castle Rock, Colorado.

  • mesquiteconcreteservice.com

    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

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

  • desktop themes

    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.

  • septic services

    Great post! Thanks for sharing this useful information.

  • Website

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

  • Website

    Your work is really inspiring us.

  • mica cosmetics

    Thanks for always sharing a great post.

  • cedarparkconcrete.org/

    I am studying motions and marks for my client. This article is very helpful.

  • You are always teaching us new ways to learn and I love it. Please also contact us for deck building services in Longmont, Colorado.

  • westminstercarpetcleaning.org/

    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

    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

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

  • Infringement Attorney

    I’d love to write about this. If I do, could I reference your work?

  • Click here

    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

    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.

  • maralvarolian.com/

    Thanks for sharing this relevant information, such a big help.

  • concrete foundation

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

  • auroraillandscaping.com

    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

    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

    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.

  • Handyman Niagara

    Fantastic article-very well written piece.

  • Roof Replacement

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

  • Tree Removal

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

  • Visit us

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

  • Preventative Maintenance

    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

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

  • Residential Dumpster

    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.

  • Balayage

    Great content!

  • Driveway Oklahoma

    Awesome information you’ve posted here, great one.

  • Outdoor Living

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

  • carpenter seattle

    Fantastic article. It’s a very well-written piece.

  • Bismarck Cleaning

    I enjoyed over read your post. It has nice information, I got good ideas from this post.

  • Buttendz Hockey Grips

    Buttendz is committed to providing the best grips in the world, designed for competition & training at any level. Players from all over the world are looking for ways to improve their equipment and gain an edge.

  • drywall replacement

    Really helpful and quite fantastic! Good work on this article. It’s worth reading.

  • Cleanlinebuilders.com

    I’m definitely going to bookmark your blog, I just love your post, thanks for such a nice sharing..Hope to get some info on your blog in future

  • Appliance Fixer

    Thanks for the Vim motions. Anyone who needs Vim motions should be here. Cheers.

  • masonry buffalo

    masonry buffalo is a top-class Masonry and concrete construction company in Buffalo, New York. We are the trusted hands for all forms of Masonry installation projects, repairs, and routine masonry maintenance for both new and old residential and commercial buildings in Buffalo and surrounding areas.

  • essaywriter

    Uniqueness is priceless in the modern world when everything is copied and imitated. We don’t want you to pay extra for a plagiarism report like other companies. We don’t charge for this tool because you have to be 100% confident about our cooperation.

  • Stockton Drywall

    Thank you for sharing this post. This is a great help.

  • Bean

    This is amazing

  • kitchen remodeling

    Wow! Thank you very much for providing us a wonderful drives content!

  • Furnace Repair

    Really enjoyed reading this thanks

  • Website

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

  • masonrykanata.com

    I’ve been visiting your blog for a while now and I always find a gem in your new posts.

  • beautiful landscaping

    This blog will be amazing in the eyes of the visitors it is because of the useful information here…Thank you sharing.

  • Wayne

    It was helpful

  • oregonizedaccounts.com

    Thanks for this nice effort which you put here in the shape of this post.

  • masonrydavis.com

    I already know vim but read this because I wanna hear you talking about it.

  • Sports Stretching

    Short but informative. Thank you for sharing your thoughts with all your readers. Anyway, I have something to share with you also mu website is all about stretching and the importance of it to our body.

  • Manifestation Reports

    Nice effort. Kudos! Been helping my readers with their manifestation, I hope it can help your readers also. Visit my website for more.