github github
stars 190
issues 1
subscribers 3
forks 3



10 days ago

nvim-spider 🕷️🕸️

Use the w, e, b motions like a spider. Move by subwords and skip insignificant punctuation.

Lua implementation of CamelCaseMotion, with extra consideration of punctuation. Works in normal, visual, and operator-pending mode. Supports counts and dot-repeats.

If you installed the plugin before March 31, you should change your keymappings to call the motions via Ex-commands to make them dot-repeatable: "<cmd>lua require('spider').motion("w")<CR>". See the example here.


The w, e, b (and ge) motions work the same as the default ones by vim, except for two differences:

Subword Motion

The movements happen by subwords, meaning it stops at the sub-parts of a camelCase, SCREAMING_SNAKE_CASE, or kebab-case variable.

-- positions vim's `w` will move to
local myVariableName = FOO_BAR_BAZ
--    ^              ^ ^

-- positions spider's `w` will move to
local myVariableName = FOO_BAR_BAZ
--    ^ ^       ^    ^ ^   ^   ^

Skipping Insignificant Punctuation

A sequence of one or more punctuation characters is considered significant if it is surrounded by whitespace and does not include any non-punctuation characters.

foo == bar .. "baz"
--  ^      ^    significant punctuation

-- ^    ^  ^  insignificant punctuation

This speeds up the movement across the line by reducing the number of mostly unnecessary stops.

-- positions vim's `w` will move to
if foo:find("%d") and foo == bar then print("[foo] has" .. bar) end
-- ^  ^^   ^  ^^  ^   ^   ^  ^   ^    ^    ^  ^  ^ ^  ^ ^  ^  ^ ^  -> 21

-- positions spider's `w` will move to
if foo:find("%d") and foo == bar then print("[foo] has" .. bar) end
-- ^   ^      ^   ^   ^   ^  ^   ^    ^       ^    ^    ^  ^    ^  -> 14

If you prefer to use this plugin only for subword motion, you can disable this feature by setting skipInsignificantPunctuation = false in the .setup() call.

This plugin ignores vim's iskeyword option.


-- packer
use { "chrisgrieser/nvim-spider" }

-- lazy.nvim
{ "chrisgrieser/nvim-spider", lazy = true },

No keybindings are created by default. Below are the mappings to replace the default w, e, and b motions with this plugin's version of them.

vim.keymap.set({"n", "o", "x"}, "w", "<cmd>lua require('spider').motion('w')<CR>", { desc = "Spider-w" })
vim.keymap.set({"n", "o", "x"}, "e", "<cmd>lua require('spider').motion('e')<CR>", { desc = "Spider-e" })
vim.keymap.set({"n", "o", "x"}, "b", "<cmd>lua require('spider').motion('b')<CR>", { desc = "Spider-b" })
vim.keymap.set({"n", "o", "x"}, "ge", "<cmd>lua require('spider').motion('ge')<CR>", { desc = "Spider-ge" })

For dot-repeat to work, you have to call the motions as Ex-commands. When calling function() require("spider").motion("w") end as third argument of the keymap, dot-repeatability will not work.


The .setup() call is optional. Currently, its only option is to disable the skipping of insignificant punctuation:

-- default value
    skipInsignificantPunctuation = true

Notes on Operator-pending Mode

In operator pending mode, vim's web motions are actually a bit inconsistent. For instance, cw will change to the end of a word instead of the start of the next word, like dw does. This is probably done for convenience in vi's early days before there were text objects. In my view, this is quite problematic since it makes people habitualize inconsistent motion behavior.

In this plugin, such small inconsistencies are therefore deliberately not implemented. Apart from the inconsistency, such a behavior can create unexpected results when used in subwords or near punctuation. If you absolutely want to, you can map cw to ce though. (Remember to add remap = true as option for the keymap.)

Subword Text Object

This plugins supports w, e, and b in operater-pending mode, but does not include a subword-variant of iw. For a version of iw that considers camelCase, check out the subword text object of nvim-various-textobjs.


To @vypxl and @ii14 for figuring out dot-repeatability.

About Me
In my day job, I am a sociologist studying the social mechanisms underlying the digital economy. For my PhD project, I investigate the governance of the app economy and how software ecosystems manage the tension between innovation and compatibility. If you are interested in this subject, feel free to get in touch.

I also occasionally blog about vim: Nano Tips for Vim


Buy Me a Coffee