开发者

Preferred way to move around in vim (normal mode)

开发者 https://www.devze.com 2023-04-11 01:18 出处:网络
I haven\'t seen this asked on stackoverflow, and this is my biggest pain point in vim: How do you all navigate within a file?I found myself using the hjkl too much, or too repetitively, and I want to

I haven't seen this asked on stackoverflow, and this is my biggest pain point in vim:

How do you all navigate within a file? I found myself using the hjkl too much, or too repetitively, and I want to get better at this. This is frustrating when you're on a large monitor.

I installed EasyMotion - and so far it's been good for me - I jus开发者_如何学编程t want to know if there's something better...

Thanks!


I like the cheatsheet of Ted Naleid. It's like a reticle so you can easily find the horizontal and vertical movements. Put it on a wall next to your monitor and you will soon pick up new movements on the fly.

The movements that I liked recently are:

  • () and {} which let you hop function wise in source code
  • / and ? + n/N just search, you normally know where you want to go
  • fx and tx - to jump to or before the next character x of course you can do a 2fx to jump to the second occurrence of x, like you can do with all movements
  • % to move between starting and ending parenthesis


I use b and w to move left and right respectively on a single line. For up and down, I use Ctrl+u and Ctrl+d respectively. IMO Ctrl+u and Ctrl+d are better than Ctrl+b and Ctrl+f because they scroll half window at a time so that you don't loose context.

I haven't really used any plugin for moving around in vim so far.

Forgot to mention two other important keystrokes, $ and ^ to move to end of line and start of line respectively.


Several move commands:

b B e E f F ge gE gj gk go G h H j k l L M n N t T w W { } / ? ^ $ # * ` ' | % 

Learn them, plus all commands starting with [ like [{ which is very useful when editing C-style code…

See :help index.txt for reference.


Mostly I use the following (in order of frequency):

  • 'R go to marked position (the ` is too off the baseline keyboard to use much)
  • /search|?search forward|backward search
  • n|N next|previous in search
  • H|L|M top|bottom|middle of display
  • G go to end of file
  • 1G go to line 1
  • { go backward a 'paragraph' (often a code block)
  • } go forward one 'paragraph'

Most all of these can be augmented with a count before the command.


It depends on how you want to move around, but generally,

  • A puts you in insert mode at the end of a line
  • I at the beginning
  • o inserts a line below
  • O above

and more powerfully, searching with /<thing you want to jump to> is very handy. In a c file where the functions are formatted

int
funcname()

/^funcname will jump you to the start of the function. There's a bunch more, but this shold be a good start for someone new to vim.


Simple documentation:
http://vim.wikia.com/wiki/Moving_around

Regular movement:
hjkl/arrow keys/page up/page down
% will switch between open/ending braces
gg/G move to top/bottom

Folding:
For collapsing large blocks of code, you can use folding.
http://vimdoc.sourceforge.net/htmldoc/fold.html

Search:
To jump to something in particular type /searchstring (use with set inc for jumping to matches while typing)
* to search forward for the same word the cursor is on
# same but search backward

You can also use marks.
http://vim.wikia.com/wiki/Using_marks

I also use ctags and jumping to find stuff across multiple files.
http://vimdoc.sourceforge.net/htmldoc/tagsrch.html

I've never needed anything else.


I don't really see much to add in terms of general enlightenment but I use (ranked by how often I use them):

w and b

to move by one word to the right and to the left.

/ and ?

to search for a word or pattern to the bottom or to the top.

G and gg

to jump to the bottom and the top of the buffer.

<C-f> and <C-b>

to jump to the next and previous screen.

* and #

to jump to next and previous occurence of the word under the cursor.

f and F

to jump before a character to the right or to the left.

t and T

to jump on a character to the right or to the left.

Ho! and

$ and ^

a lot, too, to jump to the end and the beginning of a line.


Read http://www.viemu.com/a-why-vi-vim.html and run vimtutor, also :help motion.txt will be usefull. I recommend also staying in normal mode all the time - as described in article above. Generally, learning vim is learning piano - you have to practice much.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号