Enhancing Your Vim Workflow: Tips and Configurations
Published:
Vim is a powerful text editor that can revolutionize your workflow with its extensive customization options. In this blog post, we’ll explore some useful tips and configurations to enhance your Vim experience.
Searching in Vim
Efficient searching is crucial in any text editor. To optimize your search experience in Vim, consider making the following changes to your configuration:
set nohlsearch
set incsearch
set ignorecase " case insensitive searching
set smartcase " case-sensitive if expresson contains a capital letter
nnoremap n nzzzv
nnoremap N Nzzzv
" Adding Fuzzy search for files
set path+=**
" For creating a tag file using ctags. Works with tex and programfiles.
" For more details see man page of ctags.
command! MakeTags !ctags -R .
These settings ensure that search highlights are not persistent, enable incremental searching, and set case-insensitive searching as the default behavior. The smartcase option allows Vim to perform case-sensitive searches if the search pattern contains at least one capital letter. Additionally, the provided nnoremap commands center the screen after jumping to the next/previous match, improving visibility.
Making pdf’s using Latex
If you work with LaTeX, you can streamline the process of compiling LaTeX files and generating PDFs using Vim. Here’s how:
- Create a shell script named
makepdf.sh
with the following content:
#!/bin/bash
#Making pdf from anything.
file=$(readlink -f "$1")
dir=${file%/*}
base="${1%.*}"
cd "$dir" || exit 1
command="pdflatex"
( head -n5 "$file" | grep -qi 'xelatex' ) && command="xelatex"
( head -n1 "$file" | grep -qi 'handout' ) && $command --output-directory="$dir" --jobname="$base""_handout" "\PassOptionsToClass{handout}{beamer} \input{$base}" && bibtex "$base""_handout.aux"
$command --output-directory="$dir" "$base" &&
bibtex $base.aux
$command --output-directory="$dir" "$base"
( head -n1 "$file" | grep -qi 'handout' ) && $command --output-directory="$dir" --jobname="$base""_handout" "\PassOptionsToClass{handout}{beamer} \input{$base}"
This script checks if the file can be converted to PDF using LaTeX or XeLaTeX, depending on whether xelatex is mentioned in the first 5 lines of the document. It also handles the creation of separate PDFs for beamer handouts if specified.
- Add the following lines to your .vimrc file to create mappings for triggering the script:
autocmd FileType tex map <Leader>c :!~/scripts/makepdf.sh % <CR><CR>
autocmd FileType tex map <Leader>o :!zathura %:r.pdf & <CR><CR>
These mappings assign the <Leader>c
and <Leader>o
keys to compile the current LaTeX file and open the resulting PDF in the Zathura PDF viewer, respectively.
With these configurations, you can quickly compile your LaTeX files and preview the generated PDF without leaving Vim.
Utilizing Helpful Plugins
Vim plugins extend its functionality and allow you to tailor the editor to your specific needs. While it’s essential to be selective with the plugins you use, a few can significantly enhance your editing experience. Here’s an example setup using the popular plugin manager vim-plug and some recommended plugins:
"Plugins
call plug#begin('~/.vim/plugged')
Plug 'VundleVim/Vundle.vim'
Plug 'sirver/ultisnips'| Plug 'honza/vim-snippets'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-commentary'
Plug 'junegunn/vim-easy-align'
call plug#end()
The above configuration sets up vim-plug as your plugin manager and includes some useful plugins:
- UltiSnips and vim-snippets provide powerful snippet capabilities, allowing you to assign keystrokes for commands and custom text snippets. They come with predefined snippets for various languages, saving you time and effort.
- vim-surround simplifies working with surroundings (such as parentheses, quotes, HTML tags) by providing intuitive mappings to add, change, and delete them.
- vim-commentary allows you to comment out lines or visually selected blocks of code with ease, enhancing code readability and simplifying commenting tasks.
- vim-easy-align provides a straightforward way to align text and symbols within a line or across multiple lines, making your code more organized and visually appealing.
- Remember to choose plugins that align with your specific needs and avoid overloading Vim with unnecessary extensions.
Automating Workflow with Helpful Automatic Commands
In addition to the previously mentioned tips and configurations, there are some additional automatic commands that can further enhance your Vim workflow. These commands automate certain actions based on specific events. Let’s explore two examples:
- Remembering Folds:
augroup remember_fold
autocmd!
autocmd BufWinLeave * mkview
autocmd BufWinEnter * silent! loadview
augroup END
The above commands define an autocmd group named “remember_fold.” This group ensures that the state of your file’s folds (collapsed or expanded) is saved when you switch away from the buffer and restored when you return to it. This feature is especially useful when working with large files or projects where you frequently navigate between different buffers.
- Automatic Execution of Shell Script on Quit for Texfiles:
augroup autocom
autocmd!
autocmd VimLeave *.tex !~/scripts/texclear.sh %
autocmd VimLeave *.tex !~/scripts/texclear.sh %:r_handout.tex
augroup END
These commands define an autocmd group named “autocom.” It specifies that when you quit Vim while editing a .tex
file, the shell script texclear.sh
will be executed automatically to clear the build files associated with the LaTeX/XeLaTeX build. This cleanup ensures that unnecessary files generated during compilation are removed, keeping your project directory clean.
The texclear.sh
script is as follows:
#!/bin/sh
# Clears the build files of a LaTeX/XeLaTeX build.
# I have Vim run this file whenever I exit a .tex file.
case "$1" in
*.tex)
file=$(readlink -f "$1")
dir=$(dirname "$file")
base="${file%.*}"
find "$dir" -maxdepth 1 -type f -regextype gnu-awk -regex "^$base\\.(4tc|xref|tmp|pyc|pyo|fls|vrb|fdb_latexmk|bak|swp|aux|log|synctex\\(busy\\)|lof|lot|maf|idx|mtc|mtc0|nav|out|snm|toc|bcf|run\\.xml|synctex\\.gz|blg|bbl)" -delete ;;
*) printf "Give .tex file as an argument.\\n" ;;
esac
This script clears the unnecessary build files associated with a LaTeX/XeLaTeX compilation. When you exit Vim after editing a .tex
file, the script identifies the relevant build files based on the file’s name, removes them, and ensures a clean workspace for your next LaTeX project.
By incorporating these automatic commands, you can streamline your workflow and focus more on your content creation rather than manual housekeeping tasks.
Conclusion
Vim is a versatile text editor that can greatly improve your productivity when properly configured. By optimizing searching, automating LaTeX PDF generation, and utilizing helpful plugins, you can customize Vim to suit your workflow and make editing tasks more efficient.
Remember, these configurations are based on personal preferences and may require further adjustments to fit your exact needs. Feel free to experiment and adapt them to create your own unique .vimrc setup.
We hope these tips and configurations inspire you to explore Vim further and uncover its full potential for your text editing endeavors.