VIM or the Vi Improved text editor is one of the most advanced text editors available. Let’s learn the top 20 best Linux Vim commands that you must know to gain amazing levels of productivity.
Remember, the VIM text editor has a pretty steep learning curve. But once you put in the time to get used to the commands, you will attain unmatched productivity in your day-to-day tasks that require working with text (perfect text-editor for programmers!)
Linux Vim Commands – Color schemes
Linux Vim命令–配色方案
Very geeky isn’t it? Let’s understand the editor and the commands in it.
是很讨厌吗? 让我们了解编辑器及其中的命令。
用于基本导航的Linux Vim命令 (Linux Vim Commands for Basic Navigation)
Before we begin, let’s go over how we can navigate within the VIM editor. VIM is a keyboard focused editor so you have to start getting used to not touching the mouse for basic navigation tasks.
Move backward and forward by one word at a time – cursor at the start of the word
<Line Number>G
Jump to a specified line number
Ctrl+u Ctrl+d
Similar to page up/down key while the cursor stays on-screen.
e
Move forward by one word – cursor at the end of the word
# *
Find the word within the document that your cursor is currently at.
n N
Repeats the last find command
按键
行动
方向键 j
基本光标移动正常
b w
一次向前和向后移动一个单词–在单词开头的光标
<行号>G
跳转到指定的行号
Ctrl + u Ctrl + d
类似于光标停留在屏幕上时的向上/向下翻页键。
向前移动一个单词-光标位于单词的末尾
# *
在文档中找到光标当前所在的词。
重复最后一个查找命令
掌握Linux Vim的主要命令和功能 (Geek Out with the Top Linux Vim Commands and Features)
The VIM editor is not only flexible and comprehensive but also very intuitive and “smart” when it comes to understanding the code that’s written.
在理解所编写的代码时,VIM编辑器不仅灵活,全面,而且非常直观和“智能”。
But if you don’t mind spending your time moving the mouse cursor around pointing and clicking at things, there’s no reason for you to consider VIM. This text editor is for programmers and geeks who really love to go all out, ignore the mouse for good, and do everything using the keyboard.
The thing with VIM is, if it can be done with a mouse in other text editors, it can be done with just the keyboard in VIM.
VIM的好处是,如果可以在其他文本编辑器中用鼠标完成操作,则可以仅在VIM中使用键盘完成操作。
1.在Vim中处理多个文件 (1. Work With Multiple Files in Vim)
Most of us programmers are used to working with multiple files at the same time. Our GUI text editors give us tabs that allow switching between files while all stay open simultaneously. Even though there are a few keyboard shortcuts to play around with those tabs, it’s nowhere compared to what VIM can do.
:tabedit <tab key to scroll through files in directory>
Tabedit Vim Commands
Tabedit Vim命令
The best part? You don’t touch the mouse whatsoever. How do you move around the files? With gt or gT to switch tabs forward or backward respectively. Also :tabfirst and :tablast will let you jump back and forth from the first to the last tabs that are currently open.
But if you want to open all the files directly at the start, just type:
但是,如果要在开始时直接打开所有文件,只需键入:
vim -p source_file1.py source_file2.py source_file3.py
2.在VIM中保存并打开Tab会话 (2. Save and Open Tab Sessions in VIM )
Alright, so now you have opened a ton of files in tabs and also moved across made changes to the files. But did you notice that you have to always open the files every time you want to have them in the tabs?
What if there was a way to save the open files as they are and just move with it?
如果有一种方法可以保存打开的文件原样并随其移动呢?
That’s exactly what VIM sessions are for. When you work with multiple files in VIM, it’s better to save a VIM session so the next time you open the session file, everything will come back up exactly as you left it.
How do you create a VIM session? You first move out of the insert mode by pressing the Esc key. Then enter the below command.
如何创建VIM会话? 您首先通过按Esc键退出插入模式。 然后输入以下命令。
:mksession <Session Name>.vim
Now the next time when you want to open the session, you can directly open it from the terminal by typing:
现在,下一次要打开会话时,可以通过以下方式从终端直接打开它:
vim -S <Session Name>.vim
Or if you want to open a session file while inside VIM, just type:
或者,如果您想在VIM中打开会话文件,只需键入:
:source <Session Name>.vim
3.在VIM中自动完成文本 (3. AutoComplete Text in VIM)
Feel that VIM cannot compare with the autocomplete features of other IDEs or text editors? Well, give it a shot. Even though it doesn’t use Artificial Intelligence or any sort of machine learning algorithms to back this feature, it’s robust enough.
When you’re in your VIM editor trying to edit some code, press the below key combination to check it’s superb auto-completion.
当您在VIM编辑器中尝试编辑一些代码时,请按以下组合键检查其自动完成功能。
Ctrl+n
Vim Ctrl N
Vim Ctrl N
Based on the letters typed, the editor finds the closest match for the word you might type. Though it doesn’t understand anything of what you type, it does the job really well.
4. Linux VIM命令执行括号匹配 (4. Linux VIM Commands to Perform Brace Matching)
As we previously mentioned that the VIM editor searches for patterns within the text, it brings with it brace matching while you’re coding. It makes things very easy for us when we’re working with long codes.
This one is pretty straight forward. Take your cursor to the opening/closing brace and press the % sign on the keyboard.
这很简单。 将光标移至打开/关闭括号,然后按键盘上的%符号。
And just like that, your cursor will jump to the matching brace.
这样,您的光标将跳至匹配的括号。
5.在VIM中标记并折叠代码块 (5. Mark and Fold Code Blocks in VIM)
This isn’t as straightforward as you’d want it to be because the code editor does not understand what we are typing but only works on pattern matching.
这并不像您希望的那样简单明了,因为代码编辑器不了解我们正在输入的内容,而仅适用于模式匹配。
So how do we go about folding code inside vim editor? We start with bringing the cursor to where we want to begin the folding and from there we use the letter V to begin marking the code until where we want it to fold.
Once we have marked the code with the letter V , then presszfto fold the code. The f in the zf stands for fold. how do we open the Code we just press by highlighting the first line of the folded code
To open the code now, we can bring our cursor to the line where the folded code is (as you can see in the above screenshot, the grey colored line which says 6 lines: function1(). Once your cursor is on that line, press zo where o stands for open.
But the above method is good in cases where the code is small. What if you want to jump through a few hundred lines of code? Let me show you another marking technique that will cut your time in half.
Go to the line where you want to start the folding of the code 转到您要开始折叠代码的行
Press the letter m followed by any other letter to name the marking. For example mk 按letter m再按其他字母以命名标记。 例如mk
Highlight any of the braces within the code and press % to jump directly to the matching close brace. 突出显示代码中的任何大括号,然后按%直接跳到匹配的右括号。
Press zf'k 按zf'k
The 'k signals the fold command to look for the “k” mark which we pointed out by typing out mk at the start.
'k表示fold命令寻找“ k”标记,我们通过在开头输入mk指出了该标记。
6.在不离开VIM的情况下浏览系统中的文件 (6. Explore Files in Your System, Without Leaving VIM)
A regular GUI text/code editor person would feel that they miss the “Project folder” column that usually is provided on the left sidebar of the code editor window. Can VIM replicate that?
With the two commands :Vexplore and :Sexplore, you can split the window into two sections and browse through the entire file system. Have a look at both the commands.
As you can see, in both cases, the file manager is opened and you can navigate through the files without losing your focus from the code file that you were working on.
Since we’ve already seen the split-screen capabilities of VIM, how do we split the screen in two and edit two codes?
既然我们已经看到了VIM的分屏功能,那么如何将屏幕分为两部分并编辑两个代码?
7.拆分VIM代码窗口 (7. Split VIM Code Window )
You can either Vertically or Horizontally split the VIM window and edit multiple code files without moving away from the other. How? Let’s have a look.
您可以垂直或水平拆分VIM窗口并编辑多个代码文件,而又不会相互偏离。 怎么样? 我们来看一下。
The Vexplore and Sexplore commands allow us to browse through files and open any file in the split section of the terminal. But if you just want to open the file, you can use the vsplit or split command.
VERTICAL SPLIT
:vsplit <filename>
OR
:vsp <filename>
HORIZONTAL SPLIT
:split <filename>
OR
:sp <filename>
The above command will split the terminal vertically and open the other file for you.
上面的命令将垂直拆分终端并为您打开另一个文件。
Vim Split Windows
Vim分割视窗
Command
Action
Ctrl-w ← OR Ctrl-w h
Focus on the left adjacent split
Ctrl-w → OR Ctrl-w l
Focus on the right adjacent split
Ctrl-w ↓ OR Ctrl-w j
Focus on the bottom adjacent split
Ctrl-w ↑ OR Ctrl-w k
Focus on the top adjacent split
Ctrl-w n+ OR Ctrl-w n–
Increase or decrease the size of the split by n lines
命令
行动
Ctrl-w← 要么 Ctrl-W
专注于左侧相邻的拆分
Ctrl-w→ 要么 Ctrl-w l
专注于右侧相邻分割
Ctrl-w↓ 要么 Ctrl-w j
专注于底部相邻的拆分
Ctrl-w↑ 要么 Ctrl-w k
专注于顶部相邻的拆分
Ctrl-W n + 要么 Ctrl-w n –
增加或减少n行分割的大小
If you’re working through an SSH client software like Terminus to connect to your Linux server, using the default VIM commands to switch splits will simply close the connection.
The difference between the mapping commands is the map modes they work in. Covering the map modes is out of scope for this topic so I’ll leave it to you to read. Go ahead and type in :help map-modes.
The above mappings will change the default split-window switching commands to Ctrl-J, Ctrl-K and so on instead of the added Ctrl-W.
上面的映射会将默认的拆分窗口切换命令更改为Ctrl-J,Ctrl-K等,而不是添加的Ctrl-W。
The last command will change the gg command to l. So whenever you press l, you’ll be taken to the first line.
最后一条命令会将gg命令更改为l 。 因此,无论何时按l,您都将被带到第一行。
Do this for as many commands as you need. But these mappings are session-based. Once you quit vim, the mappings are reset.
对所需的命令执行此操作。 但是这些映射是基于会话的。 退出vim后,映射将重置。
To set persistent key mapping, we need to add the commands to the VIMrc file. Simply copy-paste the commands without the : in the ~/.vimrc file. If the file does not exist, just create one with only the commands that you need to run on startup of VIM.
9. Linux VIM命令启用语法突出显示 (9. Linux VIM commands to enable Syntax Highlighting)
Continuing from our previous section, we can add the command syntax on in the ~/.vimrc file to enable the syntax highlight. Have a look at the difference that it would make.
To cut a text, the command is the same as delete, the letter d. So you can understand that even when you delete a piece of text, the VIM editor is actually preparing the text to be pasted somewhere else.
Whatever section the cursor is at, will get copied or deleted. To paste the copied text, just type p to paste after the cursor, and P to paste before the cursor.
光标所处的任何区域都将被复制或删除。 要粘贴复制的文本,只需键入p光标后粘贴,以及P光标前粘贴。
11. VIM中的拼写检查 (11. Spell Checking in VIM)
Didn’t expect to see this point, did you? Well, VIm does have a spell checking option that works based on a dictionary file that is already available. All we have to do is set up the language.
Vim allows you to run the code from within the editor without having to lose track of the code. You can do this by the use of an exclamation mark after the colon. Here’s an example:
That’s it! Now if it’s a command that ends after output, you can hit the ENTER key and get back to programming. But in case of this ping command, you can hit the keyboard interrupt Ctrl-C and then hit the ENTER key without losing your VIM editor from the background.
Alternatively, you can just type :shell to directly get a shell opened up for you. Once you’re done with what you need to do, you just type exit to exit the shell and get back to VIM which stays as it was left before.
You know that the words to the right of your cursor need to be deleted. But not the entire sentence. How do you do it? With the same command and some additional parameters.
您知道光标右边的单词需要删除。 但不是整个句子。 你怎么做呢? 使用相同的命令和一些其他参数。
The d command will simply make use of the number specified after it to delete those many words. Have a look at the command below. Both commands work based on where the cursor is at.
d命令将仅使用其后指定的数字来删除许多单词。 看下面的命令。 这两个命令都基于光标所在的位置。
d3w #This will delete 3 words to the right
d3b #This will delete 3 words to the left
15. Linux VIM命令撤消更改 (15. Linux VIM command to Undo Changes)
While you’re getting used to VIM, there will be times when you end up deleting something that you didn’t want to. The Ctrl-Z approach doesn’t work on VIM so how do you undo your work?
The letter u will automatically undo the changes just like ctrl-z would.
字母u会像ctrl-z一样自动撤消更改。
16.从其他文件插入文本 (16. Insert Text From Different File)
This is a pretty useful productivity hack. Saves time copy-pasting things from multiple files to merge between them.
这是一个非常有用的生产力黑客。 节省了从多个文件复制粘贴内容以在它们之间合并的时间。
So what exactly is this? It simply reads text from another file that is specified and inserts the entire text right after the cursor.
那到底是什么呢? 它只是从另一个指定的文件中读取文本,然后将整个文本插入到光标之后。
Before Retrieving Other File
检索其他文件之前
:r <file name>
After Retrieving File Contents
检索文件内容后
17. VIM中的宏 (17. Macros in VIM)
We know VIM can do a lot, but did you imagine a command-line editor to be able to record macros? Well, you better start imagining as this editor is called powerful because of the great potential this simple-looking editor has.
How do you record a macro? It’s very easy! You enter the command mode (If you’re in insert mode, just press Esc).
您如何录制宏? 非常简单! 您进入命令模式(如果处于插入模式,只需按Esc ) 。
qb #The letter after q is just to identify the macro. Can be anything
q #Stop recording macro
@b #Replay the macro. The letter runs that specific macro
What’s the use of this macro? Anything that you can do within the VIm editor can be done with a macro. So for example you:
这个宏有什么用? 在VIm编辑器中可以执行的任何操作都可以使用宏来完成。 例如,您:
Go to the first word 转到第一个字
Delete the first word 删除第一个字
Replace it with something else 用其他东西代替
If you have to do this for multiple lines, all you need to do is record a macro and replay it.
如果必须对多行执行此操作,则只需记录一个宏并重播它即可。
18.使用计数命令 (18. Using Count For Commands)
Almost every command that “might” need to enter multiple times, accepts a number as an argument. For example, the delete command d, the line jump command k, etc.
几乎每个“可能”需要多次输入的命令都接受一个数字作为参数。 例如,删除命令d ,行跳转命令k等。
So if I want to delete 6 lines, I can do the d command 6 times or just say 6d.
因此,如果要删除6行,可以执行d命令6次,或只说6d 。
That’s for delete, you can also do it when moving around the text. If I want to move by 3 words, all I type is 3w or 3e depending on where I want the cursor to be placed, at the start or at the end.
If you’re scrolling through a long file and you simply need to know where you are at present, go for this command. It will give you a percentage figure of where you stand at the present moment within the file.
As you can see in the above image, it tells me that my cursor’s current position is at the 40% mark from the end of the file. The command to do this is by typing Ctrl-G when you’re in command mode.
20. Linux VIM命令的帮助模块 (20. Help Modules For Linux VIM commands )
I’m going to end this post by talking about the handy tool, the help command. Similar to man pages in Linux, the help command allows users to understand all the vim commands in detail. Here’s an example of what the help command shows for our delete command.
This is just going to be the start of your journey in vim-land. There are loads of commands and features that you’ll explore as you go along with learning VIm.
这只是您在vim-land之旅的起点。 学习VIm时将探索大量的命令和功能。
But there’s one thing that you can be assured of. If you learn VIM well enough once, it will just become second nature to you to expect those features when you start using any other editor!