Skip to content

Instantly share code, notes, and snippets.

@huijunchen9260
Last active December 25, 2021 15:39
Show Gist options
  • Save huijunchen9260/91831f54466dd19517ab0e2fe70f52a4 to your computer and use it in GitHub Desktop.
Save huijunchen9260/91831f54466dd19517ab0e2fe70f52a4 to your computer and use it in GitHub Desktop.
Snippet in pure vim
let g:snippet_dir = $HOME . "/.config/nvim/vimsnip/"
source $HOME/.config/nvim/vimsnip/*.vim
"""""""""""""""""""""""""""""""
" Jump Forward and Backward "
"""""""""""""""""""""""""""""""
""" mimic multi cursor
""""" if search find <-->, copy the content in {}
""""" and paste in where <--> is.
""""" for label, replace all spaces in the content inside {}
""""" with - and paste in where <==> is.
""" Jump forward: delete template
inoremap <expr> ;j
\ search('<-->', 'cwn') > 0 ?
\ '<Esc>yi{/<--><CR>"_da<P?<++><CR>"_ca<' :
\ search('<==>', 'cwn') > 0 ?
\ '<Esc>yi{/<==><CR>"_da<P:s/\(\w\) /\1_/g<CR>/<++><CR>"_ca<' :
\ '<Esc>/<+.*+><CR>"_ca<'
nnoremap ;j /<+.*+><CR>"_ca<
""" Jump forward: apply template
inoremap <expr> ;;j
\ search('<-->', 'cwn') > 0 ?
\ '<Esc>yi{/<--><CR>"_da<P?<++><CR>"_ca<' :
\ search('<==>', 'cwn') > 0 ?
\ '<Esc>yi{/<==><CR>"_da<P:s/\(\w\) /\1_/g<CR>/<++><CR>"_ca<' :
\ '<Esc>/<+.*+><CR>"_d2lk/+><CR>"_d2l'
nnoremap ;;j /<+.*+><CR>"_d2lk/+><CR>"_d2l
""" Jump Backward
inoremap ;k <++><Esc>`.a
nnoremap ;k i<++><Esc>`.a
let g:snippet_dir_tex = g:snippet_dir . "tex/"
"""""""""""""""""""""""""""""""""""""""""""
" Manual trigger: snippet from template "
"""""""""""""""""""""""""""""""""""""""""""
""" Each part in a template call:
""" x<BS><ESC>: delibrately type an addition 'x' and delete it to maintain indentation from autoindent
""" <ESC>: return normal mode
""" :exe: execute the following command
""" read . g:snippet_dir_tex . : read the template
""" dV: delete the visual mode
""" ']: to the last yanked character
""" ]P: paste at current line to respect indentation
""" /<++><CR>"_ca<: search for and delete the first placeholder, turn to insert mode
""" Basic template
autocmd FileType tex inoremap template<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "template.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap beamer<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "beamer.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap frame<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "frame.tex"<CR>dV']]P/<++><CR>"_ca<
""" Environment
autocmd FileType tex inoremap be<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "beginend.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap column<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "column.tex"<CR>dV']]P/<++><CR>"_ca<
""" Listing
autocmd FileType tex inoremap item<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "itemize.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap enum<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "enumerate.tex"<CR>dV']]P/<++><CR>"_ca<
""" Sections
autocmd FileType tex inoremap sec<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "section.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap sec*<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "section_no_num.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap sub<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "subsection.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap sub*<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "subsection_no_num.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap ssub<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "subsubsection.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap ssub*<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "subsubsection_no_num.tex"<CR>dV']]P/<++><CR>"_ca<
""" Equations
autocmd FileType tex inoremap eq<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "equation_no_num.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap eqn<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "equation.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap al<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "align_no_num.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap aln<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "align.tex"<CR>dV']]P/<++><CR>"_ca<
autocmd FileType tex inoremap sp<tab> x<BS><ESC>:exe "-1read " . g:snippet_dir_tex . "split.tex"<CR>dV']]P/<++><CR>"_ca<
"""""""""""""""""""""""""""""""""
" Manual trigger from mapping "
"""""""""""""""""""""""""""""""""
""" Preamble
autocmd FileType tex inoremap pac<tab> \usepackage{}<++><Esc>T{i
""" Listing
autocmd FileType tex inoremap li<tab> <CR>\item<Space>
""" Text attributes
autocmd FileType tex inoremap bf<tab> \textbf{}<++><Esc>T{i
autocmd FileType tex vnoremap bf<tab> <ESC>`<i\textbf{<ESC>`>8la}<++><Esc>F}i
autocmd FileType tex inoremap it<tab> \textit{}<++><Esc>T{i
autocmd FileType tex vnoremap it<tab> <ESC>`<i\textit{<ESC>`>8la}<++><Esc>F}i
autocmd FileType tex inoremap tt<tab> \texttt{}<++><Esc>T{i
autocmd FileType tex vnoremap tt<tab> <ESC>`<i\texttt{<ESC>`>8la}<++><Esc>F}i
autocmd FileType tex inoremap em<tab> \emph{}<++><Esc>T{i
""" Math attributes
autocmd FileType tex inoremap cal<tab> \mathcal{}<++><Esc>T{i
autocmd FileType tex vnoremap cal<tab> <ESC>`<i\mathcal{<ESC>`>9la}<++><Esc>F}i
autocmd FileType tex inoremap fr<tab> \mathfrac{}<++><Esc>T{i
autocmd FileType tex vnoremap fr<tab> <ESC>`<i\mathfrac{<ESC>`>10la}<++><Esc>F}i
autocmd FileType tex inoremap scr<tab> \mathscr{}<++><Esc>T{i
autocmd FileType tex vnoremap scr<tab> <ESC>`<i\mathscr{<ESC>`>9la}<++><Esc>F}i
autocmd FileType tex inoremap mbf<tab> \mathbf{}<++><Esc>T{i
autocmd FileType tex vnoremap mbf<tab> <ESC>`<i\mathbf{<ESC>`>8la}<++><Esc>F}i
""" References
autocmd FileType tex inoremap ref<tab> \ref{}<++><Esc>T{i
autocmd FileType tex inoremap eqref<tab> \eqref{}<++><Esc>T{i
""" Citation
autocmd FileType tex inoremap cite<tab> \cite{}<++><Esc>T{i
autocmd FileType tex inoremap ct<tab> \textcite{}<++><Esc>T{i
autocmd FileType tex inoremap cp<tab> \parencite{}<++><Esc>T{i
" autocmd FileType tex vnoremap <tab>,, <ESC>`<i\{<ESC>`>2la}<ESC>?\\{<CR>a
""" Math
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Top Decoration "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Explanation of the following command:
""""" getline("."): get the content in current line
""""" col("."): the column of next cursor
""""" getline(".")[col(".") - 2] == ' ':
""""""" if the content of last character from cursor is space
""""" getline(".")[0:col(".") - 2] =~? '^.*\\[a-zA-Z]*$':
""""""" if the content from beginning of the line
""""""" to the last char from cursor matches \[a-zA-Z]*
" Usage:
"""""""""""""""""""""""""""""""""""""""
" word {cur} -> word \hat{}<++> "
" word{cur} -> \hat{word{cur}<++> "
" \word{cur} -> \hat{\word{cur}<++> "
"""""""""""""""""""""""""""""""""""""""
"""""""""
" Hat "
"""""""""
autocmd FileType tex inoremap <expr> ^<tab>
\ getline(".")[col(".") - 2] == ' ' ?
\ '\hat{}<++><Esc>T{i' :
\ getline(".")[0:col(".") - 2] =~? '^.*\\[a-zA-Z]*$' ?
\ '<ESC>F\i\hat{<ESC>2ea}<++><Esc>F}i' :
\ '<ESC>wbi\hat{<ESC>ea}<++><Esc>F}i'
autocmd FileType tex inoremap <expr> ^w<tab>
\ getline(".")[col(".") - 2] == ' ' ?
\ '\widehat{}<++><Esc>T{i' :
\ getline(".")[0:col(".") - 2] =~? '^.*\\[a-zA-Z]*$' ?
\ '<ESC>F\i\widehat{<ESC>2ea}<++><Esc>F}i' :
\ '<ESC>wbi\widehat{<ESC>ea}<++><Esc>F}i'
autocmd FileType tex vnoremap ^<tab> <ESC>`<i\hat{<ESC>`>5la}<++><Esc>F}i
autocmd FileType tex vnoremap ^w<tab> <ESC>`<i\widehat{<ESC>`>9la}<++><Esc>F}i
"""""""""""
" Tilde "
"""""""""""
autocmd FileType tex inoremap <expr> ^~<tab>
\ getline(".")[col(".") - 2] == ' ' ?
\ '\tilde{}<++><Esc>T{i' :
\ getline(".")[0:col(".") - 2] =~? '^.*\\[a-zA-Z]*$' ?
\ '<ESC>F\i\tilde{<ESC>2ea}<++><Esc>F}i' :
\ '<ESC>wbi\tilde{<ESC>ea}<++><Esc>F}i'
autocmd FileType tex inoremap <expr> ^~w<tab>
\ getline(".")[col(".") - 2] == ' ' ?
\ '\widetilde{}<++><Esc>T{i' :
\ getline(".")[0:col(".") - 2] =~? '^.*\\[a-zA-Z]*$' ?
\ '<ESC>F\i\widetilde{<ESC>2ea}<++><Esc>F}i' :
\ '<ESC>wbi\widetilde{<ESC>ea}<++><Esc>F}i'
autocmd FileType tex vnoremap ^~<tab> <ESC>`<i\tilde{<ESC>`>5la}<++><Esc>F}i
autocmd FileType tex vnoremap ^~w<tab> <ESC>`<i\widetilde{<ESC>`>9la}<++><Esc>F}i
""""""""""""""""""""
" Bar / Overline "
""""""""""""""""""""
autocmd FileType tex inoremap <expr> ^-<tab>
\ getline(".")[col(".") - 2] == ' ' ?
\ '\bar{}<++><Esc>T{i' :
\ getline(".")[0:col(".") - 2] =~? '^.*\\[a-zA-Z]*$' ?
\ '<ESC>F\i\bar{<ESC>2ea}<++><Esc>F}i' :
\ '<ESC>wbi\bar{<ESC>ea}<++><Esc>F}i'
autocmd FileType tex inoremap <expr> ^-w<tab>
\ getline(".")[col(".") - 2] == ' ' ?
\ '\overline{}<++><Esc>T{i' :
\ getline(".")[0:col(".") - 2] =~? '^.*\\[a-zA-Z]*$' ?
\ '<ESC>F\i\overline{<ESC>2ea}<++><Esc>F}i' :
\ '<ESC>wbi\overline{<ESC>ea}<++><Esc>F}i'
autocmd FileType tex vnoremap ^-<tab> <ESC>`<i\bar{<ESC>`>5la}<++><Esc>F}i
autocmd FileType tex vnoremap ^-w<tab> <ESC>`<i\overline{<ESC>`>9la}<++><Esc>F}i
"""""""""
" Dot "
"""""""""
autocmd FileType tex inoremap <expr> ^.<tab>
\ getline(".")[col(".") - 2] == ' ' ?
\ '\dot{}<++><Esc>T{i' :
\ getline(".")[0:col(".") - 2] =~? '^.*\\[a-zA-Z]*$' ?
\ '<ESC>F\i\dot{<ESC>2ea}<++><Esc>F}i' :
\ '<ESC>wbi\dot{<ESC>ea}<++><Esc>F}i'
autocmd FileType tex vnoremap ^.<tab> <ESC>`<i\dot{<ESC>`>5la}<++><Esc>F}i
autocmd FileType tex inoremap <expr> ^-><tab>
\ getline(".")[col(".") - 2] == ' ' ?
\ '\vec{}<++><Esc>T{i' :
\ getline(".")[0:col(".") - 2] =~? '^.*\\[a-zA-Z]*$' ?
\ '<ESC>F\i\vec{<ESC>2ea}<++><Esc>F}i' :
\ '<ESC>wbi\vec{<ESC>ea}<++><Esc>F}i'
autocmd FileType tex vnoremap ^-><tab> <ESC>`<i\vec{<ESC>`>5la}<++><Esc>F}i
""""""""""""""""""
" Math Symbols "
""""""""""""""""""
autocmd FileType tex inoremap -><tab> \rightarrow<space>
autocmd FileType tex inoremap =><tab> \Rightarrow<space>
autocmd FileType tex inoremap <-<tab> \leftarrow<space>
autocmd FileType tex inoremap <=<tab> \Leftarrow<space>
autocmd FileType tex inoremap <-><tab> \leftrightarrow<space>
autocmd FileType tex inoremap <=><tab> \Leftrightarrow<space>
autocmd FileType tex inoremap ^\|<tab> \uparrow<space>
autocmd FileType tex inoremap v\|<tab> \downarrow<space>
autocmd FileType tex inoremap \|-><tab> \mapsto<space>
autocmd FileType tex inoremap =<<tab> \le<space>
autocmd FileType tex inoremap >=<tab> \ge<space>
autocmd FileType tex inoremap AA<tab> \forall<space>
autocmd FileType tex inoremap EE<tab> \exist<space>
autocmd FileType tex inoremap inn<tab> \in<space>
""""""""""""""""""""""""""""""""""""
" Automatic trigger from mapping "
""""""""""""""""""""""""""""""""""""
""" Math and Table
autocmd FileType tex inoremap !! $ $<++><ESC>F$hi
autocmd FileType tex inoremap $$ <CR>$<CR><CR>$<++><ESC>F$ki<tab>
autocmd FileType tex inoremap // \frac{}<++><Esc>T{i
autocmd FileType tex inoremap __ _{}<++><Esc>T{i
autocmd FileType tex inoremap ^^ ^{}<++><Esc>T{i
autocmd FileType tex inoremap _^ _{}^{<++>}<++><Esc>2T{i
""""" Table and Equation aligning
""" Usage:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" <num>&& -> <num> lines of & with proper indentation "
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Explanation of the following command:
""""" ma: mark as a
""""" getline(".")[col(".") - 2]: previous char from the cursor
""""" <num>a<tab>& <++><CR><BS>: repeat character with proper indentation
""""" k0f&o<BS>//<CR><++><ESC>: type out //<CR><++>
""""" `a: go back to mark a (where <num> is)
""""" V`]>gv adjust indentation except for the first line
""""" `ar :replace <num> with space
""""" /<+.*+><CR>"_ca<: go to placeholder
autocmd FileType tex inoremap <expr> &&
\ getline(".")[col(".") - 2] =~? '[0-9]' ?
\ '<ESC>ma' . getline(".")[col(".") - 2] . 'a<tab>& <++><CR><BS><ESC>k0f&o<BS>\\<CR><++><ESC>`ajV`]>gv<ESC>`ar /<+.*+><CR>"_ca<' :
\ '<tab>& <CR><BS>\\<CR><++><ESC>2kA'
autocmd FileType tex inoremap <expr> &=
\ getline(".")[col(".") - 2] =~? '[0-9]' ?
\ '<ESC>ma' . getline(".")[col(".") - 2] . 'a<tab>&= <++><CR><BS><ESC>k0f&o<BS>\\<CR><++><ESC>`ajV`]>gv<ESC>`ar /<+.*+><CR>"_ca<' :
\ '<tab>&= <CR><BS>\\<CR><++><ESC>2kA'
""" command formation
autocmd FileType tex inoremap @@ <ESC>bi\<ESC>ea{}<++><ESC>F}i
autocmd FileType tex inoremap \\ <ESC>bi\<ESC>A
""" parentheses / bracket
autocmd FileType tex inoremap () ( )<ESC>hi
autocmd FileType tex inoremap [] [ ]<ESC>hi
autocmd FileType tex inoremap {} \{ \}<ESC>2hi
autocmd FileType tex inoremap (( \left(<CR>\right)<ESC>O<tab>
autocmd FileType tex inoremap [[ \left[<CR>\right]<ESC>O<tab>
autocmd FileType tex inoremap {{ \left\{<CR>\right\}<ESC>O<tab>
""" dots
autocmd FileType tex inoremap .... \ldots<space>
autocmd FileType tex inoremap c.. \cdots<space>
autocmd FileType tex inoremap v.. \vdots<space>
autocmd FileType tex inoremap \.. \ddots<space>
""" Greek
autocmd FileType tex inoremap ,a \alpha<space>
autocmd FileType tex inoremap ,b \beta<space>
autocmd FileType tex inoremap ,g \gamma<space>
autocmd FileType tex inoremap ,G \Gamma<space>
autocmd FileType tex inoremap ,d \delta<space>
autocmd FileType tex inoremap ,D \Delta<space>
autocmd FileType tex inoremap ,e \epsilon<space>
autocmd FileType tex inoremap ,z \zeta<space>
autocmd FileType tex inoremap ,h \eta<space>
autocmd FileType tex inoremap ,th \theta<space>
autocmd FileType tex inoremap ,Th \Theta<space>
autocmd FileType tex inoremap ,i \iota<space>
autocmd FileType tex inoremap ,k \kappa<space>
autocmd FileType tex inoremap ,l \lambda<space>
autocmd FileType tex inoremap ,L \Lambda<space>
autocmd FileType tex inoremap ,m \mu<space>
autocmd FileType tex inoremap ,n \nu<space>
autocmd FileType tex inoremap ,x \xi<space>
autocmd FileType tex inoremap ,X \Xi<space>
autocmd FileType tex inoremap ,pi \pi<space>
autocmd FileType tex inoremap ,Pi \Pi<space>
autocmd FileType tex inoremap ,rh \rho<space>
autocmd FileType tex inoremap ,s \sigma<space>
autocmd FileType tex inoremap ,S \Sigma<space>
autocmd FileType tex inoremap ,ta \tau<space>
autocmd FileType tex inoremap ,up \upsilon<space>
autocmd FileType tex inoremap ,Up \Upsilon<space>
autocmd FileType tex inoremap ,ph \phi<space>
autocmd FileType tex inoremap ,Ph \Phi<space>
autocmd FileType tex inoremap ,ch \chi<space>
autocmd FileType tex inoremap ,ps \psi<space>
autocmd FileType tex inoremap ,Ps \Psi<space>
autocmd FileType tex inoremap ,o \omega<space>
autocmd FileType tex inoremap ,O \Omega<space>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment