What are the most-used vim commands/keypresses?

前端 未结 10 934
长发绾君心
长发绾君心 2020-12-22 14:22

I\'m a Ruby programming trying to switch from Textmate to MacVim, and I\'m having trouble wading through the gargantuan lists of things you can do in VIM and all of the keyp

相关标签:
10条回答
  • 2020-12-22 14:46

    Here's a tip sheet I wrote up once, with the commands I actually use regularly:

    References

    • vim documentation online
    • advanced vim tips
    • more useful tips and graphical cheat sheet

    General

    • Nearly all commands can be preceded by a number for a repeat count. eg. 5dd delete 5 lines
    • <Esc> gets you out of any mode and back to command mode
    • Commands preceded by : are executed on the command line at the bottom of the screen
    • :help help with any command

    Navigation

    • Cursor movement: ←hjk l→
    • By words:
      • w next word (by punctuation); W next word (by spaces)
      • b back word (by punctuation); B back word (by spaces)
      • e end word (by punctuation); E end word (by spaces)
    • By line:
      • 0 start of line; ^ first non-whitespace
      • $ end of line
    • By paragraph:
      • { previous blank line; } next blank line
    • By file:
      • gg start of file; G end of file
      • 123G go to specific line number
    • By marker:
      • mx set mark x; 'x go to mark x
      • '. go to position of last edit
      • ' ' go back to last point before jump
    • Scrolling:
      • ^F forward full screen; ^B backward full screen
      • ^D down half screen; ^U up half screen
      • ^E scroll one line up; ^Y scroll one line down
      • zz centre cursor line

    Editing

    • u undo; ^R redo
    • . repeat last editing command

    Inserting

    All insertion commands are terminated with <Esc> to return to command mode.

    • i insert text at cursor; I insert text at start of line
    • a append text after cursor; A append text after end of line
    • o open new line below; O open new line above

    Changing

    • r replace single character; R replace multiple characters
    • s change single character
    • cw change word; C change to end of line; cc change whole line
    • c<motion> changes text in the direction of the motion
    • ci( change inside parentheses (see text object selection for more examples)

    Deleting

    • x delete char
    • dw delete word; D delete to end of line; dd delete whole line
    • d<motion> deletes in the direction of the motion

    Cut and paste

    • yy copy line into paste buffer; dd cut line into paste buffer
    • p paste buffer below cursor line; P paste buffer above cursor line
    • xp swap two characters (x to delete one character, then p to put it back after the cursor position)

    Blocks

    • v visual block stream; V visual block line; ^V visual block column
      • most motion commands extend the block to the new cursor position
      • o moves the cursor to the other end of the block
    • d or x cut block into paste buffer
    • y copy block into paste buffer
    • > indent block; < unindent block
    • gv reselect last visual block

    Global

    • :%s/foo/bar/g substitute all occurrences of "foo" to "bar"
      • % is a range that indicates every line in the file
      • /g is a flag that changes all occurrences on a line instead of just the first one

    Searching

    • / search forward; ? search backward
    • * search forward for word under cursor; # search backward for word under cursor
    • n next match in same direction; N next match in opposite direction
    • fx forward to next character x; Fx backward to previous character x
    • ; move again to same character in same direction; , move again to same character in opposite direction

    Files

    • :w write file to disk
    • :w name write file to disk as name
    • ZZ write file to disk and quit
    • :n edit a new file; :n! edit a new file without saving current changes
    • :q quit editing a file; :q! quit editing without saving changes
    • :e edit same file again (if changed outside vim)
    • :e . directory explorer

    Windows

    • ^Wn new window
    • ^Wj down to next window; ^Wk up to previous window
    • ^W_ maximise current window; ^W= make all windows equal size
    • ^W+ increase window size; ^W- decrease window size

    Source Navigation

    • % jump to matching parenthesis/bracket/brace, or language block if language module loaded
    • gd go to definition of local symbol under cursor; ^O return to previous position
    • ^] jump to definition of global symbol (requires tags file); ^T return to previous position (arbitrary stack of positions maintained)
    • ^N (in insert mode) automatic word completion

    Show local changes

    Vim has some features that make it easy to highlight lines that have been changed from a base version in source control. I have created a small vim script that makes this easy: http://github.com/ghewgill/vim-scmdiff

    0 讨论(0)
  • 2020-12-22 14:53

    http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

    This is the greatest thing ever for learning VIM.

    0 讨论(0)
  • 2020-12-22 14:53

    Here is a great cheat sheet for vim:

    enter image description here

    0 讨论(0)
  • 2020-12-22 14:54

    I can't imagine everyone uses all 20 different keypresses to navigate text, 10 or so keys to start adding text, and 18 ways to visually select an inner block. Or do you!?

    I do.

    In theory, once I have that and start becoming as proficient in VIM as I am in Textmate, then I can start learning the thousands of other VIM commands that will make me more efficient.

    That's the right way to do it. Start with basic commands and then pick up ones that improve your productivity. I like following this blog for tips on how to improve my productivity with vim.

    0 讨论(0)
  • 2020-12-22 14:54

    tuxfiles.org holds a pretty good cheat sheet. I think there are a couple of points to learning the commands:

    • Read the cheat sheet regularly. Don't worry about using all of them, or remembering all the keys, just know that the command exists. Look up the command and use it when you find yourself doing something repetitive.
    • If you find yourself doing something regularly (like deleting an entire line after a particular character d$), go a quick google search to see if you can find a command for it.
    • Write down commands you think you'll find useful and keep that list where you can see it while you're writing your code. I would argue against printing something out and instead encourage you to use post it notes for just a few commands at a time.
    • If possible, watch other programmers use vim, and ask them what commands they are using as you see them do something interesting.

    Besides these tips, there are some basic concepts you should understand.

    • vim will use the same character to represent the same function. For example, to delete a line after a character use d$. To highlight a line after a particular character use v$. So notice that $ indicates you will be doing something to the end of the line from where your cursor currently is.
    • u is undo, and ctrl+r is redo.
    • putting a number in front of a command will execute it repeatedly. 3dd will delete the line your cursor is on and the two lines that follow, similarly 3yy will copy the line your cursor is on and the two lines that follow.
    • understand how to navigate through the buffers use :ls to list the buffers, and :bn, :bp to cycle through them.
    • read through the tutorial found in :help This is probably the best way to 'learn the ropes', and the rest of the commands you will learn through usage.
    0 讨论(0)
  • 2020-12-22 14:59

    What most people do is start out with the bare basics, like maybe i, yw, yy, and p. You can continue to use arrow keys to move around, selecting text with the mouse, using the menus, etc. Then when something is slowing you down, you look up the faster way to do it, and gradually add more and more commands. You might learn one new command per day for a while, then it will trickle to one per week. You'll feel fairly productive in a month. After a year you will have a pretty solid repertoire, and after 2-3 years you won't even consciously think what your fingers are typing, and it will look weird if you have to spell it out for someone. I learned vi in 1993 and still pick up 2 or 3 new commands a year.

    0 讨论(0)
提交回复
热议问题