sponsor Vim development Vim logo Vim Book Ad

basic Tip #274: Some useful mappings for TeX

 tip karma   Rating 0/0, Viewed by 1499 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   July 6, 2002 17:49      complexity:   basic
author:   Georgi Slavchev goyko@gbg.bg      as of Vim:   6.0

You know, TeX requires a lot of additional formatting code. I'm tired of opening and closing braces, brakets,
\beginning and \ending etc. I particularly hate typing \begin and \end.
To help myself and to save a few(not a few) keystrokes I naturaly came up to some solutions, which I wish to share with
other TeXnicians and TeXperts whhich use Vim.
"===============================cut here=========================
"=============== you can put it in ~/.vim/after/ftplugin/tex.vim ===============
" Note: i_<C-L>
" This constructs a skeleton of a TeX environment.
" You write a line like this:
" floatingfigure:ht<C-L>
" and after you press <C-L>, you get:
" \begin[ht]{floatingfigure}
" \end{floatingfigure}
" -- INSERT --
" where floatingfigure is the desired environment
" ht are options
" : is delimiter; in fact, you can use whatever delimiter you want
" as long it is not in &iskeyword option.
inoremap <buffer> <C-L> 
inoremap <buffer> { {}i
inoremap <buffer> [ []i
inoremap <buffer> ^ ^{}i
inoremap <buffer> _ _{}i
inoremap <buffer> \( \(\)hi
inoremap <buffer> \[ \[\]hi

" Note: v_<C-L>
" For this to work, you have to write on a blank line the name of
" the desired environment and options (see i_<C-L>) and visual select
" (from top to bottom) this and following lines.
" After pressing <C-L> the selected lines will be surrounded
" with begin/end skeleton of the environment.
vnoremap <buffer> <C-L> o
" vnoremap <buffer> { di{}P
" vnoremap <buffer> [ di[]P
vnoremap <buffer>  di^{}P
vnoremap <buffer>  di_{}P
vnoremap <buffer> \( di\(\)hP
vnoremap <buffer> \[ di\[\]hP

" This makes "two spaces after a comma" before every :write
au BufWritePre *.tex %s/,\(\S\)/, \1/ge

"==================== You can put this in your ~/.vimrc ========================
" If cursor is inside braces and not before comma, blank or opening brace,
" exit the brace block and stay in insert mode.
" If cursor is outside braces, it inserts a space or perform an abbreviation
" as normal.
function! CleverSpace()
let CharOnCursor = strpart( getline('.'), col('.')-2, 1)
let CharAfterCursor = strpart( getline('.'), col('.'), 1)
if CharOnCursor !~ ',\|\s\|(' && CharAfterCursor =~ ')\|]\|}'
normal x
inoremap <Space> <Space>:call CleverSpace()<LF>a

" I use the last function not only for LaTeX but also in C sources.

 rate this tip  Life Changing Helpful Unfulfilling 

<< Fast fixing of email quotations (too long lines) | Some useful mappings for TeX >>

Additional Notes

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge.net Logo