109

The only thing that I miss about using other editors is finding my place with the mouse. I can look with my eyes to a specific area on the file and with a motion of the mouse and a click I am there. With VIM i have to jjjjjkkkkkkjjjjhhhh... almost there hhhhh oh crap I missed by 3 characters, lll.

I am learning to do marks and i found a bookmarks plugin that is great, I can create markers and destroy them with <f3> and navigate to them with unfortunately the marks are forgotten when the file is closed.

How do you all super navigate files? I know I could also do 5j7k4j5h and so forth, but there must be a plugin that can read my eyes and put the cursor where I really need it..

I also have learned to use /% to search for a specific term. It does help but all the matching characters are highlighted through out the document.

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
Helmut Granda
  • 4,565
  • 7
  • 35
  • 51
  • Great answers all, while we are still on the subject I figured I can experiment with something like nG (go to line Number) f % (find first character of what I am looking for, or as some one suggested use the w to move forward to the next word. – Helmut Granda Jan 05 '12 at 22:24
  • [A quick reference cheatsheet was made for vim navigation](https://www.reddit.com/r/vim/comments/kq0sbd/a_quick_reference_was_made_for_vim_navigation/) that is hotly discussed in the reddit vim group. – Raos Jan 05 '21 at 02:56
  • Use `:noh` to disable highlighting of matched patterns. – Skippy le Grand Gourou Apr 28 '21 at 07:02

11 Answers11

196

Try $ vimtutor, it will teach you everything you need to know to get started.

hjkl are the tip of the top of the iceberg and very rarely used, at least in my case.

wWEe and BbgegE all allow to move word by word:

  • w and e go forward, W and E take whitespace and punctuation into account

    " here the * marks the default location of the cursor
    " and each letter shows where you jump when you hit the key.
    
    Latin: Lorem ipsum dolor sit amet.
                       *   e   e    e
                       *   E   E     E
    *    w w     w     w     w   w   w
    *      W     W     W     W   W   W
    
  • b and ge go backward, B and gE take whitespace and punctuation into account

    Latin: Lorem ipsum dolor sit amet.
    b    b b     b     *
    B      B     B     *
         ge    ge    ge*
        ge
         gE    gE    gE*
    

fFtT are used to reach for a particular character on the current line and ;, are used to repeat that motion, in the same direction for ;and in the opposite direction for ,:

  • fm jumps ON the next m forward, F goes backward

    Latin: Lorem ipsum dolor sit amet.
    *          fm    ;            ;
               ;     Fm          *
    
  • tm jumps BEFORE the next m forward, T goes backward

    Latin: Lorem ipsum dolor sit amet.
    *         tm    ;            ;
              ;     Tm           *
    

/? are used to jump to the first occurrence of a pattern from the current cursor position:

  • /pattern goes forward

    Latin: Lorem ipsum dolor sit amet.
    *            /ips
    
  • ?pattern goes backward

    Latin: Lorem ipsum dolor sit amet.
    ?Lat             *
    

0$ are used to jump to the first and last character of the line.

    (whitespace)Latin: Lorem ipsum dolor sit amet.(whitespace)
    0                  *                                     $

^g_ are used to jump to the first and last printable character of the line.

    (whitespace)Latin: Lorem ipsum dolor sit amet.(whitespace)
                ^      *                         g_

Single and combined ()[]{} are used to move phrase by phrase or paragraph by paragraph or code block by code block.

<C-b> and <C-f> are used to scroll by screen backward and forward.

<C-u> and <C-d> are used to scroll by half-screen backward and forward.

H, M and L move the cursor to the top, middle, bottom of the viewport, respectively.

zt, zz and zb move the line under the cursor to the top, middle, bottom of the viewport, respectively.

And so on.

:help motion.txt will blow your mind.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 23
    I don't usually do this, but this comment is just to tell you that this is a great answer! – j-i-l Sep 29 '16 at 22:31
  • 2
    For some reason, I had never used H, M, or L before, great tips! – Chris May 16 '17 at 10:38
  • Adding that `]}` and `[{` (2 keystrokes) jump to the end or beginning of a scope (e.g. one defined by `{ ... }` – Matthew Woo Feb 20 '19 at 19:38
  • 3
    Well, this still doesn't really answer OP's question on `how to jump to a line he's seeing like using a mouse`. I know and use all the key bindings you mentioned all the time. But it's still not as efficient as pointing a mouse when jumping large distance. – Penghe Geng May 21 '19 at 17:53
  • 1
    yeah H,M,L are easy to rember: High,Middle,Low ;) @Chris – nils petersohn Feb 19 '20 at 12:17
26

besides vim's motion command, I find a vim plugin named EasyMotion is pretty useful to navigate, if you are familiar with vimperator or pentadactyl, EasyMotion just bring hint mode back to vim. here is a animated demo and here is the video tutorial. Hope it's helpful for you.

Vincent
  • 901
  • 7
  • 8
  • The advantage of easymotion is that you can go anywhere on screen with just 3 keystrokes. – Xavier T. Jan 06 '12 at 08:42
  • +1: EasyMotion is really a jetpack of Vim's movement. (by the way, PreciseJump too. Actually, EasyMotion is a son of PreciseJump) – Dmitry Frank Jan 06 '12 at 09:56
  • 2
    How the hell did EasyMotion's author got this super awesome status line in the demo? – Cyrille Jan 06 '12 at 10:46
  • 1
    The 'super awesome status line' is a plugin called 'Powerline'. Its here: http://www.vim.org/scripts/script.php?script_id=3881 – DEzra May 08 '12 at 15:24
  • could you please update the animated demo url? it is not working anymore – brauliobo Jun 18 '14 at 23:24
  • the animated demo url is updated. and you can find more demo animation images from https://github.com/easymotion/vim-easymotion @brauliobo – Vincent Sep 22 '16 at 11:58
  • Easymotion is a great plugin. However, it's kind of taxing to your mind. You need to enter the command, look at the highlighted portion, and enter the corresponding letter. Still not as efficient as pointing a mouse. – Penghe Geng May 21 '19 at 17:55
  • @PengheGeng for me switching between mouse and keyboard all the time is inefficient, and I prefer to get rid of mouse usage as much as possible. but if this is not a problem for you, go ahead and use mouse to navigate :) – Vincent May 23 '19 at 03:34
  • @Vincent I agree with you vim is best used by keyboard exclusively. I almost never use mouse either with vim. The point is not to suggest using the mouse as an alternative. Instead I would like to compare the efficiencies of these approaches with that of a mouse. For example, for certain moves (horizontal move within a line, or vertical move for a couple of lines),keyboard is superior to a mouse. But for long distance move across a screen, keyboard is not as efficient as a mouse with methods suggested. In that sense, "mouse efficiency" can be used for a reference: how does it compare to mouse? – Penghe Geng May 23 '19 at 13:50
23

There are a zillion ways to move around in vim, this is one of its really strong areas. I use { and } a lot, which move up and down to the next blank line in that direction. % is useful for moving back and forth to a matching bracket (of any kind). W and B move forward and back by a "word".

It might be worthwhile reviewing the Moving around section of the Vim manual.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 4
    Probably also worth mentioning is paging up and down using Ctl+U and Ctl+D, respectively. – Nick Jan 06 '12 at 01:47
12

vim has mouse support! give this a try (in your .vimrc):

set mouse=a

sidenote: as a screen user, I've found that I also need

set ttymouse=xterm2

for this to work.

rgcb
  • 1,111
  • 1
  • 11
  • 17
9

personally I prefer to use:

"/<chars><enter>" to quickly move to somewhere   ( extremely fast! trust me! )
":<number>" to go to some line, 
"ctrl + f/b" to forward/back
"g;" to move to the last editing place.
"w/b" to move by word
"jklh" to move your cursor to the exact position
Siwei
  • 19,858
  • 7
  • 75
  • 95
6

Apart from {, }, (, ), <number>cb, <number>cB, <number>cW and %, I use such navigating techniques:

  • ciw, diw (ciW, diW). etc. to quickly edit/delete word under the cursor (including non-space characters)
  • ci(, ci" (ca(, ca"), etc. to quickly edit inside parentheses, quotes (including parentheses, quotes). The same applies for d.
  • Marks with CAPITAL letters to set them globally (across files) and persistently (when buffer is closed). For instance, mA will create global mark A that will be available with 'A command (or `A to respect column).
  • gf to go quickly to file under cursor
  • f, t (F, T) to move to the char, before the char right (left) (use ; to repeat). These are particularly useful for d and c commands (use . to repeat).
  • ^] to move to the tag and ^T to move back.

