vim 编辑演示_改变我生活的7个Vim技巧(含演示)

vim 编辑演示

Hi, fellow coders! You may have heard of the Vim code editor before, or even used it a bit.

嗨,各位编码员! 您可能以前听说过Vim代码编辑器,甚至使用过它。

There are plenty of resources out there that cover the basics of Vim and I don't want to just do another rewrite of those here. Instead, I want to share some quick tips that I've learned from others while using Vim full time at work.

有很多资源涵盖了Vim的基础知识,我不想在这里再次重写这些内容。 相反,我想分享一些我在全职使用Vim时从他人那里学到的快速技巧。

These are tiny things that you can learn quickly that will make a big difference in your day-to-day work in Vim. They'd definitely made my life easier.

这些都是很小的事情,您可以快速学习,这将对您在Vim中的日常工作产生很大的影响。 他们肯定使我的生活更轻松。

Remember: Vim comes pre-installed on Mac and Linux. You just need to open up your terminal and type "vim" in the command prompt, and you'll open Vim. If you have a Windows computer, follow these instructions to install Vim on your PC.

切记:Vim预先安装在Mac和Linux上。 您只需要打开终端并在命令提示符下键入“ vim”,即可打开Vim。 如果您有Windows计算机,请按照以下说明在PC上安装Vim

开始之前 (Before we begin)

If you're interested in Vim but haven't started yet, these are the resources I'd like to recommend:

如果您对Vim感兴趣但还没有开始,这些是我想推荐的资源:

  • OpenVim - Interactive Vim Tutorial

    OpenVim-交互式Vim教程

  • vimtutor - this is an interactive command line tutorial that's available and installed on Macs and some Linux distros. Just type vimtutor in your terminal

    vimtutor-这是一个交互式命令行教程,可在Mac和某些Linux发行版上安装。 只需在终端中输入vimtutor

  • VimAdventures - First couple of levels are free, and if you like the format, and the full license is $25

    VimAdventures-前几个级别是免费的,如果您喜欢这种格式,则完整许可证为25美元

If you like Vim but it's too much to run it on its own, install a Vim extension for your favourite editor, like VS Code or Sublime or any other. That way you get to use the quick actions and shortcuts of Vim and a friendlier interface you're more used to.

如果您喜欢Vim,但要单独运行它实在太多了,请为您喜欢的编辑器安装Vim扩展,例如VS Code或Sublime或任何其他。 这样,您就可以使用Vim的快速操作和快捷方式以及更习惯的友好界面。

One important realization I had about learning and working with Vim: you don't need to master everything (which is practically impossible anyway) to start using it.

我对学习和使用Vim的一个重要认识是:您不需要掌握一切(实际上几乎是不可能的)就可以开始使用它。

Once you figure out the basics, every time you have a question or a block throughout your workday, write it down, and then go through that list and search the Internet for how to do that in Vim.

一旦掌握了基础知识,在工作日中每次遇到问题或遇到障碍时,请写下来,然后遍历该列表并在Internet上搜索如何在Vim中做到这一点。

This way, you will incrementally fill any gaps you might have and will become better with each new little thing you add to your repertoire (these micro-improvements will be similar to the tips in this article below).

这样,您将逐步填补可能存在的空白,并且会随着添加到曲目库中的每一个新的小东西而变得更好(这些微改进将类似于下面的本文技巧)。

Now let's proceed to the fun stuff – the tips and tricks. You don't have to have any Vim plugins installed to take advantage of these.

现在,让我们继续学习有趣的东西-技巧和窍门。 您无需安装任何Vim插件即可利用它们。

1.如何以正确的缩进量开始在一行上书写 (1. How to start writing on a line at correct indentation)

Before I learned this, I used to jump on a new line and go to insert mode, TAB to the right indentation and start typing code. With this little trick, you won't have to do do all the extra tabbing, it will just place you in insert mode at the right indentation.

在我学到这一点之前,我曾经跳到新行并进入插入模式,即TAB到正确的缩进位置并开始键入代码。 有了这个小技巧,您不必做所有额外的制表符,它只会使您处于插入模式的正确缩进位置。

RECIPE: S (SHIFT+s)

配方: S (SHIFT + s)

DEMO:

演示:

2.自动调整窗口大小 (2. Resize windows automatically)

Very often we do something with windows inside Vim that causes them to be resized incorrectly, sometimes one being way too wide and the other way too narrow.

通常,我们在Vim中使用窗口来做某些事情,导致窗口的大小不正确,有时一个窗口太宽而另一个窗口太窄。

The easiest way to see that effect is to open 3 windows in one Vim tab and resize the terminal window in which you opened Vim.

看到这种效果的最简单方法是在一个Vim选项卡中打开3个窗口,并调整打开Vim的终端窗口的大小。

You want to resize the windows to all be the same size, with available space evenly distributed. Good news is, you don't have to do it manually.

您希望将窗口的大小全部调整为相同大小,并均匀分配可用空间。 好消息是,您不必手动进行操作。

RECIPE: CTRL+w =

配方: CTRL+w =

The combination of CTRL+w, followed by pressing the equals sign key will equalize the windows.

CTRL + w的组合键,然后按等号键,将使窗口相等。

DEMO:

演示:

3.跳至匹配的括号/括号 (3. Jump to the matching bracket/brace)

