• Stars
    star
    510
  • Rank 86,627 (Top 2 %)
  • Language
    Lua
  • License
    MIT License
  • Created almost 2 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Bundle of more than 30 new text objects for Neovim.

nvim-various-textobjs 🟪🔷🟡

Bundle of more than two dozen new text objects for Neovim.

List of Text Objects

textobj description inner / outer forward-seeking default keymaps filetypes (for default keymaps)
indentation surrounding lines with same or higher indentation see overview from vim-indent-object - ii, ai, aI, (iI) all
restOfIndentation lines down with same or higher indentation - - R all
subword like iw, but treating -, _, and . as word delimiters and only part of camelCase outer includes trailing _,-, or space - iS, aS all
toNextClosingBracket from cursor to next closing ], ), or } - - % all
restOfParagraph like }, but linewise - - r all
entireBuffer entire buffer as one text object - - gG all
nearEoL from cursor position to end of line, minus one character - - n all
lineCharacterwise current line, but characterwise outer includes indentation and trailing spaces - i_, a_ all
column column down until indent or shorter line. Accepts {count} for multiple columns. - - | all
value value of key-value pair, or right side of a variable assignment (inside one line) outer includes trailing commas or semicolons small iv, av all
key key of key-value pair, or left side of a variable assignment outer includes the = or : small ik, ak all
url link beginning with "http" - big L all
number* numbers, similar to <C-a> inner: only pure digits, outer: number including minus sign and decimal point small in, an all
diagnostic LSP diagnostic (requires built-in LSP) - big ! all
closedFold closed fold outer includes one line after the last folded line big iz, az all
chainMember field with the full call, like .encode(param) outer includes the leading . (or :) small im, am all
visibleInWindow all lines visible in the current window - gw all
restOfWindow from the cursorline to the last line in the window - gW all
mdlink markdown link like [title](url) inner is only the link title (between the []) small il, al markdown, toml
mdFencedCodeBlock markdown fenced code (enclosed by three backticks) outer includes the enclosing backticks big iC, aC markdown
cssSelector class in CSS like .my-class outer includes trailing comma and space small ic, ac css, scss
htmlAttribute attribute in html/xml like href="foobar.com" inner is only the value inside the quotes trailing comma and space small ix, ax html, xml, css, scss, vue
jsRegex* JavaScript regex pattern outer includes the slashes and any flags small i/, a/ javascript, typescript
doubleSquareBrackets text enclosed by [[]] outer includes the four square brackets small iD, aD lua, shell, neorg, markdown
shellPipe command stdout is piped to outer includes the front pipe character small iP,aP bash, zsh, fish, sh

Warning
* Textobject deprecated due to treesitter-textobject introducing a similar textobject that is more capable.

Installation

-- packer
use {
	"chrisgrieser/nvim-various-textobjs",
	config = function () 
		require("various-textobjs").setup({ useDefaultKeymaps = true })
	end,
}

-- lazy.nvim
{
	"chrisgrieser/nvim-various-textobjs",
	opts = { useDefaultKeymaps = true },
},

Configuration

The .setup() call is optional if you are fine with the defaults below. (Note that the default is to not set any keymaps.)

-- default config
require("various-textobjs").setup {
	-- lines to seek forwards for "small" textobjs (mostly characterwise textobjs)
	-- set to 0 to only look in the current line
	lookForwardSmall = 5, 

	-- lines to seek forwards for "big" textobjs (mostly linewise textobjs)
	lookForwardBig = 15,

	-- use suggested keymaps (see README)
	useDefaultKeymaps = false, 

	-- disable some default keymaps, e.g. { "ai", "ii" }
	disabledKeymaps = {},
}

