Advanced text editor is a nightmare for the casual user. If it is finally possible to close it, then you sigh with relief and firm confidence that you will never run this program again in your life.
Perhaps Vim can not just start and start working – requires some preparation. But once it is worth trying it in and wondering how it was possible to do without it.
Many people use Vim, without using a small part of what he is capable of. In this article , we will look at new techniques for working with Vim, including abbreviations, word completion and editing of several documents in one Vim session.
Although learning Vim may seem like a difficult task in the early stages, be sure that being able to work with it in the future will save you a lot of time. One of the ways that Vim can save you time is word completion. This Vim feature is often used by developers and system administrators when editing source codes of programs or configuration files.
Auto completion works as follows. When you type a long word that already appears somewhere in the text, press Ctrl + p or Ctrl + n while typing the word. For example, you already have the word “searching” in the text. Now start typing this word somewhere in the text, while typing only the first letters, for example “sea”, and press Ctrl + p . Vim will automatically complete the word “searching” or suggest alternative words starting with “sea”, if any.
If there are several options for completing a word, then after pressing Ctrl + p, you can continue typing the word, thus narrowing the scope of the proposed word variations. Alternatively, you can simply select the desired word from the list using the arrow keys and then press Tab . If Vim does not find any suitable words in the document, you will get the message “pattern not found”.
What is the difference between the Ctrl + p and Ctrl + n commands ? In that the first is looking for words in the opposite direction, relative to the current one, and the second – in the forward direction. In many cases, people do not make a big difference between these teams.
As you have probably noticed, Vim is a great editor for people who love working from the keyboard. Among the many features of the editor in this regard, there is another powerful thing – abbreviations that you can customize on the fly, in the editing process. Let’s see what it is.
Imagine that you are editing some text, and it often contains the same long word that you are already tired of typing. This may be someone’s name or the name of a company. So, if you do not want to constantly type “ReallyLongWord”, just make an abbreviation for it in command mode:
: ab rlw ReallyLongWord
The first argument of the command : ab is, in fact, the abbrev itself, and the second is the word to which the abbreviation will be extended. Now, if you type the word “rlw” while typing, Vim will automatically replace it with “ReallyLongWord”. Simple, isn’t it?
What to do if you no longer need to cut and you want to delete it? Just close Vim and all defined abbreviations will be deleted automatically. You can also delete the abbreviation without leaving the editor with the command:
: una rlw
The author constantly uses the described Vim feature. Very handy thing when you often need to use in the text someone’s difficult last name or long name of the organization.
Evaluated abbreviations? And what if you need to perform a sequence of certain commands by pressing a hot key or a few keys? This will help you mapping.
A simple example. Vim can highlight search results. This feature is usually disabled by default. If you follow
You will be automatically moved to the first search result, but you will not see the remaining terms found. If you want to see all occurrences of the desired term, you will need to turn on the search highlighting. The author finds the highlight found distracting, but it is really necessary and useful in the work. So, in the Vim configuration file, there is a line that performs mapping:
nmap <silent> <Cn> <Esc>: call ToggleHLSearch () <CR>
This line configures the mapping of the Ctrl + n combination to enable / disable the search result highlighting feature. First, the Escape key is pressed to switch to command mode, then the command is given: call ToggleHLSearch () and carriage return is pressed, that is, Enter .
Another simpler example. When the author edits someone else’s articles, it is convenient for him to move through the text, skipping a few words at a time. For convenience, the author set up the following mapping, moving the cursor five words forward at a time:
imap, w 5W
It looks a little odd, using two taps instead of two taps. However, personally to the author, it is inconvenient to hold your finger over the number key, and it is more convenient to use the variant given in the example. In general, this is not the point, but to set an example for the reader.
In order for the mappings and abbreviations you have configured to not disappear after closing Vim, you need to add them to the .vimrc file located in your home directory.
Mastering the Vim interface
Initially not obvious, but in a single Vim session, you can simultaneously edit several files. This is done with the help of so-called. “windows” or with the help of a relatively new feature Vim, added in the 7th version – tabs.
Let’s first look at the windows. When you open Vim to edit a file, you see one window with the contents of the file. Imagine that you are editing a very large file and you need to edit it in parallel in two places. Give the command (do not forget to press Ctrl + Esc to go to command mode)
and you will see the same file in two different windows.
Ok, but now how do I switch back to the previous window? Very simple. Switch to command mode, press Ctrl + w and use the arrow keys or the navigation keys ( h, j, k, l ) to select the window you want to switch to relative to the current window. Let’s say your Vim session is divided horizontally into two windows. With the help of the command
Ctrl + wk
you switch to the window located above the active, while the command
Ctrl + wj
will switch you to the window below . A duplicate command Ctrl + w will switch you to the next window.
In addition to switching between windows, you can move the windows themselves. Team
Ctrl + wr
rotates the windows clockwise and the command
Ctrl + w R
rotates windows counterclockwise.
Using multiple windows to work with the same file is used relatively rarely. What is used more often is the division of the session into windows when editing multiple files. Imagine that you wanted to open some other file in the second window. Just pass the path to the desired file as a parameter to the command : split . For example:
: split /etc/dhcp3/dhclient.conf
What if you needed to return to the single-window version of the Vim session? To do this, you only need to close unnecessary windows in the traditional way (using the command : q , :! Q or : wq ) or, being in the window you want to close, give the command
Ctrl + wc
In the case of the last command, Vim will not let you close the modified, but not saved file, which is logical.
Let’s summarize the list of basic commands used when working with windows in Vim.
- : split or Ctrl + ws – adds a horizontal window to the Vim session
- : vsplit or Ctrl + wv – adds a vertical window to the Vim session
- : split filename – adds a horizontal window to the Vim session and loads the filename into it
- : vsplit filename – adds a vertical window to the Vim session and loads the filename into it
- Ctrl + wh – switches to the left window, relative to the current
- Ctrl + wl – switches to the right window, relative to the current
- Ctrl + wj – switches to the bottom window relative to the current one
- Ctrl + wk – switches to the upper window relative to the current one
- Ctrl + w Ctrl + w – switches to the next window
- Ctrl + wr – moves windows clockwise
- Ctrl + w R – moves windows counterclockwise
- Ctrl + wc – closes the current window
And immediately the answer to those who ask: “Is it not better to open several one-window sessions instead of all this fuss with windows?”. Think about how sometimes it is tedious to edit several files opened in several separate SSH sessions to the server, especially the heavily loaded ones. And then, it is much more convenient to copy / paste between several files within one session, when you have everything before your eyes and you do not need to frantically switch between several terminals in search of what you were going to copy and from where.
Since the appearance of tabs in web browsers, users are very used to and actively use this mechanism. Indeed, the idea is excellent. So why not use them in Vim? If you often work simultaneously with several files and you do not need to see them all on one screen at the same time, the use of tabs is the very thing that you need.
To start Vim with several files open in different tabs, run it with the following command:
$ vim -p filename1 filename2 filename3
If you already have an open Vim session, you can upload a file (or create a new one) into a new tab with the command:
: tabnew filename
If you do not specify a filename, the tab that is not associated with the file name will be opened and you will need to specify it when saving.
Switching between bookmarks is also very simple. The gt command will switch you to the next tab, and the gT command to the previous one.
There are several ways to close tabs. The first way is all the same “traditional” closure with the help of the commands : q , :! Q or : wq . The second way is to use the command to close the current tab:
As in the case of windows, this command will not allow you to close a tab if the file in it has been modified but not saved. Let’s summarize the list of commands for working with tabs:
- vim -p filename1 filename2 – launches Vim and opens (or creates new) files filename1 and filename2 in two tabs
- : tabnew – will open an empty tab
- : tabnew filename – opens a new tab and loads (creates a new one) the filename in it
- gt – switch to the next tab
- gT – switch to the previous tab
- : tabc – closes the current tab
You can also combine the use of tabs and windows by opening multiple tabs and splitting each into multiple windows. Combine, Vim is very flexible. If you have a thought about something that you would like from a text editor, most likely Vim already knows how.
Of course, the possibilities of Vim when working with tabs and windows are much richer. You can use the command : help and get the information you need.
Having mastered the techniques described here, you can significantly increase your level of productivity. In the following articles we will get acquainted with several other functions of Vim, and in the meantime you can refer to the vimtutor (started from the shell) and independently dig in the native Vim manual, in which you will likely find something new and useful for yourself.
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Recent Posts: TechnoBlogy