Share an article from: How to enter text anywhere using the Vim Editor.

Vim is such a great tool that many people can’t get out of it once they start using it. However, Vim still has its flaws. The steep learning curve is one of the drawbacks, and the limitations in some usage scenarios cannot be ignored. Today we introduce two methods to solve the pain points in use.

Use Vim anywhere

I’ll introduce you to Vim-Anywhere, a simple script that allows you to enter text anywhere in Linux using the Vim editor. That means you can simply call up your favorite Vim editor, type whatever you want, and paste that text into any app or website you want. The text will be available on the clipboard until you restart the system. This tool is useful for those who prefer to use Vim key bindings in non-VIM environments.

Install VIM-Anywhere in Linux

The ViM-Anywhere tool runs on any Linux distribution based on GNOME (or other derivative). Also, make sure you have the following dependencies installed.

  • Curl
  • Git
  • gVim
  • xclip

For example, you can install these tools in Ubuntu with the following command:

$ sudo apt install curl git vim-gnome xclipCopy the code

Then run the following command to install Vim-Anywhere:

$ curl -fsSL https://raw.github.com/cknadler/vim-anywhere/master/install | bashCopy the code

Vim-anywhere is now installed. Now let’s see how to use it.

Enter text anywhere using the Vim editor

Suppose you need to create a Word document. But you’d rather use the Vim editor than LibreOffice. Well, that’s where VIM-Anywhere comes in. Vim-anywhere automates the entire process. It simply calls the Vim editor, so you can write whatever you want and paste it into a.doc file.

Let me show you a use case. Open LibreOffice or any graphical text editor of your choice. Then open Vim-Anywhere. All you have to do is press CTRL+ALT+V. It will open the gVim editor. Press I to switch to interactive mode and enter text. When you’re done, type :wq to close and save the file.


The text will remain available in the clipboard until you restart the system. After you close the editor, your previous app retakes the main screen. You simply press CTRL+P to paste the text in.


This is just one example. You can even use Vim-Anywhere to type on annoying Web forms and other applications. Once Vm-Anywhere is called, it opens a buffer. When you close Vim-Anywhere, the contents of the buffer are automatically copied to your clipboard, and the previous app retakes the main screen.

Vm-anywhere creates a temporary file in/TMP/vm-anywhere when called. These temporary files will remain consistent until you restart the system, providing you with a temporary history.

$ ls /tmp/vim-anywhereCopy the code

You can reopen the most recent file with the following command:

$ vim $( ls /tmp/vim-anywhere | sort -r | head -n 1 )Copy the code

Update the Vim – anywhere

Run the following command to update Vim-Anywhere:

$ ~/.vim-anywhere/updateCopy the code

Change shortcut keys

The default key for calling Vim-Anywhere is CTRL+ALT+V. You can change this to any custom key binding using the gconf tool.

$ gconftool -t str --set /desktop/gnome/keybindings/vim-anywhere/binding <custom binding>Copy the code

Uninstall Vim – anywhere

Some people may find it pointless and unnecessary to type in some text every time you open the Vim editor and then copy that text to another application.

If you don’t find this tool useful, simply uninstall it using the following command:

$ ~/.vim-anywhere/uninstallCopy the code

Modify multiple files at the same time

Sometimes, you may need to modify multiple files, or copy the contents of one file into another. In a graphical user interface, you can open a file in any graphical text editor, such as Gedit, and use CTRL + C and CTRL + V to copy and paste content. You cannot use this editor in command line mode. But don’t worry, as long as you have a Vim editor. In this tutorial, you will learn to edit multiple files simultaneously using the Vim editor. Trust me, it’s fun.

We can do this in two ways.

Methods a

There are two files, file1.txt and file2.txt, with a bunch of random words:

$ cat file1.txt
ostechnix
open source
technology
linux
unix

$ cat file2.txt
line1
line2
line3
line4
line5Copy the code

Now, let’s edit both files simultaneously. Please run:

$ vim file1.txt file2.txtCopy the code

Vim will display the contents of the file in order. The contents of the first file are displayed first, then the second, and so on.


– Switch between files

To move to the next file, type:

:nCopy the code


To return to the previous file, type:

:NCopy the code

