fresco 节省内存
Think you know vim? Guess again.
认为您知道vim吗? 继续猜。
As an experienced vim user, I really thought I knew all there was to know in vim. Sure, there are always improvements and optimizations to be made, but I thought that I already had all the tools I needed to be an efficient software developer. Vim had blown my mind with new tricks before, so I thought that there was nothing left that would surprise me in the same way.
作为经验丰富的vim用户,我真的以为我知道vim的全部知识。 当然,总是需要进行改进和优化,但是我认为我已经拥有了成为高效软件开发人员所需的所有工具。 Vim以前用新的技巧使我震惊,所以我认为没有什么可以让我感到惊讶了。
I was wrong.
我错了。
In this article, you will learn 7 exciting new things that vim can do for you.
在本文中,您将学习vim可以为您做的7件事。
For this article, I expect that you have at least a beginner’s fluency with vim, which means that simple commands such as basic movement, inserting, and searching are already understood. If you aren’t at this level yet, you can open the terminal and run:
对于本文,我希望您至少对初学者具有流利的vim知识,这意味着已经理解了诸如基本移动,插入和搜索之类的简单命令。 如果您尚未达到此级别,则可以打开终端并运行:
sudo apt-get install vimThen, you can go through the vim tutorials by running:
然后,您可以通过运行以下内容来浏览vim教程:
vimtutorI also recommend reading my previous vim article: https://medium.com/swlh/8-vim-tricks-that-will-take-you-from-beginner-to-expert-817ff4870245
我还建议阅读我以前的vim文章: https : //medium.com/swlh/8-vim-tricks-that-will-take-you-from-beginner-to-expert-817ff4870245
By default, vim commands that delete text, such as “d”, “c”, and “x”, overwrite the register. This allows you to write “dd” to perform a cut, then write “p” to perform a paste. While this functionality can be useful, a missing functionality in vim is the ability to delete text WITHOUT overwriting the register.
默认情况下,删除文本的vim命令(例如“ d”,“ c”和“ x”)会覆盖寄存器。 这使您可以写“ dd”执行剪切,然后写“ p”执行粘贴。 尽管此功能很有用,但vim中缺少的功能是可以删除文本而不会覆盖寄存器的功能。
For example, say you want to delete some text and replace it with the text currently in your register. The problem is that when you run the delete command, you lose the information in your register! This is a very common text editing scenario, so the manual use of other registers is not a good solution.
例如,假设您要删除一些文本并将其替换为您寄存器中当前的文本。 问题是,当您运行delete命令时,您将丢失寄存器中的信息! 这是一种非常常见的文本编辑方案,因此手动使用其他寄存器不是一个好的解决方案。
I found that the best way to fix this issue is the use the following in your .vimrc:
我发现解决此问题的最佳方法是在.vimrc中使用以下命令:
" Shortcut to use blackhole register by defaultnnoremap d "_dvnoremap d "_dnnoremap D "_Dvnoremap D "_Dnnoremap c "_cvnoremap c "_cnnoremap C "_Cvnoremap C "_Cnnoremap x "_xvnoremap x "_xnnoremap X "_Xvnoremap X "_X" Change <leader> to be commalet mapleader = ","let g:mapleader = ","" Shortcut to use clipboard with <leader>nnoremap <leader>d dvnoremap <leader>d dnnoremap <leader>D Dvnoremap <leader>D Dnnoremap <leader>c cvnoremap <leader>c cnnoremap <leader>C Cvnoremap <leader>C Cnnoremap <leader>x xvnoremap <leader>x xnnoremap <leader>X Xvnoremap <leader>X XThis makes the “d”, “c”, and “x” commands delete WITHOUT overwriting the register, and makes “,d”, “,c”, and “,x” commands delete WITH overwriting the register.
这将使“ d”,“ c”和“ x”命令删除而不覆盖寄存器,并使“,d”,“,c”和“,x”命令删除而覆盖寄存器。
This can fundamentally change how you edit code, as it opens up a “copy, delete, paste” workflow.
这可以从根本上改变代码的编辑方式,因为它打开了“复制,删除,粘贴”工作流。
Figure 1: In this example, I want to create a new variable PUBLISH_RATE = 1.0 / MAIN_LOOP_PERIOD_SECONDS, copy the text PUBLISH_RATE, and then replace the argument in rospy.Rate() to PUBLISH_RATE. 图1:在此示例中,我想创建一个新变量PUBLISH_RATE = 1.0 / MAIN_LOOP_PERIOD_SECONDS,复制文本PUBLISH_RATE,然后将rospy.Rate()中的参数替换为PUBLISH_RATE。Everyone who learns how to use vim knows how to use “hjkl” for small navigation steps and “gg” and “G” to jump to the top and bottom of the file. While these are both very useful, we need the ability to find a balance between these fine and coarse movement methods. To increase your navigation speed and accuracy, use the following:
学习如何使用vim的每个人都知道如何在小的导航步骤中使用“ hjkl”,并使用“ gg”和“ G”跳转到文件的顶部和底部。 尽管这两种方法都很有用,但我们需要能够在这些精细和粗略运动方法之间找到平衡。 要提高导航速度和准确性,请使用以下命令:
“SHIFT + L” (low) to jump the cursor to the bottom of the current screen without moving the screen “ SHIFT + L”(低)可将光标跳转到当前屏幕的底部,而无需移动屏幕 “SHIFT + M” (middle) to jump the cursor to the center of the current screen without moving the screen “ SHIFT + M”(中间)可将光标跳转到当前屏幕的中心,而无需移动屏幕 “SHIFT + H” (high) to jump the cursor to the top of the current screen without moving the screen “ SHIFT + H”(高)可将光标跳转到当前屏幕的顶部,而无需移动屏幕 “CTRL + U” (up) to move the cursor and screen half a page up “ CTRL + U”(向上)可移动光标并将屏幕向上移动半页 “CTRL + D” (down) to move the cursor and screen half a page down “ CTRL + D”(向下)可将光标向下移动半屏 Figure 2: Demonstration of the High, Middle, Low, Up, Down movement. 图2:演示高,中,低,上,下运动。As a software developer, it is rare that you will only be working with one file. When using vim, you can use the following command to open another file in a new tab:
作为软件开发人员,很少只使用一个文件。 使用vim时,可以使用以下命令在新选项卡中打开另一个文件:
:tabe <filename>This has tab completion to help you find the files you want, as well as history by clicking the UP arrow key.
它具有制表符补全功能,可帮助您找到所需的文件以及单击UP箭头键的历史记录。
You can then switch between tabs with:
然后,您可以使用以下方法在标签之间切换:
“gt” to move right “ gt”向右移动 “gT” to move left “ gT”向左移动 Figure 3: Demonstration of using :tabe to open files in new tabs and then navigating between tabs. 图3:演示使用:tabe在新选项卡中打开文件,然后在选项卡之间导航。This next tip blew my mind when I first found it. Say you are editing text, then open a new file “controller/left/left.py” with tip 3:
当我第一次发现它时,下一个技巧使我震惊。 假设您正在编辑文本,然后使用提示3打开一个新文件“ controller / left / left.py”:
:tabe controller/left/left.pyThen say you want to also open another file called “controller/right/right.py”. You can write “:tabe”, then press the UP arrow key to use your history. This will help you show “:tabe controller/left/left.py”. From here, you could slowly move the cursor over to left and replace it with right 2 times. That is how I did this before.
然后说您还想打开另一个名为“ controller / right / right.py”的文件。 您可以输入“:tabe”,然后按向上箭头键使用您的历史记录。 这将帮助您显示“:tabe controller / left / left.py”。 从这里开始,您可以将光标缓慢移至左侧,然后将其右移2次。 这就是我之前所做的。
Wouldn’t it be nice to do this faster? What if you could use an awesome text editor…
更快地执行此操作不是很好吗? 如果您可以使用很棒的文本编辑器怎么办...
SOLUTION: “CTRL + F” which will allow you to edit your command with vim syntax. You can then press “/left” then “N”, then “cw” then “right”, then “N, then “.”. Find word, change word, find previous word, run previous change.
解决方案:“ CTRL + F”将允许您使用vim语法编辑命令。 然后,您可以按“ / left”,然后按“ N”,再按“ cw”,再按“ right”,再按“ N,再按”。 查找单词,更改单词,查找上一个单词,运行上一个更改。
Figure 4: Demonstration of using “CTRL+F” to edit vim command mode text. In this case, I changed global_planner to wind_sensor. 图4:演示使用“ CTRL + F”编辑vim命令模式文本。 在这种情况下,我将global_planner更改为wind_sensor。A common frustrating and time-consuming task is modifying the case of words. For example, if you need to change 10 upper-case words to all be lower-case, you typically need to rewrite the 10 words manually. Luckily for us, vim has tools that allow us to capture the existing text information and make this change with ease.
常见的令人沮丧且耗时的任务是修改单词的大小写。 例如,如果您需要将10个大写单词全部更改为小写,则通常需要手动重写10个单词。 对我们来说幸运的是,vim提供了一些工具,使我们能够捕获现有的文本信息并轻松进行更改。
“~” to change the case from lower to upper or upper to lower “〜”将大小写从低到高或从上到下 “gUw” to change a word from lower case to upper case (can replace “w” with your desired motion) “ gUw”将单词从小写变为大写(可以用所需的动作替换“ w”) “guw” to change a word from upper case to lower case (can replace “w” with your desired motion) “ guw”将单词从大写字母更改为小写字母(可以用所需的动作替换“ w”) Figure 5: Demonstration of changing the case of characters. In the comment, I create a macro that changes the word’s first letter to upper-case and then moves to the next word. I use this macro to make the first letter of the next 10 words to be upper case. Then, I change the variable name from WIND_PUBLISH_PERIOD_SECONDS to wind_publish_period_seconds by holding the “~” key. 图5:更改字符大小写的演示。 在注释中,我创建了一个宏,该宏将单词的第一个字母更改为大写,然后移至下一个单词。 我使用此宏将接下来的10个单词的第一个字母变为大写。 然后,通过按住“〜”键,将变量名称从WIND_PUBLISH_PERIOD_SECONDS更改为wind_publish_period_seconds。Another frustrating and time-consuming task is modifying an enumeration. For example, if you have a comment that lists 10 steps in a function, but then you need to remove step 2, you typically need to modify the following 8 numbers manually. Luckily for us, vim has tools that allow us to capture the existing text information and make this change with ease.
另一个令人沮丧且耗时的任务是修改枚举。 例如,如果您的注释列出了功能中的10个步骤,但是随后您需要删除步骤2,则通常需要手动修改以下8个数字。 对我们来说幸运的是,vim提供了一些工具,使我们能够捕获现有的文本信息并轻松进行更改。
“CTRL + A” to increment a number “ CTRL + A”增加数字 “CTRL + X” to decrement a number “ CTRL + X”减少数字 Figure 6: In this example, I want to change the numbers 0–4 to be 1–5. Rather than changing each number manually, I create a macro that increments the current number, then moves down. I use this macro to change the next 4 numbers. 图6:在此示例中,我想将数字0–4更改为1–5。 我没有手动更改每个数字,而是创建了一个宏,该宏递增当前数字,然后向下移动。 我使用此宏来更改接下来的4个数字。In my previous tutorial, I showed how you can use your clipboard as your default register. This allows you to copy with “CTRL + C” in another program and the paste with “p” in vim, or copy with “yy” in vim and paste in another program with “CTRL + V”.
在上一教程中,我展示了如何使用剪贴板作为默认寄存器。 这样,您可以在另一个程序中使用“ CTRL + C”进行复制,在vim中使用“ p”进行粘贴,或者在vim中使用“ yy”进行复制,然后使用“ CTRL + V”进行粘贴。
However, at the time of that article, I only used Linux. When I tried to use the same vim configuration on Mac, I found that this change did not work on Mac. I was able to find the solution by updating the .vimrc with the following:
但是,在撰写本文时,我仅使用Linux。 当我尝试在Mac上使用相同的vim配置时,我发现此更改在Mac上不起作用。 我可以通过使用以下更新.vimrc来找到解决方案:
" Use clipboard as default registerif system('uname -s') == "Darwin\n" set clipboard=unnamed "OSXelse set clipboard=unnamedplus "LinuxendifEvery so often, vim shows me a powerful technique that can turn a common frustration into a simple command. These moments always make me smile. Learning these vim tricks will help you save hours of your life, which gives you more time to do what you truly love: learning more vim tricks.
vim经常向我展示一种强大的技术,可以将常见的挫败感转化为简单的命令。 这些时刻总是让我微笑。 学习这些vim技巧将帮助您节省生命,这使您有更多时间去做自己真正喜欢的事情:学习更多vim技巧。
Check out my vim configuration file at https://github.com/tylerlum/vim_configuration.
在https://github.com/tylerlum/vim_configuration中查看我的vim配置文件。
翻译自: https://levelup.gitconnected.com/7-surprising-vim-tricks-that-will-save-you-hours-b158d23fe9b7
fresco 节省内存