VIM 超快速导航

我唯一怀念使用其他编辑器的地方就是用鼠标找到自己的位置。我可以用我的眼睛看到文件上的特定区域,通过鼠标的一个动作和一个点击,我就在那里。有了 VIM,我必须 Jjjjkkkkkkkjjjjhhh... 几乎在那里啊啊啊啊哦糟糕,我错过了3个字符,三。

我正在学习做标记,我发现了一个书签插件,这是伟大的,我可以创建标记和销毁他们与 <f3>和导航到他们与不幸的标记是遗忘时,文件关闭。

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..

我还学会了使用/% 来搜索特定的术语。它确实有帮助,但是所有匹配的字符都在整个文档中突出显示。

49761 次浏览

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.

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.

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.

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

I navigate using ctags a lot too.

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.

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.

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.

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.

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

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>.

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")