With your cursor on a square [ or round ( bracket or a curly brace {, press % (SHIFT+5) to jump to its matching symbol. Press it again to jump back (toggle between them).

将光标放在方形[或圆形(括号或花括号{上,按%(SHIFT + 5))跳到其匹配的符号。再次按它跳回来(在它们之间切换)。

if (condition) {
  // code
}
// If your cursor was on {, and you pressed %, you'd jump to }

RECIPE: % with your cursor on the character you want to find a match to.

配方: % ,将光标放在要与之匹配的字符上。

DEMO:

演示:

4.缩进/取消缩进一行或多行 (4. Indent/Unindent a line or multiple lines)

>> ⁠– indents a line
<< ⁠– unindents a line

When you have multiple lines selected (in VISUAL LINE mode), you only need to press > or < once to indent or unindent the lines (as shown in demo below)

当您选择了多行时(在VISUAL LINE模式下),您只需按一次>或<就可以缩进或取消缩进(如下面的演示所示)

It doesn't matter where your cursor is positioned in the line when indenting - it will still work. After indent is done, cursor is auto-positioned on first non-empty character in the line.

缩进时光标在行中的位置无关紧要-仍然可以使用。 缩进完成后,光标将自动定位到该行中的第一个非空字符上。

RECIPE: One line: >>, <<. Multiple lines: >, <.

配方:一行: >><< 。 多行: ><

DEMO:

演示:

5.修复整个文件中的缩进 (5. Fix indentation in the whole file)

Start in the top of a file (to get there, press gg anywhere in the file.). Then press =G, and Vim will fix the indentation in the whole file. If you don't start in the beginning of the file, it will fix indentation from current line to the bottom of file.

从文件顶部开始(要到达该位置,请在文件中的任何位置按gg 。)。 然后按=G ,Vim将在整个文件中修复缩进。 如果您不从文件的开头开始,它将固定从当前行到文件底部的缩进。

RECIPE: =G

配方: =G

Press the equals sign, followed by SHIFT+G

按等号,然后按SHIFT + G

DEMO:

演示:

6.使用标签的基础 (6. Basics of working with tabs)

Often you want to be looking at multiple files or contexts at the same time. Vim tabs are very handy but underutilized feature for this. I don't know of any other editor that supports this (but I'm sure there is a way to do it elsewhere).

通常,您希望同时查看多个文件或上下文。 Vim选项卡非常方便,但是使用不足。 我不知道有任何其他编辑器支持此功能(但是我敢肯定有其他方法可以做到这一点)。

For example, I like to keep my code related files in my main tab, and in another tab: README with a TODO list and a place I can jot down further ideas.

例如,我想将与代码相关的文件保留在主选项卡中,并保留在另一个选项卡中:README(带有TODO列表的自述文件)和一个可以记下更多想法的位置。

To write the commands to work with tabs, you will need to be in command mode. To start writing the command, press : and type. The command will show up in the left bottom corner of the editor as you are typing. Press enter to execute.

要编写使用选项卡的命令,您将需要处于命令模式。 要开始编写命令,请按:并输入。 键入时,该命令将显示在编辑器的左下角。 按Enter执行。

RECIPE: :tabnew creates a new tabgt - go to next tabgT - go to previous tab:tabo - close all other tabs besides the active one

方药: :tabnew创建一个新的标签gt -进入下一选项卡gT -到前一个标签:tabo -关闭所有其他选项卡除了有源一个

DEMO:

演示:

7.如何快速返回上一个文件 (7. How to quickly go back to a previous file)

Often, when you edit a file with code, you open another one in the same window. Then it's not so easy to come back to the one you just worked on. You could list buffers and navigate to the previous one but you need to remember its name for that and spend your precious time. Vim users don't like to spend too much time on actions. :) So you can use CTRL+o for this.

通常,使用代码编辑文件时,会在同一窗口中打开另一个文件。 然后回到刚才的工作并不是那么容易。 您可以列出缓冲区并导航到上一个缓冲区,但是您需要记住该缓冲区的名称并花费宝贵的时间。 Vim用户不喜欢在动作上花费太多时间。 :)因此,您可以为此使用CTRL + o。

All it does is that it finds a previous position of your cursor - and if it happened to be in a different file (the one you just lost by opening a new one), it jumps us right back there.

它所做的只是找到光标的上一个位置-如果它碰巧在另一个文件中(您刚刚通过打开一个新文件而丢失了),它将使我们跳回到那里。

Thank you for reading and I hope these tips serve you well!🐦 If you want to follow my adventures, here's my Twitter :)

感谢您的阅读,我希望这些技巧对您有帮助!🐦如果您想跟随我的冒险经历, 这里是我的Twitter :)

💌 I write a weekly newsletter that covers topics like learning to code, changing habits, personal finance, book recommendations & key takeaways, minimalism, starting a business, psychology and more. For those of you who are interested: join 1K+ like-minded people passionate about self-improvement and learning.Subscribe here

💌我每周写一次新闻通讯,内容涵盖学习编码,改变习惯,个人理财,书籍推荐与要点,简约,创业,心理学等主题。 对于您感兴趣的人:加入1K +志趣相投的人们,他们对自我完善和学习充满热情。 在这里订阅

🌱 These days I am working on my side project - an app called "Zerno". Sign up to get early access very soon! ZERNO app

days这些天,我正在做自己的副项目-名为“ Zerno”的应用程序。 注册以很快获得抢先体验! ZERNO应用

翻译自: https://www.freecodecamp.org/news/7-vim-tips-that-changed-my-life/

vim 编辑演示

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值