Showing posts with label Advanced. Show all posts
Showing posts with label Advanced. Show all posts

Tuesday, March 2, 2021

Vim - Ex Mode

When learning Vim, it's important to understand its modes, including the Ex Mode where you can continually type your commands using Vim's built-in Ex-compatibility mode.
Photo by Alex Knight on Unsplash

Once you installed Vim, learned the basics of Vim Modes and understood Normal mode, it's time to learn yet another Vim mode: Ex Mode. This is the mode where you can commands in batch and even emulate the old Ex text editor.

What's Ex Mode?

If you learned about the Command-line mode you already know how to enter Ex commands in Vim. Turns out that the Ex mode is an extension of that mode and allows us to run not only single commands but also serves as a built-in batch processing tool for Vim.

It's also worth pointing out that Ex is a text editor, predecessor of Vim, and from which Vim (and vi) got a lot of inspiration from. Vim still today supports a direct Ex emulation (via the Q command) which is less and less used.

The Ex mode is also part of a broader group of advanced capabilities which we'll discuss in future posts.

Entering Ex mode

To enter the improved Ex mode from Vim, press either gQ in Normal mode. You should see a message similar to:

Type gQ to get to Ex mode
From there, you can start typing your commands. For example, highlighted in yellow are three commands run in the same Ex session:

While in Ex mode, keep entering your Ex commands

Ex mode from the shell

Ex mode can also be accessed from the shell via the ex command (or vim -e). Running it, should result in a screen similar to:
Vim started in ex mode

Getting to Normal Mode

To get back to Normal mode from Ex mode, enter the :visual c(or :vi for short).

Quitting Ex Mode

To quit Ex mode, enter the already known :q command.

When to use the Ex mode?

Advanced Vimmers prefer using the Ex for batch processing since they can combine Vim's awesome features (such as buffers and regular expressions) in more powerful ways than if they had to perform the same operations by combining tools like awksed and others.

Apart from batch processing, other more advanced use cases are:

  • editing multiple files non-interactively (for example, as part of the script)
  • an alternative for slow connections
  • mappings and abbreviations are disabled
  • restricted access to keys such as Escape or Control
Keep tuned as we will provide more examples of the above use cases in future blog posts.

Learning More

Ready to learn more about the Ex mode? Open its dedicated manual with:

:h ex-mode

Mode-specific help

If you want to know more about specific keys, Vim also has an intelligent mechanism to get you to the help quickly. It follows this pattern:

What Prepend Example
Normal mode command :help x
Visual mode command v_ :help v_u
Insert mode command i_ :help i_<Esc>
Command-line command : :help :quit
Command-line editing c_ :help c_<del>
Vim command argument - :help -r
Option ' :help 'textwidth'
Regular expression / :help /[

We hope you get used to the above syntax and use it regularly in you your Vim journey. It will not only help you learn more about Vim but also to memorize the commands better.

Conclusion

On this post we continued in our Vim journey by learning a bit more about Vim's Ex mode. If it seems complicated, don't be concerned. It takes years to master Vim but be sure that the more you learn, the more you realize that time is that secret ingredient in getting comfortable, becoming proficient and efficient with Vim.

Learning Vim is like learning a musical instrument. It takes time, effort and discipline but once you master it, the gains are endless. You definitely won't regret.

Recommended Reading

Since the use cases for Ex mode are more advanced, we recommend reading the following articles:

See Also

Tuesday, February 23, 2021

Vim - Select Mode

When learning Vim, it's important to understand its modes, of which, one of the least know is Select Mode.
Photo by Alex Knight on Unsplash

Once you installed Vim, learned the basics of Vim Modes and understood Normal mode, it's time to learn yet another Vim mode: Select Mode. This mode is similar (but different) from the more common Visual mode. Let's take a look

Select Mode

Select mode looks like Visual mode, but the commands accepted are quite different. It was created to be similar to Windows' selection mode but with time has been less and less used.

Entering Select mode

All the following are ways you can enter the select mode:

  • gh or gH or g_CTRL-H - commands in Normal mode
  • Using the mouse to select an area, and 'selectmode' contains "mouse". 'mouse' must also contain a flag for the current mode.
  • Using a non-printable movement command, with the Shift key pressed, and 'selectmode' contains "key".  For example: <S-Left> and <S-End>
  • v, V or CTRL-V - if 'selectmode' is set to "cmd".
  • CTRL-G from Visual mode, 
If the showmode option is set, as soon as you enter the Select mode, should see a message in the bottom line such as:
Press gh to enter Select mode

Important considerations regarding the Select mode

  • Printable characters, <NL> and <CR> cause the selection to be deleted, and Vim enters Insert mode.  The typed character is inserted.
  • Non-printable movement commands, with the Shift key pressed, extend the selection. 'keymodel' must include "startsel".
  • Non-printable movement commands, with the Shift key NOT pressed, stop Select mode. 'keymodel' must include "stopsel".
  • <ESC> stops Select mode.
  • CTRL-O - switches to Visual mode for the duration of one command
  • CTRL-G - switches to Visual mode.

Operators in Select Mode

When using an operator in Select mode, and the selection is linewise, the selected lines are operated upon, but like in characterwise selection.  For example, when a whole line is deleted, it can later be pasted halfway a line.

Why/When use Select Mode?

At a first sight, the Select mode is similar albeit more restrict than Visual mode so when to use it?

The first thing to understand is that the Select mode was created to provide a selection behavior similar to the one used in conventional editors as explains in the documentation:

Select mode looks like Visual mode, but the commands accepted are quite different.  This resembles the selection mode in Microsoft Windows.

That said, today that Vim is widely available in all platforms and pretty popular in Windows too, given its restrictions over the more popular (and feature-rich) Visual mode, there's no real reason to use it.

Getting back to Normal Mode

To get back to Normal mode from select mode (or one of Vim's other modes), press <Esc> or <Ctrl-C> or <Ctrl-[>.

Learning More

Ready to learn more about the Select mode? Open its dedicated manual with:

:h select-mode

Mode-specific help

If you want to know more about specific keys, Vim also has an intelligent mechanism to get you to the help quickly. It follows this pattern:

What Prepend Example
Normal mode command :help x
Visual mode command v_ :help v_u
Insert mode command i_ :help i_<Esc>
Command-line command : :help :quit
Command-line editing c_ :help c_<del>
Vim command argument - :help -r
Option ' :help 'textwidth'
Regular expression / :help /[

We hope you get used to the above syntax and use it regularly in you your Vim journey. It will not only help you learn more about Vim but also to memorize the commands better.

Conclusion

On this post we continued in our Vim journey by learning a bit more about Vim's Select mode. If it seems complicated, don't be concerned. It takes years to master Vim but be sure that the more you learn, the more you realize that time is that secret ingredient in getting comfortable, becoming proficient and efficient with Vim.

Learning Vim is like learning a musical instrument. It takes time, effort and discipline but once you master it, the gains are endless. You definitely won't regret.

See Also

Any comment about this page? Please contact us on Twitter

Featured Article

Vim - Ex Mode

When learning Vim, it's important to understand its modes, including the  Ex Mode  where you can continually type your commands u...

Popular Posts