Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Refining search patterns with the command-line window

#28

Run time:

When you need to build a complex command, or devise a non-trivial search pattern, Vim’s command-line mode can be rather limiting. In this episode, I will introduce the command-line window, which allows you to use the full power of Vim when editing search queries and commands.

Shownotes

Commandline mode

When you press : or / in Vim, you go into commandline mode. This opens up a prompt at the bottom of the screen, where you can enter a command or a search pattern.

In commandline mode, you can press ctrl-p to go backwards and ctrl-n to go forwards through your command history. The up and down arrow cursor keys also work.

command action
ctrl-p Show previous historical command/search
ctrl-n Show next historical command/search
ctrl-f Switch from commandline mode to the commandline window

If you need to make extensive edits to a command, change to the commandline window by pressing ctrl-f.

Note that ctrl-f is the default, but it can be customized using the cedit option. Read :help cedit for more information.

The commandline window

The commandline window shows your last 20 search patterns or commands. You can move up and down through them with the k and j keys, and when you press <enter> it executes the command underneath the cursor.

The cool thing about the command-line window is that it’s a regular Vim buffer, so all of the editing commands that you are used to are available to you. This makes it extremely useful if you need to iterate on a search pattern.

command action
q/ Open the commandline window with history of searches
q: Open the commandline window with history of commands
ctrl-f Switch from commandline mode to the commandline window

Note that you can customize the number of commands that Vim remembers with the history setting.

Replacing prime marks with curly quotes

In the video, I use the following example as a demonstration:

This string contains a 'quoted' word.
This string contains 'two' quoted 'words'.
This 'string doesn't make things easy'.

My intention is to replace the prime marks with curly double quotes, so that it looks like this:

This string contains a “quoted” word.
This string contains “two” quoted “words”.
This “string doesn't make things easy”.

To create a suitable search pattern, I went through the following iterations:

\v'.+'
\v'[^']+'
\v'('\w|[^'])+'
\v'(('\w|[^'])+)'

The final pattern works for each of my test cases, so I can use it for a substitution command as follows:

/\v'(('\w|[^'])+)'
:%s//“\1”/gc

Here, I’ve left the search field blank in the substitution command, which tells Vim to use the most recent search. Alternatively, I could hard code the search pattern into the substitution command:

:%s/\v'(('\w|[^'])+)'/“\1”/gc

When making this substitution on an entire document, I use the c flag so that I can inspect each substitution before either doing it or skipping it.

Homework

Note that this substitution command is not a complete solution for replacing prime marks with curly double quotes. It works for my 3 test cases, but there are other situations where it will fail. Here is one:

This string doesn't contain any quotes, isn't it.

Your homework, should you choose to accept it, is to refine the search pattern so that it handles this case appropriately. See if you can think up other failing test cases, and hone the substitution command to work for those as well.

Further reading

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..