• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Draft / Vim Find Your Leader

Vim Find Your Leader

Last updated on May 2, 2019 by Sal Ferrarello

When using Vim you can find your <Leader> (see :help <Leader) by checking the value of mapleader

:echo mapleader

however there are some edge cases.

Undefined Mapleader

If mapleader is not defined, you’ll get the message

E121: Undefined variable: mapleader

in which case your Leader is the default value, which is a backslash (\).

Space bar as Leader

If mapleader is set to your space bar (currently this is my preferred Leader value), you won’t see any output.

Tab as Leader

If mapleader is set to the tab key, you won’t see any output.

A Better Way to Find Your Leader

The following command will output:

  • if leader is undefined, it will display mapleader is \ (the default value)
  • if leader is space bar, it will display mapleader is <Space>
  • if leader is tab, it will display mapleader is <Tab>
  • otherwise, it will display the value of mapleader, e.g. mapleader is ,

Command to Display Leader

:echo join([ 'mapleader is ', ! exists("mapleader") ? '\' : mapleader ==? "\<Space>" ? '<Space>' : mapleader ==? "\<Tab>" ? '<Tab>' : mapleader, ! exists("mapleader") ? ' (the default value)' : '' ], '')

Breakdown of Command

First we call echo join(), this outputs all of the strings in the array we pass to join() concatenated together. We pass a second parameter to join() of '' so there are no spaces between the parts of the string (the default value for this second parameter is ' '). (See :help echo, :help join)

Then we pass a chain of inline conditionals (see :help expression-syntax, :help exists, :help expr-==?, :help <>), the logic here is

:if ! exists("mapleader")
:  echo '\'
:elseif mapleader ==? "\<Space>"
:  echo '<Space>'
:elseif mapleader ==? "\<Tab>"
:  echo '<Tab>'
:else
:  echo mapleader
:endif

Finally, we display the text ” (the default value)” for the case when mapleader is not defined.

:if ! exists("mapleader")
:  echo "( the default value)"
:endif
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Draft, Solution Tagged With: vim

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in