If you want to set your own keybindings, you can do so by calling the respective functions:

  • The function names correspond to the textobject names from the overview table.
  • The text objects that differentiate between outer and inner require a Boolean parameter, true always meaning "inner," and false meaning "outer."
  • The keymaps need to be called as Ex-command, otherwise they will not be dot-repeatable. function () require("various-textobjs").diagnostic() end as third argument for the keymap works in general, but the text objects will not be dot-repeatable then.
-- example: `?` for diagnostic textobj
vim.keymap.set({ "o", "x" }, "?", '<cmd>lua require("various-textobjs").diagnostic()<CR>')

-- example: `an` for outer subword, `in` for inner subword
vim.keymap.set({ "o", "x" }, "aS", '<cmd>lua require("various-textobjs").subword(false)<CR>')
vim.keymap.set({ "o", "x" }, "iS", '<cmd>lua require("various-textobjs").subword(true)<CR>')

-- exception: indentation textobj requires two parameters, the first for
-- exclusion of the starting border, the second for the exclusion of ending
-- border
vim.keymap.set({ "o", "x" }, "ii", '<cmd>lua require("various-textobjs").indentation(true, true)<CR>')
vim.keymap.set({ "o", "x" }, "ai", '<cmd>lua require("various-textobjs").indentation(false, true)<CR>')

For your convenience, here the code to create mappings for all text objects. You can copypaste this list and enter your own bindings.

Mappings for all text objects
local keymap = vim.keymap.set

keymap({ "o", "x" }, "ii", "<cmd>lua require('various-textobjs').indentation(true, true)<CR>")
keymap({ "o", "x" }, "ai", "<cmd>lua require('various-textobjs').indentation(false, true)<CR>")
keymap({ "o", "x" }, "iI", "<cmd>lua require('various-textobjs').indentation(true, true)<CR>")
keymap({ "o", "x" }, "aI", "<cmd>lua require('various-textobjs').indentation(false, false)<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').subword(true)<CR>")
keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').subword(false)<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').toNextClosingBracket()<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').restOfParagraph()<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').entireBuffer()<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').nearEoL()<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').lineCharacterwise(true)<CR>")
keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').lineCharacterwise(false)<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').column()<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').value(true)<CR>")
keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').value(false)<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').key(true)<CR>")
keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').key(false)<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').url()<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').diagnostic()<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').closedFold(true)<CR>")
keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').closedFold(false)<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').chainMember(true)<CR>")
keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').chainMember(false)<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').visibleInWindow()<CR>")
keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').restOfWindow()<CR>")

--------------------------------------------------------------------------------------
-- put these into the ftplugins or autocms for the filetypes you want to use them with

keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').mdlink(true)<CR>",
	{ buffer = true }
)
keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').mdlink(false)<CR>",
	{ buffer = true }
)

keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').mdFencedCodeBlock(true)<CR>",
	{ buffer = true }
)
keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').mdFencedCodeBlock(false)<CR>",
	{ buffer = true }
)

keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').cssSelector(true)<CR>",
	{ buffer = true }
)
keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').cssSelector(false)<CR>",
	{ buffer = true }
)

keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').htmlAttribute(true)<CR>",
	{ buffer = true }
)
keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').htmlAttribute(false)<CR>",
	{ buffer = true }
)

keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').doubleSquareBrackets(true)<CR>",
	{ buffer = true }
)
keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').doubleSquareBrackets(false)<CR>",
	{ buffer = true }
)

keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').shellPipe(true)<CR>",
	{ buffer = true }
)
keymap(
	{ "o", "x" },
	"YOUR_MAPPING",
	"<cmd>lua require('various-textobjs').shellPipe(false)<CR>",
	{ buffer = true }
)

Advanced Usage

Smart Alternative to gx

Using the URL textobject, you can also write a small snippet to replace netrw's gx. The code below retrieves the next URL (within the amount of lines configured in the setup call), and opens it in your browser. While this is already an improvement to vim's built-in gx, which requires the cursor to be standing on a URL to work, you can even go one step further. If no URL has been found within the next few lines, the :UrlView command from urlview.nvim is triggered. This searches the entire buffer for URLs to choose from.