If there are any unsaved changes, Vim will not allow you to move to the next file. To save changes in the current file, type:

ZZCopy the code

Note that there are two uppercase letters ZZ (SHIFT + ZZ).

To discard the changes and move to a previous file, type:

:N!Copy the code

To see the file you are currently editing, type:

:buffersCopy the code


You’ll see a list of loaded files at the bottom.


To switch to the next file, type :buffer followed by the buffer number. For example, to switch to the first file, type:

:buffer 1Copy the code


– Open other files for editing

We are currently editing two files, file1. TXT and file2.txt. I want to open another file called file3.txt for editing.

What would you do? It’s easy. Just type :e and then enter the file name as shown below:

:e file3.txtCopy the code


Now you can edit file3.txt.

To see the number of files you are currently editing, type:

:buffersCopy the code


Note that for files opened with :e, you cannot switch with :n or :n. To switch to another file, type :buffer, then enter the file buffer number.

– Copies the contents of one file to another

You already know how to open and edit multiple files at once. Sometimes, you may want to copy the contents of one file into another. It can be done. Switch to the file of your choice, for example, suppose you want to copy the contents of file1.txt to file2.txt:

First, please switch to file1.txt:

:buffer 1Copy the code

Move the cursor in front of the line you want to copy, and type YY to extract (copy) the line. Then, move to file2.txt:

:buffer 2Copy the code

Move the cursor to where you want to paste the copied line from file1.txt, and then type P. For example, if you want to paste a copied line between line2 and line3, place the mouse cursor in front of the line and type P.

Example output:

line1
line2
ostechnix
line3
line4
line5Copy the code


To save the changes you made in the current file, type:

ZZCopy the code

Again, two uppercase letters ZZ (SHIFT + Z).

Save all the changes to the files and exit the Vim editor by typing:

:wqCopy the code

Similarly, you can copy any line from any file to another file.

– Copies the entire file contents to another file

We know how to copy a line, but what about the contents of the entire file? That’s ok. For example, you want to copy the entire contents of file1.txt into file2.txt.

First open file2. TXT:

$ vim file2.txtCopy the code

If the file is already loaded, you can switch to file2.txt by entering the following command:

:buffer 2Copy the code

Move the cursor to the location where you want to paste the content of file1.txt. I want to paste the contents of file1.txt after line 5 of file2.txt, so I move the cursor to line 5. Then, type the following command and press Enter:

:r file1.txtCopy the code


Here, r stands for “read”.

You should now see the contents of file1.txt pasted after line 5 of file2.txt.

line1
line2
line3
line4
line5
ostechnix
open source
technology
linux
unixCopy the code


To save changes in the current file, type:

ZZCopy the code

To save all changes to all files and exit the Vim editor, type:

:wqCopy the code

Method 2

Another way to open multiple files at the same time is to use the -o or -o flag.

To open multiple files in a horizontal window, run:

$ vim -o file1.txt file2.txtCopy the code


To switch between Windows, press Ctrl-w w (that is, CTRL + W and w again). Alternatively, you can use the following shortcuts to move between Windows:

  • Ctrl-w k – Top window
  • Ctrl-w j – The window below

To open multiple files in a vertical window, run:

$ vim -O file1.txt file2.txt file3.txtCopy the code


To switch between Windows, press Ctrl-w w (that is, CTRL + W and w again). Alternatively, use the following shortcuts to move between Windows:

  • Ctrl-w L- Left window
  • Ctrl-w h — The right window

Everything else is the same as described in method one.

For example, to list the files currently loaded, run:

:buffersCopy the code

Switching between files:

:buffer 1Copy the code

To open other files, type:

:e file3.txtCopy the code

Copy the entire contents of a file to another file:

:r file1.txtCopy the code

The only difference in method two is that as long as you use ZZ to save changes to the current file, the file will be closed automatically. You then need to type :wq in sequence to close the file. However, if you follow method one, when you type :wq, all changes are saved in all files, and all files are immediately closed.

See the man page for more details.

$ man vimCopy the code


If you want to learn more about Linux knowledge systems, you can take a look at the hundreds of knowledge systems we spent over a month organizing hundreds of hours:

“Linux cloud computing from entry to master” Linux learning entry tutorial series combat notes