最常用的 vim 命令/按键是什么?

发布于 2024-10-26 00:39:26 字数 421 浏览 5 评论 0原文

我是一名 Ruby 编程人员,试图从 Textmate 切换到 MacVim,但我在遍历 VIM 中可以执行的操作的庞大列表以及它们的所有按键时遇到了麻烦。我厌倦了听到“你可以使用‘I’插入文本,或‘a’在字符后附加文本,或‘A’在行尾附加文本,或者......”我无法想象每个人都使用 20 个不同的按键来导航文本,使用 10 个左右的按键来开始添加文本,以及 18 种方式来直观地选择内部块。还是你呢!?

我理想的备忘单是每个人每天用来编写代码的 30-40 个最常用的按键或命令,以及 Ruby 开发者每天使用的绝对必要的插件和 10 个最常用的命令。理论上,一旦我掌握了这些命令并开始像使用 Textmate 一样精通 VIM,那么我就可以开始学习数千个其他 VIM 命令,这将使我更多高效的。

或者,我学习 VIM 的方式完全错误吗?

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 keypresses for them. I'm tired of hearing "You can use 'I' for inserting text, or 'a' for appending text after the character, or 'A' for appending text at the end of the line, or…" 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!?

My ideal cheat sheet would be the 30-40 most-used keypresses or commands that everyone uses for writing code on a daily basis, along with the absolute essential plugins that rubyists use daily and the 10 most-used commands for them. 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.

Or, am I learning VIM the wrong way altogether?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

帝王念 2024-11-02 00:39:26

这是我曾经写过的提示表,其中包含我实际经常使用的命令:

参考文献

概述

  • 几乎所有命令都可以在前面加上一个用于重复计数的数字。例如。 5dd 删除 5 行
  • 让您退出任何模式并返回命令模式
  • 前面的命令: 在屏幕底部的命令行上执行
  • :help 任何命令的帮助

导航

  • 光标移动:←hj ↑<强>k l→
  • 用词:
    • w 下一个单词(按标点符号); W下一个单词(按空格)
    • b 背单词(按标点符号); B背单词(按空格)
    • e 结束词(按标点符号); E结尾词(按空格)
  • 按行:
    • 0 行首; ^ 第一个非空白
    • $行尾
  • 按段落:
    • { 上一个空行; }下一个空行
  • 按文件:
    • gg 文件开头; G 文件结尾
    • 123G转到特定线路号
  • 按标记:
    • mx 设置标记x'x 转到标记x
    • '.转到上次编辑的位置
    • ' '返回跳转前的最后一点
  • 滚动:
    • ^F 前进全屏; ^B向后全屏
    • ^D 下半屏; ^U上半屏
    • ^E向上滚动一行; ^Y向下滚动一行
    • zz中心光标线

编辑

  • u撤消; ^R 重做
  • . 重复上一个编辑命令

插入

所有插入命令均以 终止以返回到命令模式。

  • i 在光标处插入文本; I 在行首插入文本
  • a 在光标后附加文本; A 在行尾后附加文本
  • o 在下面打开新行; O 在上面打开新行