vim.keymap.set("n", "gx", function()
	 -- select URL
	require("various-textobjs").url()

	-- plugin only switches to visual mode when textobj found
	local foundURL = vim.fn.mode():find("v")

	-- if not found, search whole buffer via urlview.nvim instead
	if not foundURL then
		vim.cmd.UrlView("buffer")
		return
	end

	-- retrieve URL with the z-register as intermediary
	vim.cmd.normal { '"zy', bang = true }
	local url = vim.fn.getreg("z")

	-- open with the OS-specific shell command
	local opener
	if vim.fn.has("macunix") == 1 then
		opener = "open"
	elseif vim.fn.has("linux") == 1 then
		opener = "xdg-open"
	elseif vim.fn.has("win64") == 1 or vim.fn.has("win32") == 1 then
		opener = "start"
	end
	local openCommand = string.format("%s '%s' >/dev/null 2>&1", opener, url)
	os.execute(openCommand)
end, { desc = "Smart URL Opener" })

Delete Surrounding Indentation

Using the indentation textobject, you can also create custom indentation-related utilities. A common operation is to remove the line before and after an indentation. Take for example this case where you are removing the foo condition:

-- before (cursor on `print("bar")`)
if foo then
	print("bar")
	print("baz")
end

-- after
print("bar")
print("baz")

The code below achieves this by dedenting the inner indentation textobject (essentially running <ii), and deleting the two lines surrounding it. As for the mapping, dsi should make sense since this command is somewhat similar to the ds operator from vim-surround but performed on an indentation textobject. (It is also an intuitive mnemonic: delete surrounding indentation.)

vim.keymap.set("n", "dsi", function()
	-- select inner indentation
	require("various-textobjs").indentation(true, true)

	-- plugin only switches to visual mode when textobj found
	local notOnIndentedLine = vim.fn.mode():find("V") == nil
	if notOnIndentedLine then return end

	-- dedent indentation
	vim.cmd.normal { "<" , bang = true }

	-- delete surrounding lines
	local endBorderLn = vim.api.nvim_buf_get_mark(0, ">")[1] + 1
	local startBorderLn = vim.api.nvim_buf_get_mark(0, "<")[1] - 1
	vim.cmd(tostring(endBorderLn) .. " delete") -- delete end first so line index is not shifted
	vim.cmd(tostring(startBorderLn) .. " delete")
end, { desc = "Delete surrounding indentation" })

Other Ideas?

If you have some other useful ideas, feel free to share them in this repo's discussion page.

Limitations

  • This plugin uses pattern matching, so it can be inaccurate in some edge cases.
  • The value-textobj does not work with multi-line values.

Other Text Object Plugins

Credits

Thanks

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.

Blog
I also occasionally blog about vim: Nano Tips for Vim

Profiles

Buy Me a Coffee

Buy Me a Coffee at ko-fi.com

More Repositories

1

shimmering-obsidian

Alfred Workflow with dozens of features for controlling your Obsidian vault.
JavaScript
811
star
2

nvim-spider

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

shimmering-focus

Minimalistic Obsidian Theme for keyboard-centric users.
CSS
469
star
4

nvim-scissors

Automagical editing and creation of snippets.
Lua
327
star
5

nvim-recorder

Enhance the usage of macros in Neovim.
Lua
202
star
6

nvim-genghis

Lightweight and quick file operations without being a full-blown file manager.
Lua
195
star
7

nvim-early-retirement

Send buffers into early retirement by automatically closing them after x minutes of inactivity.
Lua
170
star
8

nvim-origami

Fold with relentless elegance.
Lua
160
star
9

nvim-tinygit

A lightweight bundle of commands focussed on swift and streamlined git operations.
Lua
143
star
10

alfred-bibtex-citation-picker

