No matter whether you’re a sysadmin by profession or just an old school tech enthusiast like me, if you ever find yourself in love with the awe-inspiring Unix OS, chances are you’re gonna feel the need of a versatile terminal-based Linux text editor at one point or another. For today’s guide, we’ll keep our focus on Vim, considered the best by many, if not all, as the best Linux text editor or code editor ever. So, let’s just skip on all those holy wars among you Vim vs. Emacs pundits and just keep our eye’s on the 100 best Vim commands, today’s topic. Emacs fanatics, however, please don’t get upset, we’ll cover that extensible editor of yours definitely in some time future.
Vim Commands That Will Turn You into A Vim Guru
Below, we’re presenting the 100 most useful Vim commands that will come in handy during your time with Vim. As we’re aiming mainly to beginner Vim users, don’t get daunted all you seasoned Vim masters. This guide will not only enhance the Vim experience of new users but also prove to be a valuable reference to more mature users.
Linux Command Cheat Sheet for Exploring the Vim Modes
Before delving directly into the commands this guide is meant for, we want to draw your attention to Vim modes. Vim features two distinct modes, a command mode and an insert mode.
When you open Vim for the first time, you’re in the command mode. The command mode lets you navigate the screen, delete and copy text among many others. The insert mode on the other hand, as implied, lets users insert text into Vim’s console. You can use the ESC key to switch back to the command mode after finishing typing.
Pressing i in the console gets you to the insert mode. Now you can start typing your texts inside Vim. The text you type will appear in front of the cursor key.
It’s among another commonly used Vim commands you can use to insert text. The texts will be appended right after the cursor.
Want to place your texts at the beginning of the current line? The l command lets you insert text right at your desired location.
This is one of my favorite Vim commands to append text at the end of my current line. It also falls under those Vim commands used for inserting text.
Pressing the lowercase o creates a new line just after the current line you’re in.
The uppercase variant creates the new line before the current line.
You can use this handy little command to open your files under the cursor.
One of my most favorite Vim commands, gi restarts the insert mode in the last insert point you used.
Typing in long strings of text doesn’t matter if you can’t save them for future usage. Enter the wq command in the command mode to save your file and exit Vim.
Often you’ll find yourself in a position where you’re not satisfied with the texts you have typed and would like to close the editor without saving your contents. Entering q! in the command mode lets you do exactly so.
Vim Commands for Moving Around The Console
One of the foremost reason behind Vim’s ultra-fast user experience is its fast movement features. Vim features and supports numerous convenient Vim commands to make your life in front of the terminal blazing fast. Below are some of the most useful Vim commands for faster navigation.
Use the h command to navigate to the left side of your Vim console. This is analogous to the left arrow of your keyboard.
The l commands help you navigate to the right and is a substitute for the right arrow key.
Use the lowercase k command to move upwards. Similar to the arrow up key.
The j command takes you to downward and is a substitute for the down arrow key.
The h j k l keys help you move across the screen much faster than the standard arrow keys. Prefix them with a number to get the most out of these Vim commands. So, for going down ten lines, instead of holding the key for a prolonged period of time, just type 10j. Mastering these passive Vim commands for navigation will enhance your productivity much faster.
This command takes the cursor at the topmost region of your screen.
It will put the cursor in the middle of your Vim screen.
The capital L puts the cursor at the end of the screen.
The H M L Vim commands come in handy whenever you want to get to an approximate portion of your console screen.
- 0 or <HOME>
Entering either 0 or the <HOME> button will take you to the start of the line.
This is among the most commonly used Vim commands to get to the first non-blank character in a line.
This command will take the cursor at the end of the current line.
The lowercase b command lets you move back by tokens.
This command helps you move forward by tokens.
The uppercase variant of b lets you move back by words.
This command will let you move forward by words.
A token is any sequence of digits, letters, or underscores; whereas a word is any non-blank sequence that is separated by whitespace.
This Vim command is basically a Page Up command. However, it moves the screen by half and holds onto the current cursor position.
The Page Down variant of the above command. Functions in the same way.
Prefix the G command with a number to jump directly to a specific line number. Suppose, if you have line numbering enabled and are writing a shell script, you can go to the 10th line by merely typing 10G.
Given, # is the number of a specific line, entering this command will take you to that line directly. It’s one of the most used Vim commands to jump from line to line.
This command (two back ticks) takes you wherever you just were.
You can go-to at the start of the following sentence with this command.
Go-to at the beginning of the earlier sentence.
Used for going to the beginning of the next text block.
Takes cursor at the beginning of the previous text block.
Vim Commands for Editing Texts
Vim combines a significant number of powerful yet flexible editing features that lets users edit their documents seamlessly while paving the path to a versatile workflow. The power of Vim comes from the fact that you can combine many of such Vim commands together to solve your custom needs. This section of the guide covers some elementary Vim commands used in everyday text editing. Continue reading to find out more useful Vim commands for editing.
The yy command lets you copy an entire line. It’s something you’ll often employ during your time with Vim.
This is one of the most widely used Vim commands for copying a word across the editor.
One of my all-time favorite Vim commands, it gives users the ability to copy texts from the current position of the cursor to the end of a line.
The v command can be used to highlight a single character at a time in combination with the movement keys (arrows/ h j k l).
Almost the same as the above one, but instead of a word, this command will highlight an entire line.
You can paste the contents of your clipboard to the unnamed register with this handy little command.
This is among the most used Vim commands for deleting highlighted texts.
Want to delete an entire line with a single Vim command? The dd command is specially created for this purpose.
You can delete a single word pretty quickly with the dw command.
One of the most powerful Vim commands ever, the D command deletes everything from the current cursor location to the end of the line.
This command is used for deleting everything from your cursor’s current position to the beginning of the line.
You can use this command to delete everything from your cursor’s current position to the beginning of the file.
This command will wipe out everything from your cursor’s current position to the end of the file.
Use the x command whenever you need to delete a single character.
The u command is among the most widely used Vim commands for many to undo the last operation. Combining with a postfix allows users to undo multiple actions. So, u# undo’s the last # number of actions.
Use the above command to redo the last undo operation.
The dot (.) command is one of those useful Vim commands that decreases your workload significantly by repeating the last action whenever you need such functionality.
You can use the cc command to change line(s) by clearing and entering the insert mode at the same time. The cursor is placed at the current level of indentation.
Useful Vim Commands for Replacing Texts
Apart from the generic command and insert modes, Vim has another mode called the visual mode. You can highlight and change a particular portion of your text document in this mode. But where’s the fun in that, huh? Using a select set of Vim commands can achieve you the same result, just a bit faster. Below are some of my favorite Vim commands for changing texts on the fly.
The r command is a pretty handy tool when it comes to changing a single character. Follow it with <char>, and it will change the current character under the cursor with <char>.
The capital R opens the insert mode, but instead of inserting texts, you can replace them with this command.
The tilda(~) command is pretty useful when you need to change the case of a character in your document. Follow it with a number to flip that many characters.
Enter t<char> to select up to but not including the next <char> on a particular line.
Press f<char> to select up to and including the next <char> on a line.
Want to select everything in between a set of parentheses or some other unique character? Type i<char> to select everything that sits between two consecutive <char>.
This command is identical to the previous one but includes the <char> at both ends of text.
Most Used Vim Commands for Searching Through A Document
The searching functionality is of greater importance to any versatile Linux text editor. Vim offers exceptionally powerful commands for searching through a document. It’s one of the most powerful areas of this innovative text editor. Glance over the below Vim commands to search through your document effectively.
The slash command is the single most used command for searching through huge text files in Vim. Just type / and follow it with the texts you want Vim to search for you and look out at the bottom corner of the console.
The \c option, when piped to the search (/) command, lets users search for texts in a case sensitive manner. Wise utilization of this command can save hours of hard work.
This is among the most useful Vim commands for searching previous texts for a given <pattern>.
The n command searches in the direction of your last search. Use this command if you know in which direction your search item is.
Almost identical to the above command, but searches in the opposite direction of your last search.
The above command utilizes regular expression for searching every occurrence of [pattern] and replaces it with [replacement] without asking for confirmation.
Same as the previous command but asks for confirmation before replacing each instance of [pattern] with [replacement].
Instead of replacing every single instance of [pattern] on your file, this Vim command will replace only those [pattern] that are in the current line with [replacement].
- :bufdo /[pattern]
This is among those powerful Vim commands that let users search for [pattern] in every currently opened buffer. It will enhance your productivity and decrease search time significantly.
This is among those useful Vim commands that will come in handy whenever you want to delete every line that contains string from your document.
Linux Command Cheat Sheet for Working With Multiple Files in Vim
Editing in the terminal is fun and alright. But, where’s the edge of terminal editors if you can’t edit multiple files with them. Luckily, as an iconic text editor, Vim lets you work with multiple documents in a pretty neat way. The ability to split the screen when working with multiple files at the same time is something Vim has been offering even before the concept of the split screen became mainstream. Now, you should slowly start to realize why this legendary editor is hailed in such high regards.
- :sp [filename]
Use this command to create a new file and split the console screen horizontally in order to show the two different buffers.
- :vsp [filename]
The functionality of this Vim command is, in essence, identical to the above command, but instead of splitting the console horizontally it splits the screen vertically.
This Vim command will switch your editor to the next buffer. It’s among those few fundamental Vim commands without which you won’t be able to work with multiple documents in Vim.
Identical to the earlier command, but switches to the previous buffer instead of going next.
Use this Vim command when closing a specific buffer. Make sure to save your data using appropriate Vim commands.
This is one of the handy Vim commands that will present users with a list of all open buffers.
If you want to split your Vim windows horizontally, then this is the command you’re looking for.
Instead of splitting the windows horizontally, this Vim command will split it vertically.
Utilize this command to switch between multiple windows directly from the command mode.
You can use this handy Vim command to quit a particular window.
This command will move the location of your cursor to the left window.
Same as the previous command, but instead of moving the cursor left, this command will point to the right window.
Use this command whenever you want to move a window beneath your existing one.
Same as above, but takes the cursor to the window above the current one.
Useful Vim Commands When Working With Multiple Tabs
Vim offers a variety of Vim commands to open and navigate a plethora of tabs right from the terminal. You can use these useful Vim commands whenever you are working with multiple files simultaneously. We encourage new Vim users to glance over such commands as they will often come in handy after you’ve mastered the basic Vim commands and start to do heavyweight staffs with it.
You can use the :tabnew command to create a new tab and work with some other document while not leaving the current file.
The gt command will show you the next tab opened by you.
The above command will show you the first tab you’ve got opened in a particular session.
Same as the previous command, but instead of showing the first tab it will show the last tab.
- tabm n(position)
This powerful Vim command will come in handy whenever you feel the need to re-arrange your existing tabs.
- tabdo %s/foo/bar/g
You can utilize the above command whenever you want to execute a command in every opened tab at the same time.
- :tab ball
This Vim command is one of my favorite Vim commands, and it puts each open files in tabs.
- :new abc.txt
This is among those Vim commands that lets you open a new file named abc.txt in a new window without exiting the current document.
Miscellaneous Vim Commands
As Vim offers plenty of Vim commands, we are unable to cover every single one of them categorically. That’s why in this section we’ll overview some useful Vim commands that are used by veteran users for a wide array of purpose.
Pressing this Vim command in the command mode saves your current document but do not exist the existing session.
This command will exit the current session without saving your changes. Note that you will see the E37 error if you have unsaved changes in your document. In such scenarios, you need to override this command and use q! instead.
- :help [command]
The help command performs a search operation on the command you’ve typed and shows relevant information on them right in the console.
- :e [file]
This command will open a file named [file] and create a new one if it doesn’t exist in your filesystem already.
- :w [filename]
Use this command to save your existing document directly into a new file named [filename].
Writing this command in the command mode will suspend your current Vim session. You can also do this by pressing ctrl+z at the same time.
- :browse e
Use this command whenever you want to invoke the graphical file explorer from your Vim console.
Writing this command will align each line of your current file.
Use this whenever you need to align all lines at the present position of your cursor.
- :set autoindent
This is one of the most used Vim commands you will use during your time with Vim. It sets autoindent for your current session.
Vim has reached to the massive popularity it enjoys today among veteran Unix users not only because of its powerful editing capabilities but also for the vast array of versatile commands it offers. Mastering such useful Vim commands is not a child’s play, and we strongly suggest you do not try to memorize each of them individually. Instead, try to pick a few commands you can use right away and apply those directly in your next editing session. Only by practically using them, you will be able to master these different Vim commands. Hopefully, this guide fulfills your intro to Vim and provides the insights you were looking for.