Exploring VIM configurations

vim.png

Below are a few configurations of your vimrc

Essential .vimrc configuration items

For whatever reason, the following options are not set by default, but they should be.

  1. Turn on hiddenDon’t worry about the name. What this does is allow Vim to manage multiple buffers effectively.
    • The current buffer can be put to the background without writing to disk;
    • When a background buffer becomes current again, marks and undo-history are remembered.

    Turn this on.

    set hidden
  2. Remap ` to 'These are very similar keys. Typing 'a will jump to the line in the current file marked with ma. However, `a will jump to the line and column marked with ma.It is more useful in any case I can imagine, but it is located way off in the corner of the keyboard. The best way to handle this is just to swap them:
    nnoremap ' `
    nnoremap ` '
  3. Map leader to ,The leader character is your own personal modifier key, as g is Vim’s modifier key (when compared to vi). The default leader is \, but this is not located standardly on all keyboards and requires a pinky stretch in any case.
    let mapleader = ","

    <SPACE> is also a good choice. Note: you can of course have several “personal modifier keys” simply by mapping a sequence, but the leader key is handled more formally.

  4. Keep a longer historyBy default, Vim only remembers the last 20 commands and search patterns entered. It’s nice to boost this up:
    set history=1000
  5. Enable extended % matchingThe % key will switch between opening and closing brackets. By sourcing matchit.vim, it can also switch among e.g. if/elsif/else/end, between opening and closing XML tags, and more.
    runtime macros/matchit.vim

    Note: runtime is the same as source except that the path is relative to the Vim installation directory.

  6. Make file/command completion usefulBy default, pressing <TAB> in command mode will choose the first possible completion with no indication of how many others there might be. The following configuration lets you see what your other options are:
    set wildmenu

    To have the completion behave similarly to a shell, i.e. complete only up to the point of ambiguity (while still showing you what your options are), also add the following:

    set wildmode=list:longest

Recommended .vimrc configuration items

Most people like these.

  1. Use case-smart searchingThese two options, when set together, will make /-style searches case-sensitive only if there is a capital letter in the search expression. *-style searches continue to be consistently case-sensitive.
    set ignorecase
    set smartcase

    This is usually the most useful combination.

  2. Set the terminal titleA running gvim will always have a window title, but when vim is run within an xterm, by default it inherits the terminal’s current title.
    set title

    This gives e.g. | page.html (~) - VIM |.

  3. Maintain more context around the cursorWhen the cursor is moved outside the viewport of the current window, the buffer is scrolled by a single line. Setting the option below will start the scrolling three lines before the border, keeping more context around where you’re working.
    set scrolloff=3

    Typing zz is also handy; it centers the window on the cursor without moving the cursor. (But watch out for ZZ!)

  4. Store temporary files in a central spotSwap files and backups are annoying but can save you a lot of trouble. Rather than spreading them all around your filesystem, isolate them to a single directory:
    set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
    set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp

    This is especially valuable after an unexpected reboot — you don’t have to track down all the leftover temp files. However: if you are editing files on a shared file system, it will be easier to clobber concurrent modifications, as other users’ Vim processes processes will not see your swaps.

  5. Scroll the viewport faster<C-e> and <C-y> scroll the viewport a single line. I like to speed this up:
    nnoremap <C-e> 3<C-e>
    nnoremap <C-y> 3<C-y>
  6. Enable limited line numberingIt’s often useful to know where you are in a buffer, but full line numbering is distracting. Setting the option below is a good compromise:
    set ruler

    Now in the bottom right corner of the status line there will be something like: 529, 35 68%, representing line 529, column 35, about 68% of the way to the end.

  7. A bunch of stuff your OS should already doIf you are running Windows or OS X or a sloppy Linux distribution, you may not be using these:
    " Intuitive backspacing in insert mode
    set backspace=indent,eol,start
    
    " File-type highlighting and configuration.
    " Run :filetype (without args) to see what you may have
    " to turn on yourself, or just set them all to be sure.
    syntax on
    filetype on
    filetype plugin on
    filetype indent on
    
    " Highlight search terms...
    set hlsearch
    set incsearch " ...dynamically as they are typed.

    The filetype lines enable type-specific configuration, such as knowledge of syntax and indentation. E.g. foo.c will be opened with Vim’s pre-configured C settings, and bar.py will be opened with Python settings.

    If the search term highlighting gets annoying, set a key to switch it off temporarily:

    nmap <silent> <leader>n :silent :nohlsearch<CR>
  8. Catch trailing whitespaceThe following will make tabs and trailing spaces visible when requested:
    set listchars=tab:>-,trail:·,eol:$
    nmap <silent> <leader>s :set nolist!<CR>

    By default whitespace will be hidden, but now it can be toggled with ,s.

  9. Stifle many interruptive promptsThe “Press ENTER or type command to continue” prompt is jarring and usually unnecessary. You can shorten command-line text and other info tokens with, e.g.:
    set shortmess=atI

    See :help shortmess for the breakdown of what this changes. You can also pare things down further if you like.

  10. Stop distracting your co-workersVim is a little surly, beeping at you at every chance. You can either find a way to turn off the bell completely, or more usefully, make the bell visual:
    set visualbell

    Instead of emitting an obnoxious noise, the window will flash very briefly. This is similar to screen’s interpretation of the bell in its default configuration.

Source

Vim Wiki

Related Posts

Comments are closed.