Citation picker & lightweight reference manager for BibTeX files, via Alfred.
JavaScript
138
star
11

nvim-rip-substitute

Perform search and replace operations in the current buffer using a modern user interface and contemporary regex syntax.
Lua
133
star
12

obsidian-smarter-md-hotkeys

A plugin for Obsidian providing hotkeys that select words and lines in a smart way before applying markup. Multiple cursors are supported as well.
TypeScript
132
star
13

nvim-kickstart-python

A launch point for your nvim configuration for Python
Lua
113
star
14

pandoc_alfred

Pandoc-Suite for Academic Writing in Markdown
JavaScript
98
star
15

obsidian-theme-design-utilities

Some Utilities and Quality-of-Life Features for Designers of Obsidian Themes
TypeScript
95
star
16

nvim-chainsaw

Speed up log creation. Create various kinds of language-specific log statements, such as logs of variables, assertions, or time-measuring.
Lua
92
star
17

finder-vim-mode

Feature-rich mouseless control of macOS Finder, inspired by vim/ranger.
Shell
81
star
18

.config

My personal dotfiles
Python
74
star
19

pdf-annotation-extractor-alfred

Alfred Workflow to extract annotations from PDF files.
JavaScript
72
star
20

nvim-rulebook

Add inline-comments to ignore rules, or lookup rule documentation online.
Lua
69
star
21

nvim-lsp-endhints

Display LSP inlay hints at the end of the line, rather than within the line.
Lua
66
star
22

nvim-puppeteer

Automatically convert strings to f-strings or template strings and back.
Lua
56
star
23

obsidian-divide-and-conquer

An Obsidian plugin that provides commands for bulk enabling/disabling of plugins. Useful for debugging when you have many plugins.
TypeScript
55
star
24

obsidian-quadro

Obsidian Plugin for social-scientific Qualitative Data Analysis (QDA). An open alternative to MAXQDA and atlas.ti, using Markdown to store data and research codes.
TypeScript
49
star
25

zsh-magic-dashboard

Pressing "enter" on an empty buffer displays an information-rich and pretty dashboard.
Shell
48
star
26

grappling-hook

Obsidian Plugin for blazingly fast file switching. For those who find the Quick Switcher still too slow.
TypeScript
44
star
27

nvim-alt-substitute

A substitute of vim's :substitute that uses lua patterns instead of vim regex. Supports incremental preview.
Lua
43
star
28

new-tab-default-page

Obsidian plugin to open a note of your choice when creating a new tab, like in the browser.
TypeScript
42
star
29

cmp-nerdfont

nvim-cmp source for nerdfont icons
Lua
40
star
30

alfred-neovim-utilities

Search neovim plugins and online :help via Alfred
JavaScript
34
star
31

cmp_yanky

cmp-source for clipboard history from yanky.nvim
Lua
34
star
32

nvim-dr-lsp

Status line component showing the number of LSP definition and reference of the token under the cursor.
Lua
27
star
33

alfred-docs-searches

Search more than two dozen official documentation sites via Alfred
JavaScript
25
star
34

twitter-workspace-for-drafts

Various capabilities for composing tweets in the Drafts app.
22
star
35

obsidian-sembr

Obsidian Plugin for Semantic Line Breaks
TypeScript
22
star
36

nvim-pseudometa-plugin-template

A template for new nvim plugins
Shell
22
star
37

alfred-reddit-browser

Browse your favorite subreddits (and hackernews) via Alfred.
JavaScript
20
star
38

obsidian-footnote-indicator

Indicates the presence of footnotes in the gutter and the Status bar
Shell
20
star
39

alfred-atop

System Monitoring and Process Management via Alfred
JavaScript
19
star
40

gitfred

Helpful GitHub Assistant for Alfred.
JavaScript
17
star
41

alfred-homebrew

Search, install, or uninstall homebrew packages conveniently via Alfred.
JavaScript
16
star
42

