Vimrc

Vimrc is the config file for Vim. Customizations in the config file to make using Vim a little nicer. The vimrc file can be found at ~/.vimrc.

Starting Fresh[4]

If things aren't working how you like, don't be afraid to start over with a fresh install by renaming your original .vimrc file and your .vim directory to .vimrc-bak and .vim-bak respectively, and creating a new blank file and empty directory in their place.

Basics

Command Effect
syntax on Turn on syntax highlighting
set number Use line numbers
set number relativenumber Use current line number and relative numbers for others

Remapping[12]

Remapping is where you can expand one keystroke into another keystroke or set of keystrokes. There are multiple types of remapping:

An example that would be in a vimrc file:

map j gg           (moves cursor to first line)
map Q j            (moves cursor to first line)
noremap W j        (moves cursor down one line)

The effect of this would be that:

Modes

map and noremap have different modes they can apply to:

An example being nnoremap will make a non-recursive mapping that only applied to normal mode.

Custom Commands[5]

A command is used in the command mode, which is accessed through :. A custom command is made using this format:

command[!] [CapitalizedCustomCommand] [vim command that will be run]

For example, if I wanted to create a custom command that would edit my global todo file, I could write command TODO e ~/.tod. This will execute e ~/.tod when I write :TODO while in command mode in Vim.

Clipboard

Vim will use it's own unnamed register unless specified to use the system clipboard. You can tell it to use the system clipboard on yank and paste by prefacing either with "+.[1]

To make Vim always use the system clipboard, you can add this to your ~/.vimrc file[1,2]:

# Non-Mac
set clipboard=unnamedplus
# Mac
set clipboard=unnamed

Language Specific Options[7,8]

You can set language specific options within the ~/.vim/ftplugin/ folder. For instance, a Python options file would be the file ~/.vim/ftplugin/python.vim. If the filetype is not initially recognized by Vim, you can add it to the ftdetect folder[11].

If you want to associate one format with another language, you can use the following to associate them:

" Make sure all types of *.txt are rendered with markdown syntax
autocmd BufNewFile,BufRead *.txt set ft=markdown

Trim Trailing Whitespace on Save[3]

Before writing the buffer to file on any file (*), search and replace all whitespace characters (\s) at the end of a given line with nothing. Do not show any error messages if no matches exist (e flag).

" Trim trailing whitespace on save
autocmd BufWritePre * :%s/\s\+$//e

Other Plugins and Packages

References

  1. https://vim.fandom.com/wiki/Accessing_the_system_clipboard
  2. https://stackoverflow.com/questions/17561706/vim-yank-does-not-seem-to-work
  3. https://howchoo.com/vim/vim-how-to-remove-trailing-whitespace-on-save
  4. https://stackoverflow.com/questions/8824942/how-can-i-reset-vim-back-to-a-plain-vanilla-install
  5. https://yewtu.be/watch?v=Jm0IjtDAWcs
  6. https://github.com/amix/vimrc
  7. https://stackoverflow.com/questions/11023194/automatically-wrap-long-git-commit-messages-in-vim
  8. https://www.gilesorr.com/blog/vim-ftplugin.html
  9. https://thoughtbot.com/blog/writing-vim-syntax-plugins
  10. https://vim.fandom.com/wiki/Keep_your_vimrc_file_clean
  11. http://vimdoc.sourceforge.net/htmldoc/filetype.html#ftdetect
  12. https://stackoverflow.com/questions/3776117/what-is-the-difference-between-the-remap-noremap-nnoremap-and-vnoremap-mapping
  13. https://github.com/tpope
  14. https://nickjanetakis.com/blog/using-vims-autocmd-to-set-a-custom-file-type-for-specific-files
  15. https://tuckerchapman.com/2020/05/09/vimrc_organization/

Last modified: 202401040446