Efficient Vim Configuration and Plugin Management

Vim is a highly efficient text editor that supports extensive customization through plugins and configuration files. The primary configuration is handled via the .vimrc file located in the user's home directory (~/.vimrc). Below is a robust configuration setup that utilizes Vundle for plugin management, along with optimized settings for code development and navigation.

" Disable compatibility with Vi
set nocompatible
filetype off

" Configure Vundle runtime path
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" Core Plugins
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
Plugin 'jiangmiao/auto-pairs'
Plugin 'preservim/nerdtree'
Plugin 'Yggdroot/LeaderF'
Plugin 'majutsushi/tagbar'

call vundle#end()
filetype plugin indent on

" --- Environment Setup ---
let mapleader = ","       " Set leader key to comma
set number                " Show line numbers
set mouse=a               " Enable mouse support
set expandtab             " Use spaces instead of tabs
set tabstop=4             " Number of spaces per tab
set shiftwidth=4          " Indentation width
set autoindent            " Auto-indent new lines
set ignorecase            " Case-insensitive search
set smartcase             " Case-sensitive if uppercase present
set incsearch             " Incremental search
set hlsearch              " Highlight search results

" --- Window Navigation Keybindings ---
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

" --- File Type Detection ---
autocmd BufRead,BufNewFile *.{py} set filetype=python
autocmd BufRead,BufNewFile *.{js} set filetype=javascript
autocmd BufRead,BufNewFile *.{c,h} set filetype=c
autocmd BufRead,BufNewFile *.{cpp,hpp} set filetype=cpp
autocmd BufRead,BufNewFile *.{go} set filetype=go
autocmd BufRead,BufNewFile *.{sh} set filetype=sh
autocmd BufRead,BufNewFile *.{java} set filetype=java

" --- Automatic File Headers ---
augroup HeaderTemplates
    autocmd!
    autocmd BufNewFile *.{c,cpp,h,sh,py,java} call GenerateFileHeader()
augroup END

function! GenerateFileHeader()
    let l:filename = expand("%:t")
    let l:timestamp = strftime("%Y-%m-%d %H:%M")
    
    " Define header lines based on file type
    if &filetype == 'sh'
        call setline(1, "#!/bin/bash")
        call append(0, "# File: " . l:filename)
        call append(1, "# Author: Developer Name")
        call append(2, "# Created: " . l:timestamp)
        call append(3, "# Description: ")
    elseif &filetype == 'python'
        call setline(1, "#!/usr/bin/env python3")
        call append(0, "# -*- coding: utf-8 -*-")
        call append(1, "# File: " . l:filename)
        call append(2, "# Author: Developer Name")
        call append(3, "# Created: " . l:timestamp)
        call append(4, "")
        call append(5, "def main():")
        call append(6, "    pass")
        call append(7, "")
        call append(8, "if __name__ == '__main__':")
        call append(9, "    main()")
    else
        " C/C++/Java style comment block
        call setline(1, "/**")
        call append(0, " * File: " . l:filename)
        call append(1, " * Author: Developer Name")
        call append(2, " * Date: " . l:timestamp)
        call append(3, " */")
        
        " Add basic boilerplate for C/C++
        if &filetype == 'c' || &filetype == 'cpp'
            call append(line("$"), "")
            call append(line("$"), "#include <stdio.h>")
            call append(line("$"), "")
            call append(line("$"), "int main(int argc, char *argv[]) {")
            call append(line("$"), "    return 0;")
            call append(line("$"), "}")
        endif
    endif
    normal G
endfunction

" --- Plugin Configurations ---

" NERDTree Settings
nnoremap <silent> <F2> :NERDTreeToggle<CR>
let NERDTreeIgnore=['\.pyc$', '\.o$', '\.git$']

" Tagbar Settings
nmap <F8> :TagbarToggle<CR>
let g:tagbar_width = 30

" LeaderF Configuration
let g:Lf_ShortcutF = "<leader>f"
noremap <leader>b :Leaderf buffer<CR>
noremap <leader>m :Leaderf mru<CR>
let g:Lf_ShowRelativePath = 1
let g:Lf_HideHelp = 1

" YouCompleteMe Settings
let g:ycm_min_num_of_chars_for_completion = 2
let g:ycm_autoclose_preview_window_after_completion = 1
let g:ycm_complete_in_comments = 1
let g:ycm_collect_identifiers_from_tags_files = 1
let g:ycm_confirm_extra_conf = 0

" --- UI/Theme ---
syntax enable
set background=dark
colorscheme desert " Default dark theme, ensure installed or change to molokai

To install the plugins specified in the configuration, open Vim and execute the following command in command mode:

:PluginInstall

Conversely, to remove a plugin, simply delete or comment out the coresponding Plugin line in your .vimrc file and run:

:PluginClean

This will remove any plugins that are no longer listed in your configuration file.

Tags: Vim vimrc vundle configuration Linux

Posted on Thu, 28 May 2026 19:42:40 +0000 by wsantos