Special offer
popup-offers
English
+370 5 205 5502 sales@monovm.com

Linux Vim Commands

There are many editors you could use for Linux, and today we will be discussing the best commands to know for the Vim editor, one of the more popular ones.

22 Jul, 21 by Antoniy Yushkevych 8 min Read

List of content you will read in this article:

Vi Improved text editor or VIM text editor is one of the most reliable and advanced text editors available in the market. But what is a VIM text editor? Let us study.

 

Commonly, the Vim text editor is also known as Vi. Initially, Bill Joy discovered this editor in the 1970s. At that time, Vi was A short form for the visual editor. Vim is an extremely fast and efficient UNIX text editor. It is also inclusive in Linux and macOS. Being a small application, it provides you with great ease of use. You can control it with the keyboard without the need for a mouse or menus. For more clarity, here is an example. 

Suppose you wish to insert some text into a particular file. To do that, you press I and then type. There are several key combinations that correspond to taking certain actions. Therefore, with the help of key combinations, you can execute the work.  Although Vim takes a different approach to editing text as compared to what users expect generally. But, this is the way Unix works, in order to edit config files, scripts, changelogs, and much more. 

After understanding the basic concept of Vim text editor, let us move to the next segment!

 

By default, Vim is the default editor on POSIX systems. There are instances where you are unable to access some editors due to various situations. For instance, in cases where you have just installed the OS, or performed booting on your device. Well, in that case, do not worry, you can use Vim instead of stalling your work!

Moreover, Vim is extremely efficient when it comes to design and functioning. The native interface is not dependent upon menus or some fancy peripherals. There are no extra keys too, for instance, Ctrl or Alt. Vim proves to be greatly simplified. It uses the keys that are common to any keyboard, no matter what the language, layout, or device is. 

Now, you may wonder how to use the Vim text editor! Well, it’s really simple. Take a look!

 

Vim has immense potential with regard to its simplicity and interactiveness. If you are new to this text editor, you can get a walk-through of the basics with the help of Vimtutor. Vimtutor provides you with basic lessons on how to operate Vim. 

Here are the steps that you must follow to begin your journey with Vim text editor.

  • Go to the terminal and type vim.
  • To initiate the insert text mode press I. Here, you can type text into your document without the use of any commands.
  • And if you wish to switch to the normal mode, press Esc. When you are operating in normal mode, here are some keys to remember for basic operations. h is for Left, k is for up, j is for down, and I is for right. 
  • To save your work, type :q and to exit Vim.

You must take note of the fact that when you first launch Vim text editor, which creates and opens an empty text file. 

Your first action is to open a file or save an empty file with a specific name. You cannot directly type something in the file because the normal mode is on that issues commands to Vim. And therefore if you wish to type without opening or saving a file press I on your keyboard. After doing this, you are ready to enter text in the text editor.

Now, let us understand several Linux Vim commands.

 

We are going to cover the following pointers under this section. Take a look:

  • Multiple files in Vim
  • Autocomplete text
  • Brace Matching
  • How to explore files in your system
  • Split code window
  • Re-map commands
  • Syntax highlighting 
  • Spell checking
  • Delete multiple words 

Multiple files in Vim

Programmers need to work with multiple files on a regular basis. Although there are certain keyboard shortcuts to switch to different tabs, Vim does it way better. 

Take a look at the following command:

:tabedit <tab key to scroll through files in directory>

Here, you do not have to touch the mouse at all. Simple use get or gT to switch tabs. Moreover, you can also use :tabfirst and :tablast to switch back and forth from all the tabs currently open.

Additionally, if you wish to open all the files in the starting, type the following command:

vim -p source_file1.py source_file2.py source_file3.py 

Autocomplete text

Vim is robust enough to execute the auto-complete features without using artificial intelligence or some machine learning algorithms.

If you wish to execute auto-completion, use the following key combination and you’re good to go!

Ctrl+n

Brace matching

Vim makes things pretty easy for you while you’re working with complicated codes. To accomplish this task, take your cursor to the opening or closing press and enter % from your keyboard. After doing this, the cursor jumps to the matching brace. 

How to explore files in a new system

In Vim, you have to use two commands, :Vexplore and :Sexplore. These commands help you to split your window into two sections and search your file system throughout. 

Take a look at the execution process for more clarity.

As it is evident from the snapshots that the file manager is opened and you can easily navigate through the files without leaving the page of the code file. 

Now, you have seen that navigation through the files at the same time on a single screen is possible. But, what if you wish to split the screen and edit two codes simultaneously.

Tread on!

Use the vsplit or split commands as shown below.

Vertical split

:vsplit <filename>

or

:vsp <filename>

Horizontal split

:split <filename>

or

:sp <filename>

After you execute this command the screen splits into two parts vertically as shown in the snapshot below.

There are instances when you are working with an SSH client software using the Vim commands to switch between the split-screen, it terminates the connection. 

Whereas, the text editor helps you to remap keys and avoid such conflicts. 

Remap commands

There are several shortcuts that can save you from putting in an immense effort. 

Here are the commands:

:map <new key-combo> <existing key-combo>

For example: 

:map <C-J> <C-W><C-J>

:map <C-K> <C-W><C-K>

:map <C-L> <C-W><C-L>

:map <C-H> <C-W><C-H>

:map l gg

This mapping helps to change the split window switching commands to Ctrl+J and Ctrl+K.

Subsequently, the last line changes the gg command to l. Therefore, if you now press I the text editor directs you to the first line. 

You can try this for as many commands as you want. You must remember that these mappings are session-based, therefore, as soon as you quit the text editor the mapping is reset. 

Furthermore, if you wish to set up the key mappings by default in the vim configuration, carry out the following step:

root@ubuntu:~# ln -s ~/code/dotfiles/vim/vimrc ~/.vimrc

Syntax highlighting 

In order to enable the syntax highlighting, you can add the following command. Take a look at the following snapshot for Syntax off vim and Syntax on vim respectively.

Spell Checking

Well, to your surprise the text editor also does spellcheck. For that, you need to set up the language first and then continue further.

Add the following command in the ~/.vimrc file.

set spell spelllang=en_us

And you’re done.

Delete multiple words

What if you wish to delete some words on the right of a person but not the complete sentence?

Well, to execute the same you need to follow the below-given command:

d3w #This will delete 3 words to the right 

d3b #This will delete 3 words to the left

Use this command as and when required according to your needs.

 

This article helps you understand the basic commands related to the vim text editor. There is a bundle of features and commands associated with the same. Vim has a lot for you to explore. 

We hope that the information provided in this article helps you to start the journey with Vim!

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com