Photo by
Alex Knight
on
Unsplash |
Once you installed Vim, learned the basics of Vim Modes and understood Normal mode, it's time to learn another very important mode in Vim: Command-Line Mode (or Command mode, or Cmdline mode). This is the mode where you enter actual commands in Vim.
The Command-Line Mode
It's probably not new to you that Vim is one of the few text editors that allows you to extensively use commands. Combined with Vim's modal design, the Command-Line mode (or to simplify, Command mode) is yet another of Vim's characteristics that make it stand out from alternatives.
Vim's Command mode has a wide variety of commands that allow you to do practically everything. Let's take a look.
Accessing the Command-Line Mode
There are three ways to enter the Command-line mode. Type in Normal mode:
- : - to enter Ex commands
- / or ? - to search
- ! - to filter
Entering commands with :
Typing the : key in Normal mode will allow you to enter Ex commands. Ex commands are Vim's built-in commands you can run to perform any type of operation. Typing : from Normal mode sets your cursor on the last line of the editor where you can type your commands:
Type : in normal mode to get to the command-line mode |
From there, you could enter commands that virtually would do anything. For example, to open a file, type:
Next, to list which files are currently open, type :ls:
Type :ls to list which files are currently open |
We could use the :e[dit] <filename> command to open other files, make our modifications and save them with either :w or :saveas <another-name>. Once done, we quit with the ultra-famous Vim quit command :q!
Searching with / and ?
Filtering with !
Some commands you should know
There are so many commands you can use that we'll list some interesting Ex commands you should know.
Editing Files
- :e <filename> - edits file
- :bd! - closes the current file (buffer)
- :ls - lists open files
- :bn - goes to the next open file
- :bp - goes to the previous open file
Quitting Vim
- :q - quit (short for :quit)
- :q! - quit without saving (short for :quit!)
- :wq - write and quit
- :wq! - force write and quit
- :x - write and quit (similar to :wq, but only write if there are changes)
- :exit - write and exit (same as :x)
- :qa - quit all (short for :quitall)
- :qa! - quit ignoring changes
Searching
Getting Help
Vim has a number of other methods that you can read about in the help documentation, :h or :help.
Command-Line Completion
It's important to note that Vim also provides you command-line completion via the <Tab> key. For example, if you wanted to change your colorscheme, you could type :colorscheme <Tab> and from there, move to the option you want using the <Tab> key:
Type :colorscheme <Tab> and let Vim complete it for you |
Previous Commands
Vim also offers contextual history for previous commands. While in Command-line mode, press the <Up> and <Down> to view previously executed commands.
Getting back to Normal Mode
To get back to Normal mode from Command mode (or one of Vim's other modes), as you might expect, press <Esc> or <Ctrl-C> or <Ctrl-[>.
Learning More
Ready to learn more about the Command mode? Open its dedicated manual with:
Mode-specific help
If you want to know more about specific keys, Vim also has an intelligent mechanism to get you to the help quickly. It follows this pattern:
What | Prepend | Example |
Normal mode command | :help x | |
Visual mode command | v_ | :help v_u |
Insert mode command | i_ | :help i_<Esc> |
Command-line command | : | :help :quit |
Command-line editing | c_ | :help c_<del> |
Vim command argument | - | :help -r |
Option | ' | :help 'textwidth' |
Regular expression | / | :help /[ |
We hope you get used to the above syntax and use it regularly in you your Vim journey. It will not only help you learn more about Vim but also to memorize the commands better.
Conclusion
On this post we continued in our Vim journey by learning a bit more about Vim's Command-Line mode. If it seems complicated, don't be concerned. It takes years to master Vim but be sure that the more you learn, the more you realize that time is that secret ingredient in getting comfortable, becoming proficient and efficient with Vim.
Learning Vim is like learning a musical instrument. It takes time, effort and discipline but once you master it, the gains are endless. You definitely won't regret.