Neovim and LaTeX with LazyVim, part 3: custom commands

This is a follow-up to thefirst post on Neovim and LaTeX andthe second one.

If you write LaTeX in Neovim using theLazyVimdistribution, you already get a fantastic editing experience thanks to VimTeX and texlab. But there’s one thing that often trips people up:how do you quickly wrap selected text in\emph{…},\textbf{…}, or other LaTeX commands?

The answer ismini.surround— and with a small buffer-local configuration, it becomes a LaTeX wrapping powerhouse.


What mini.surround Gives You (Out of the Box)

LazyVim ships with themini.surroundextra, which you can enable in yourlazy.lua(or wherever you manage extras):

Once enabled, you get operations like:

KeymapAction
gsaAdd surrounding
gsdDelete surrounding
gsrReplace surrounding
gsfFind surrounding

For example, visual-select a word and pressgsa"to wrap it in double quotes, orgsa)for parentheses. Great — but what about LaTeX-specific commands?


The Problem with LaTeX Commands

VimTeX gives you text objects, motions, completion, and much more — but it doesn’t ship a built-in “surround this selection with\emph{…}” feature. That’s wheremini.surround‘scustom surroundingscome in.

We can teachmini.surroundto understand LaTeX commands by defining custom surroundings in a filetype-specific Lua file.


Setting Up Custom LaTeX Surroundings

Create (or edit) the file~/.config/nvim/after/ftplugin/tex.lua. This file is automatically sourced by Neovim whenever you open a.texfile.

Why ftplugin?Files in this directory are loadedafterall plugin configuration, and only for the matching filetype. This means our settings are buffer-local and don’t interfere with any other filetype.


How It Works

Thecmd_surroundhelper function does two things:

  1. input: A Lua pattern that matches\cmd{...}so thatgsdandgsrknow what tofind and remove.
  2. output: The left (\cmd{) and right (}) strings used whenaddinga surrounding.

We then assign short, memorable single-character IDs to each command.


Using the Surroundings

Here’s what you can do in any.texbuffer:

Adding a Surrounding

Visual-select some text, then:

KeymapResult
gsae\emph{selected text}
gsab\textbf{selected text}
gsai\textit{selected text}
gsat\texttt{selected text}
gsau\underline{selected text}
gsas\textsc{selected text}
gsaq(add opening and closing LaTeX quotations, i.e., two backticks and ”)

You can also usegsain normal mode with a motion:gsaeiwwraps the current word in\emph{…}.

Deleting a Surrounding

Place your cursor inside a command and press:

KeymapAction
gsdeRemove\emph{…}, leaving the inner text
gsdbRemove\textbf{…}
gsdiRemove\textit{…}

Replacing a Surrounding

Want to change\emph{…}to\textbf{…}? No problem:

That’sgsr(replace surrounding), thene(find\emph{…}), thenb(replace with\textbf{…}).


A Practical Example

Suppose you have this text:

You visual-select “very important” and pressgsab. You get:

Now you decide that emphasis is better. With the cursor inside the\textbf{…}, pressgsrbe:

And if you want to remove the markup entirely:gsde:


Extending with Your Own Commands

The pattern is easy to extend. Just add more entries tocfg.custom_surroundings:

Pick single characters that are easy to remember and don’t conflict with built-in mini.surround ids (avoid(,),[,],{,},',",`).


A Note on Limitations

Theinputpatterns use Lua’s pattern matching, whichcannot handle nested braces. For example,\emph{foo {bar} baz}may not be matched correctly bygsdorgsr. For the vast majority of LaTeX writing, this is not an issue — but it’s worth keeping in mind if you work heavily with nested commands.


Happy LaTeX writing! 🎓

2 thoughts on “Neovim and LaTeX with LazyVim, part 3: custom commands”

Leave a Reply

This site uses Akismet to reduce spam.Learn how your comment data is processed.