Banner of boosting-latex-editing-with-custom-vim-mappings-9.jpeg

Boosting LaTeX Editing with Custom Vim Mappings


Category: vim

📅 November 23, 2025   |   👁️ Views: 1

Author:   mosaid

Introduction

As someone who writes mathematical documents regularly, I've always found LaTeX editing to be a constant dance between content creation and formatting struggles. The back-and-forth typing of verbose commands like \textbf{} and \underline{} constantly interrupted my creative flow. That's when I decided to supercharge my Vim setup with custom mappings specifically designed for LaTeX - and the results have been transformative for my productivity.

Basic Settings

Before diving into the fancy mappings, let's start with the foundation. I prefer 2-space indentation for LaTeX files as it strikes the perfect balance between readability and efficient use of screen space:

autocmd FileType tex setlocal tabstop=2 shiftwidth=2 expandtab

Compiling and Viewing LaTeX

One of the most common tasks in LaTeX editing is compiling and previewing. Instead of constantly switching to the terminal, I've created seamless workflows right within Vim.

Instant Compilation: Pressing <leader>c compiles the current file and shows me the output right in a Vim buffer - no context switching needed:

    
function! RunTex()
    let s:current_file = expand("%")
    enew | silent execute ".!xelatex " . shellescape(s:current_file, 1)
    setlocal buftype=nofile bufhidden=wipe noswapfile nowrap
    if &number == 0
        set number
    endif
    set relativenumber
    normal! G
    nnoremap <buffer> <CR> :bd!<CR>
endfunction
nnoremap <leader>c :call RunTex()<CR>
    

Quick PDF Preview: When I need to see the actual PDF, <leader>o opens it in Evince without leaving Vim:

    
function! ViewTex() abort
    let pdf = substitute(expand('%:p'), '\.tex$', '.pdf', '')
    silent! execute '!nohup evince ' . shellescape(pdf) . ' >/dev/null 2>&1 &'
    redraw!
endfunction
nnoremap <leader>o :call ViewTex()<CR>
    

Smart Insert Mode Shortcuts

These are the real workhorses of my LaTeX workflow - they let me insert common elements with minimal keystrokes while keeping me in the flow.

Mathematical Spacing: Instead of typing \quad every time, I just type qq in insert mode and it expands automatically.

Text Wrapping: Need text in a math environment? tt wraps my text with proper formatting and even positions my cursor inside the braces.

Formatting Commands: Bold and underline become second nature with ;bf for \textbf{} and ;u for \underline{}.

Template Loading: This is where the real magic happens. I have pre-written templates for common elements:

  • ;tcb inserts a beautiful tcolorbox environment with proper styling

  • ;ds loads my document structure template with title, author, and sections pre-filled

    
inoremap qq \quad
inoremap tt ~\text{}~<++><ESC>F{a
inoremap ;bf \textbf{}<++><ESC>F{a
inoremap ;u \underline{}<++><ESC>F{a
inoremap ;tcb <ESC>:r /home/mosaid/.vim/snippets/latex/tcb<CR>
inoremap ;ds <ESC>:0r /home/mosaid/.vim/snippets/latex/ds<CR>
    

Visual Mode Power Tools

When working with existing text, these visual mode mappings are absolute lifesavers:

Quick Formatting: Select text and use <leader>b for bold or <leader>u for underline - it wraps your selection perfectly.

Math Mode Magic: My personal favorites include:

  • 5 on selected text wraps it with math mode delimiters

  • 6 converts display math \[ \] to inline math $ $

  • 9 transforms \( \) math notation to the cleaner $ $ syntax

    
vnoremap <leader>$ s~$<C-r>"$~<++><Esc>
vnoremap <leader>u c\underline{<C-r>"}<++>
vnoremap <leader>b c\textbf{<C-r>"}<++>
vnoremap 5 c~$<C-r>"$~<Esc>
vnoremap 6 <Esc>:'<,'>s/\\\[/\~$/e<CR>:'<,'>s/\\\]/\$\~/e<CR>
vnoremap 9 <Esc>:'<,'>s/\\(/\~$/eg<CR>:'<,'>s/\\)/\$\~/eg<CR>
    

Mathematical Efficiency Boosters

These small but mighty mappings handle the repetitive mathematical notation that used to slow me down:

    
inoremap ;o $(O; \vec i, \vec j)$
inoremap $$ ~$$~<++><Esc>F$i
inoremap [ []<++><Esc>F]i
    

The ;o mapping is particularly useful for coordinate systems, while $$ and [ handle the common cases of creating math environments and brackets with proper cursor placement.

Conclusion

What started as a simple attempt to reduce typing has evolved into a comprehensive LaTeX editing system that feels almost like having a personal assistant. The beauty of these mappings isn't just in the time saved - it's in the uninterrupted flow state they enable. I no longer break my mathematical reasoning to remember LaTeX syntax; the commands have become extensions of my thought process. Whether you're a student writing homework, a researcher drafting papers, or anyone who frequently works with LaTeX, I highly recommend investing time in building your own Vim mappings - the productivity payoff is absolutely worth it.


← Automating Code Transformations in Vim with RunScriptsOnSelect