And lots of others ways to move :)

Check this out for more information: http://www.thegeekstuff.com/2009/03/8-essential-vim-editor-navigation-fundamentals/

P.S. For rails users there is a very useful rails-vim plugin, you may want to check it out too.

penkovsky
  • 893
  • 11
  • 14
5

I only have this diagrams!

▼/▶ Cursor   ▽/▷ Target
┌───────────── 0      
│  ┌────────── ^      $ ──────────────┐
│  │  ┌─────── Fo     fe ────────┐    │
│  │  │┌────── To     te ───────┐│    │
│  │  ││ ┌──── ge     w ───┐    ││    │
│  │  ││ │ ┌── b      e ─┐ │    ││    │ 
│  │  ││ │ │  ┌h      l┐ │ │    ││    │
▽  ▽  ▽▽ ▽ ▽  ▽▼      ▼▽ ▽ ▽    ▽▽    ▽
   echo "A cheatsheet from quickref.me"
                 - SCREEN 1 START
   ┌─┬─────────▷ #!/usr/bin/python
   │ │     ┌───▷     
   │ │     │     print("Hello")
   │ │     { } ▶ print("Vim")
   │ │       │   print("!")
   │ │       └─▷    
   │ │ ┌───┬───▷ print("Welcome")
G gg H M L k j ▶ print("to")
│        │   └─▷ print("quickref.me")
│        │       print("/vim")
│        │     
│        └─────▷ 
│                - SCREEN 1 END
└──────────────▷ print("SCREEN 2")
Raos
  • 374
  • 5
  • 8
  • 1
    I didn't know "ge" (and thus "gE") thanks! Could add whitespace at the end to show "g_", and perhaps some capitals ("B", "W", "E"). Also "-" and "+"(or ""). Already looks handy though! – DZet Mar 10 '21 at 15:10
