Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Option settings and mapping for vimscript local buffers


May 24, 2021 Vim


Table of contents


Now let's take a minute to review the three things we've already talked about: mapping, abbreviations, and options, and we'll talk about something new in the process. We will set them in a single buffer at the same time.

What this chapter says will really show what they do in the next chapter, and for now we just need to lay the groundwork first.

In this chapter you need to open two files in Vim, which are separate. I 'll name foo bar and you can name them whatever you want. Then enter some text for each file.

Mapping

Select the foo and then execute the following command:

:nnoremap          <leader>d dd
:nnoremap <buffer> <leader>x dd

Now keep under foo make sure you're currently in common mode, and then tap the <leader>d V im deletes a row. As I've said before, there's nothing new.

Still keep under foo tap <leader>x . V im also deletes a line. This is normal because we <leader>x dd

Now switch to file bar T ap in the usual <leader>d Similarly, Vim deletes the current row, and there's nothing new about it.

ok, now let's have something <leader>x tap under file bar .

Vim deletes only one character, not the entire line! Why is this happening?

The second nnoremap <buffer> that the map is valid only in the buffer that defines it:

When you tap under the bar <leader>x Vim can't find a map that matches it, and it's parsed with two commands: <leader> (this doesn't do anything) and x (usually delete one character). x

Local Leader

In this example, <leader>x buffer map is a local buffer map, but this is not an appropriate definition. If we need to set a map that will only be used for a specific buffer, we will generally use the <localleader> instead of <leader>

Using two different leader buttons is like setting up a namespace, which helps you ensure that all the different maps are clearer and more direct to you.

But this is especially important when you are writing a plug-in that will be used by others. Setting up a local map <localleader> your plug-in from overwriting the global mapping that others have set up with <leader> because they may already be very used to setting up global mapping for them.

Set up

In the previous chapters of this book, we talked about using set set options. There are some options that always apply to the entire Vim, but some options can be set based on buffers.

Cut back to the foo and execute the following command:

:setlocal wrap

Then switch to file bar execute the following command:

:setlocal nowrap

Turn your Vim window down and you'll find that some foo line up in bar in bar.

Let's test another option. Switch to foo the following command:

:setlocal number

Now switch to bar execute the following command:

:setlocal nonumber

The line number now appears in the file foo while bar not.

Not all options can be setlocal If you want to know if a particular option can be set to a local option, :help to view its help documentation.

I'm a little brief about how the local option works. You'll learn more about this in practice.

Cover

ok, before we start the next section, let's look at a very interesting feature about local mapping. Switch to file foo and then execute the following command:

:nnoremap <buffer> Q x
:nnoremap          Q dd

Then tap Q what happens.

When you hit Q Vim executes the first map instead of the second, because the first map is more specific than the second, which can be seen as the second map is obscured by the first map.

Switch back to bar and tap Q and Vim uses the second mapping. This is because the second map in this buffer is not obscured by the first map.

Practice

Read :help local-options

Read :help setlocal .

Read :help map-local