更改

  • r 替换单个字符; R 替换多个字符
  • s 更改单个字符
  • cw 更改单词; C 更改为行尾; cc 更改整行
  • c更改移动方向上的文本
  • ci( 更改括号内(更多示例请参见 文本对象选择

删除

  • x< /strong> 删除字符
  • dw 删除单词 D 删除到行尾 dd 删除整行
  • d<; Motion> 沿运动方向删除 将

yy 复制行剪切并粘贴

  • 到粘贴缓冲区 dd 将行剪切到粘贴缓冲区
  • p 在光标行下方粘贴缓冲区; P 在光标行上方粘贴缓冲区
  • xp 交换两个字符(x 删除一个字符,然后p将其放回光标位置之后)

  • v视觉块流;V视觉块线; ^V视觉块列
    • 大多数运动命令都会将块扩展到新的光标位置
    • o 将光标移动到块的另一端
  • dx将块剪切到粘贴缓冲区
  • y 将块复制到粘贴缓冲区
  • > 缩进块; < 取消缩进块
  • gv 重新选择最后一个可视块

全局

搜索

  • / 向前搜索; ?向后搜索
  • *向前搜索光标下的单词; #向后搜索光标下的单词
  • n同一方向的下一个匹配项; N 下一个相反方向的匹配
  • fx 前进到下一个字符xFx 向后移动到前一个字符 x
  • ; 再次以相同方向移动到同一字符; , 再次向相反方向移动到相同字符

文件

  • :w 将文件写入磁盘
  • :w 名称 将文件写入disk as name
  • ZZ 将文件写入磁盘并退出
  • :n 编辑新文件; :n! 编辑新文件而不保存当前更改
  • :q 退出编辑文件; :q! 退出编辑而不保存更改
  • :e 再次编辑同一文件(如果在 vim 外部更改)
  • :e . 目录资源管理器

Windows

  • ^Wn 新窗口
  • ^Wj 向下到下一个窗口; ^Wk 回到上一个窗口
  • ^W_ 最大化当前窗口; ^W= 使所有窗口大小相等
  • ^W+ 增加窗口大小; ^W- 减小窗口大小

源导航

  • % 跳转到匹配的圆括号/方括号/大括号,或语言块(如果加载了语言模块)
  • gd 转到定义光标下的局部符号; ^O 返回上一个位置
  • ^] 跳转到全局符号的定义(需要 tags 文件); ^T 返回到上一个位置(保留任意位置堆栈)
  • ^N (在插入模式下)自动单词完成

显示本地更改

Vim 有一些功能可以轻松突出显示已从源代码管理中的基本版本更改的行。我创建了一个小 vim 脚本,使这变得简单: http://github.com/ghewgill/vim-scmdiff

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

References

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

薄荷梦 2024-11-02 00:39:26

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

This is the greatest thing ever for learning VIM.

嘦怹 2024-11-02 00:39:26

这是一个很棒的 vim 备忘单

在此处输入图像描述

Here is a great cheat sheet for vim:

enter image description here

累赘 2024-11-02 00:39:26

您学过 Vim 的内置教程吗?如果没有,请转到命令行并输入 vimtutor。这是学习初始命令的好方法。

Vim 具有令人难以置信的灵活性和强大功能,如果您像大多数 vim 用户一样,您将学习很多新命令并忘记旧命令,然后重新学习它们。内置的帮助很好,值得定期浏览以学习新东西。

互联网上有一些很好的 vim 常见问题解答和备忘单。我建议搜索 vim + faqvim + cheatsheetCheat-Sheets.org#vim 是一个很好的来源,Vim 提示 wiki

Have you run through Vim's built-in tutorial? If not, drop to the command-line and type vimtutor. It's a great way to learn the initial commands.

Vim has an incredible amount of flexibility and power and, if you're like most vim users, you'll learn a lot of new commands and forget old ones, then relearn them. The built-in help is good and worthy of periodic browsing to learn new stuff.

There are several good FAQs and cheatsheets for vim on the internet. I'd recommend searching for vim + faq and vim + cheatsheet. Cheat-Sheets.org#vim is a good source, as is Vim Tips wiki.

笨笨の傻瓜 2024-11-02 00:39:26

tuxfiles.org 拥有一份非常好的备忘单。我认为学习这些命令有几个要点:

  • 定期阅读备忘单。不必担心使用所有这些键,或记住所有键,只需知道该命令存在即可。当您发现自己在做重复的事情时,查找该命令并使用它。
  • 如果您发现自己经常做某事(例如删除特定字符 d$ 之后的整行),请快速进行 google 搜索,看看是否可以找到相应的命令。
  • 写下您认为有用的命令,并将该列表保留在您编写代码时可以看到的地方。我反对打印一些东西,而是鼓励您一次只使用便签纸来执行几个命令。
  • 如果可能的话,观察其他程序员使用 vim,当你看到他们做一些有趣的事情时,询问他们正在使用什么命令。

除了这些提示之外,您还应该了解一些基本概念。

  • vim 将使用相同的字符来表示相同的功能。例如,要删除字符后的一行,请使用 d$。要突出显示特定字符​​后的一行,请使用 v$。因此,请注意 $ 表示您将从光标当前所在位置开始执行某些操作,直至行尾。
  • u 是撤消,ctrl+r 是重做。
  • 在命令前面加上数字将重复执行该命令。 3dd 将删除光标所在的行及其后面的两行,类似地 3yy 将复制光标所在的行及其后面的两行。
  • 了解如何在缓冲区中导航,使用 :ls 列出缓冲区,并使用 :bn:bp 循环浏览缓冲区。
  • 通读:help中的教程 这可能是“熟悉诀窍”的最佳方式,其余命令您将通过使用来学习。

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.
╰つ倒转 2024-11-02 00:39:26

大多数人所做的都是从最基本的开始,例如 iywyyp 。您可以继续使用箭头键四处移动,用鼠标选择文本,使用菜单等。然后,当某些事情拖慢您的速度时,您会寻找更快的方法来完成它,并逐渐添加越来越多的命令。您可能会在一段时间内每天学习一个新命令,然后逐渐增加到每周一个。一个月后你会感觉相当有成效。一年后,你就会拥有相当扎实的曲目,两三年后,你甚至不会有意识地思考你的手指在打字什么,如果你必须为别人拼写出来,那会看起来很奇怪。我在 1993 年学习了 vi,每年仍然学习 2 到 3 个新命令。

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.

北方的韩爷 2024-11-02 00:39:26

@Greg Hewgill 的备忘单非常好。几个月前我开始从 TextMate 转向。现在我的工作效率就像使用 TM 时一样,并且不断对 Vim 的强大功能感到惊讶。

这是我是如何切换的。也许它对你有用。

Grosso modo,我认为进行彻底的转变不是一个好主意。 Vim 非常不同,最好循序渐进。

为了回答你的子问题,是的,我每天都使用 iaIAoO 的所有内容来进入插入模式。一开始这确实看起来很奇怪,但过了一段时间你就不会真正考虑它了。

一些命令对于任何编程相关任务都非常有用:

  • rR 用于替换字符
  • < /code> 增加和减少数字
  • cit 更改 HTML 标记的内容及其变体(catditdatci( 等)
  • (映射到 ,,)用于全能补全
  • 视觉效果使用 进行块选择
  • 等等...

一旦你习惯了 Vim 的方式,就很难不敲 ox在其他编辑器或文本字段中编辑文本时始终如此。

@Greg Hewgill's cheatsheet is very good. I started my switch from TextMate a few months ago. Now I'm as productive as I was with TM and constantly amazed by Vim's power.

Here is how I switched. Maybe it can be useful to you.

Grosso modo, I don't think it's a good idea to do a radical switch. Vim is very different and it's best to go progressively.

And to answer your subquestion, yes, I use all of iaIAoO everyday to enter insert mode. It certainly seems weird at first but you don't really think about it after a while.

Some commands incredibly useful for any programming related tasks:

  • r and R to replace characters
  • <C-a> and <C-x>to increase and decrease numbers
  • cit to change the content of an HTML tag, and its variants (cat, dit, dat, ci(, etc.)
  • <C-x><C-o> (mapped to ,,) for omnicompletion
  • visual block selection with <C-v>
  • and so on…

Once you are accustomed to the Vim way it becomes really hard to not hit o or x all the time when editing text in some other editor or textfield.

﹂绝世的画 2024-11-02 00:39:26

我无法想象每个人都使用所有 20 个不同的按键来导航文本,使用 10 个左右的按键来开始添加文本,以及 18 种方式来直观地选择内部块。或者你也是!?

我愿意。

理论上,一旦我掌握了这一点并开始像使用 Textmate 一样精通 VIM,那么我就可以开始学习数千个其他 VIM 命令,这将使我更加高效。

这是正确的做法。从基本命令开始,然后选择可以提高工作效率的命令。我喜欢关注此博客,获取有关如何使用 vim 提高工作效率的提示。

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.

情栀口红 2024-11-02 00:39:26

转至 使用 vim 进行高效编辑 并了解入门所需的内容。并非该页面上的所有内容都是必不可少的,因此请挑选您想要的内容。

从那里开始,使用 vim 完成所有事情。 “hjkl”、“y”和“p”会让你走得很远,即使这不是最有效的方法。当你遇到一项任务,而你不知道有效(或根本不知道)完成它的秘诀时,并且你发现自己已经做了多次,那就去查一下。渐渐地,它将成为第二天性。

很多年前我就发现 vim 令人望而生畏(当时它的末尾还没有“m”),但只需要大约一周的稳定使用就可以提高效率。我仍然发现它是完成工作最快的编辑器。

Go to Efficient Editing with vim and learn what you need to get started. Not everything on that page is essential starting off, so cherry pick what you want.

From there, use vim for everything. "hjkl", "y", and "p" will get you a long way, even if it's not the most efficient way. When you come up against a task for which you don't know the magic key to do it efficiently (or at all), and you find yourself doing it more than a few times, go look it up. Little by little it will become second nature.

I found vim daunting many moons ago (back when it didn't have the "m" on the end), but it only took about a week of steady use to get efficient. I still find it the quickest editor in which to get stuff done.

2024-11-02 00:39:26

将其放入 .bashrc 中,以在最后编辑的行中使用上次编辑的文件打开 vim

alias vil="vim  +\"'\"0"

Put this in your .bashrc to open vim with last edited file at last edited line

alias vil="vim  +\"'\"0"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文