pdf-summarizer-alfred

Get summaries of your PDFs via ChatPDF.
Shell
14
star
43

hyper-seek

Alfred workflow that shows inline search results, without a keyword.
JavaScript
13
star
44

obsidian-extra-md-commands

Obsidian plugin that adds various commands, e.g. for __bold__ or <cite>.
TypeScript
12
star
45

alfred-writing-assistant

Autocorrection, synonym suggestions for the word under the cursor, and rephrasing of the selected text. All with one key press.
JavaScript
12
star
46

obsidian-sidebar-toggler

Finer control of the Obsidian sidebars. To be used with an external window manager.
TypeScript
11
star
47

wrench-knife

Collection of useful snippets for Hammerspoon
Lua
11
star
48

obsidian-nothing

An Obsidian plugin that adds a no-op command.
Shell
11
star
49

alfred-workflow-template

Shell
10
star
50

pseudometa-obsidian-plugin-template

A description for the plugin
JavaScript
9
star
51

pdf-annotation-extractor

Extracts Annotations from PDFs as well-formatted markdown
JavaScript
9
star
52

alfred-themes

A small collection of Alfred Themes I designed
8
star
53

alfred-pass

Alfred Client for the pass-cli
Shell
8
star
54

dotfiles-old

pseudometa's dotfiles
TeX
7
star
55

obsidian-smarter-paste

Improvements for when you paste things into Obsidian
TypeScript
7
star
56

alfred-wikipedia-suggest

Get in-line Wikipedia search suggestions
JavaScript
7
star
57

alfred-read-later

Simple standalone read-later-app for Alfred. Saves the items in plaintext on your device.
JavaScript
7
star
58

alfred-steam-companion

Alfred Workflow that interacts with Steam. Launches games directly, creates aliases, uninstalls games, and searches the Steam Store
6
star
59

drafts-snooze

Combination of settings, draft actions and shortcuts to enable the snoozing of drafts.
5
star
60

obsidian-task-statusbar

Obsidian plugin that displays the number of completed and total tasks in the status bar.
Shell
4
star
61

portfolio-performance-alfred

Searches your watchlists in the Portfolio Performance App for securities. Select one to copy its Name, Symbol, ISIN, or WKN to the clipboard.
JavaScript
4
star
62

alfred-anime-search

Search animes listed in the myanimelist database. Open entries at myanimelist or AniList.
JavaScript
3
star
63

tot-alfred

Tot.app Integration for Alfred.
JavaScript
3
star
64

chrisgrieser

2
star
65

axelrod-prisoner-dilemma

Recreation of the prisoner's dilemma model from Axelrod's "Evolution of Cooperation" in Python
Python
2
star
66

obsidian-personal-plugin

Personal Obsidian plugin, containing features customized to my workflow.
TypeScript
2
star
67

apuz-gender-analyzer

Analysis of the genders of the authors at the German journal "Aus Politik und Zeitgeschichte" (APuZ)
Python
2
star
68

quadro-example-vault

Example vault for Quadro, the QDA Plugin for Obsidian.
Makefile
2
star
69

alfred-reminders-companion

Display and edit reminders due today.
JavaScript
2
star
70

name-gender-analyzer

Makefile
1
star
71

test-repo

This repo is currently only used for testing GitHub Release Actions
Shell
1
star
72

nanotipsforvim-blog

source for the blog
Just
1
star
73

alfred-japanese-dictionary

Japanese-English Dictionary using jisho.org with audio, csv export of words, and preview of dictionary sites.
JavaScript
1
star
74

alfred-workflow-devtools

Various utilities and quality-of-life-features for Alfred workflow developers
Shell
1
star
75

alfred-dock-switcher

Switch between dock layouts.
Shell
1
star
76

alfred-quick-file-access

Quickly access recent files, files with a specific tag, files in the current window, files in the downloads folder, or trashed files.
JavaScript
1
star