5

With option relativenumber (:help relativenumber - included with Vim 7.3) you can get to the line under your eyes with the a [count]k or [count]j movement, which you let you move in current line with movements like ftweb as explained by @romainl.

mMontu
  • 8,983
  • 4
  • 38
  • 53
3

this is a good place to learn the main navigation commands, some of the power/speed of vim comes from the combination of movement with action i.e. cw -> change word, d10j -> delete 10 lines down etc. Also this and this are interesting reads.

T I
  • 9,785
  • 4
  • 29
  • 51
2

You can :set nohlsearch to turn off the highlight of your search characters.

I navigate using ctags a lot too.

richo
  • 8,717
  • 3
  • 29
  • 47
  • https://github.com/richoH/dotfiles/blob/master/vimrc You might find some other useful things in there. – richo Jan 06 '12 at 00:07
  • Thanks for the tip, I was looking through your vimrc and I found your note where you have set to highlight the line when you reach 80 characters when writing emails or docs. Why would you write an email in VIM? I can understand a doc for your personal use but not sure about an email. – Helmut Granda Jan 07 '12 at 05:03
  • I use mutt + vim to read and write the vast majority of my email. I also use the it's all text plugin to write a lot of web content in vim. I have word wrapping turned on for emails but for links etc it can be useful to have a reminder if I've not formatted correctly, or if nested quotes are getting too long. – richo Jan 08 '12 at 02:41
1

I have wrote a plugin with super fast navigation in mind, it's called move-less:

https://github.com/anschnapp/move-less

It let's you search the hole file comfortable by scrolling with (h j k l-keys). You don't change your cursor position while scrolling the hole site (based on folding trick). If you abort your search you just where you started.

For super fast navigation the intension is to use my plugin to get the target position on the screen and then jump to the right position via easymotion (which already was recommended from another answer):

https://github.com/easymotion/vim-easymotion

If you combine both you could even take advance of the jump-history and you can jump between your origin and target destination simply via <c-o> and <c-i>.

snap
  • 1,598
  • 1
  • 14
  • 21