There are a few tricks in Vim that can make your Vim editor even more productive. Here are 5 ways to improve your Vim skills for your reference.

Vim is one of the most popular text editors out there, so it’s definitely worth your time to learn how to use it. Even if you just learn to use the ubiquitous Vi(m) command-line text editor to open a file, type and edit some text, save the edited file, and exit the program, you’ll benefit.

You’ll find that scenarios that are very convenient to use Vim almost always involve running remote Shell operations. If you use SSH from time to time, for example

ssh [email protected]
Copy the code

And working in a virtual private server (VPS) or local virtualization container, you can benefit from these powerful Vim skills.

1. Set Vim as the default text editor

In almost all modern Linux (or BSD) distributions, Vim can be used at the terminal emulator’s shell command prompt. Once you have defined Vim as the default editor in your user shell program, you can use the familiar Vim key bindings to navigate the built-in utilities, such as MAN. I’ll show you how to do this using Bash and the Z shell (ZSH), which is now the default shell for macOS users (since Catalina).

Set Vim to the default in Bash

Bash manages Settings through a combination of dot files. The most common practice is to add your editor to the.bashrc file in your home directory, but you can also add it to.bash_profile. (Read the GNU Bash documentation for the differences).

Set Vim as the default editor by adding the following to ~/.bashrc:

# set default editor to Vim
export EDITOR=vim
Copy the code

Lines starting with # are optional comments, which are a good way to remind yourself of what the command does.

Set Vim as the default in ZSH

ZSH is an increasingly popular terminal emulator, especially since Apple’s FreeBSD-based Darwin system recently switched from Bash to ZSH.

ZSH dot files are equivalent to Bash files, so you can choose between ~/.zshrc or ~/.zprofile. See the ZSH documentation for details on when to use which.

Set it to default:

# set default editor to Vim
export EDITOR=vim
Copy the code

Second, optimize Vim configuration

Vim, like the terminal emulator shell, uses dot files to set personal preferences. If you spotted this pattern, you might have guessed it was ~/.vimrc.

The first setting you might want to change is to switch the traditional Vi compatibility mode to “Off.” Since Vim is a superset of Vi, all the functionality in Vi is available in Vim and has been greatly improved in Vim, you can get a lot of advanced functionality. The latest version (8.2) allows you to open a terminal as a child process shell in a split window.

By the way, explicitly turning off compatibility with traditional Vi doesn’t seem to have much effect (in fact, it probably doesn’t). When Vim encounters.vimrc files, it secretly automatically switches compatibility mode to off. But sometimes it’s still important to turn it off explicitly. The abbreviation nocp is a synonym for nocompatible and serves the same purpose. All roads lead to Rome, and there are many ways to switch preferences.

In the.vimrc syntax, lines starting with “are comments (like # in the.bashrc file) that help you remember things like why you chose an obscure setting name.

To turn off Vi compatibility, add the following to the ~/.vimrc file:

" ensure that legacy compatibility mode is off
" documentation: <http://vimdoc.sourceforge.net/htmldoc/options.html\#'compatible>'
set nocp
Copy the code

3. Understanding patterns

Vim’s concept of “mode” is very important, especially the distinction between “normal mode” and “insertion mode.” Confusion over patterns is a problem for most new users. Patterns are not unique to Vim, or even introduced by Vi. Command mode is so old that it predates the invention of copy and paste in the 70s.

Important patterns

Vim relies on different patterns to define keystroke behavior. Important patterns to know are:

  • Common mode: the default mode, which is used to navigate and open files.
  • Insert mode (including replace) : In this mode Vim allows text to be entered into an open file.
  • Visual mode: Vim behaves like mouse-based input, such as copy, edit, replace, and so on.
  • Command mode (including line mode, Ex command mode, and last line mode) : is a powerful way to do more in Vim.

Each model has a lot to explore. Use Vimtutor (Vimtutor) to interactively learn to move the cursor, patterns, and run Ex commands in last-line mode. Some indispensable productivity operators include:

The operator instructions
:E Open resource Manager for locating files and directories.
. Repeat the last edit.
; Repeat the previous action or movement forward
. To repeat a previous action or movement backward.
/ Search ahead for documents.
? Search the document backwards.
* Find the next occurrence of the word at the cursor.
# Finds the last occurrence of the word at which the cursor is located.
~ Toggle case.
% in(a),[]{}Switch between open and close symbols; Very useful for coding.
z= Make spelling suggestions.

Play Vim like a piano

While it’s important to keep Vim’s operator “language” in memory, the hard part to master it is learning to think like a musician, combining operators and actions into “chords” so you can play Vim like a piano. This is where Vim’s text-manipulation capabilities are comparable to Emacs, another well-known command line editor. (Although one editor will wear out your Esc key and the other will wear out your Ctrl key.)

When describing key “chords”, it is traditional in Vim to use a capital C followed by a hyphen (C-) to refer to the Ctrl key. This is not universal, but I will follow this convention from here and illustrate it when there is a risk of confusion.

If you type long lines in Vim, you’ll want to set it to newline. To personalize Vim based on how you work, consider this setting: By default, how do you want Vim to start with text newlines? On or off? I like to close it and leave it outside of the run command file. When I want to wrap text, I simply set it to :set wrap in command line mode.

There’s nothing wrong with having Vim set to wrap by default, it’s just a matter of preference — it can change over time. You can also control pasting, code language indent syntax, and Tab Settings (tabs or Spaces? How many Spaces? But delve into those options here as well). All of these options for default behavior are fully configurable and changeable, and can be changed in real time as you operate in command line mode.

You’ll find plenty of advice on setting Vim defaults in community forums, the Vim Wiki, and articles like this one. You should be familiar with setting preferences for your personal computing environment, and Vim is no exception. I highly recommend that you start with small changes to your Settings and work your way through the rest so that you can easily restore your Settings. That way, you can avoid plug-ins for years, or not use them at all.

V. Segmentation, labels and terminals in Vim 8.2

There are two ways to split the files you’re working on into different views: they can be displayed side by side, or they can be switched in full screen (Windows) using the application TAB. These changes to the application window are initiated from command mode, which requires a colon (:) to call up the prompt.

Each split window can hold one file for editing, and you can switch between more files with tabs. Split screen space is limited, so tabs come in handy when you want to split more screens. How you want it set up is a matter of personal preference. To split a window horizontally, use :sp and to split a window vertically, use :vs.

Starting with Vim 8.2, you can use vert Term to open a vertically split terminal shell subprocess to operate on the command line next to your code. You’ll need to type Exit to close your terminal process, just as you would close a shell session, but you’ll close the split window and tabs the same way you close any normal Vim window, with :q.

To initialize a TAB, use a special edit command: :tabedit, which automatically switches to the newly opened TAB. If you give the command a filename as an argument, the file will be opened and edited. If you omit to give it a filename as an argument, you can use the edit command e filename. TXT in command line mode, just like in any normal Vim window. You can use the next (:tabn) and previous (:tabp) commands to navigate between tabs.

To use split, you need to know how to use the combination keys C-W and the move keys in the direction you want to move, such as left (H), down (j), left (k), right (L). If you want to learn more about key combinations, read the Vim manual: Help split and: Help tabPage.

Get help

While you can open reference Vim manuals in Vimtutor, opening Vim help with :help allows you to spend your own time in the editor and get more done without relying entirely on articles like this one. Experience is the key to mastering Vim. Experience helps sharpen your overall computational intuition, as much of Vim is drawn from the Unix universe.

Have fun exploring the beauty of Vim, and share any questions in the comments.

Via: opensource.com/article/20/…