Set Up VIM as An IDE - Part 102 November, 2019written by Bill

Have you ever heard about Vim before? Many people say that it's a powerfull text editor. I started to use this tool couple months ago and i enjoy it. I'm not a hardcore Vim user that knows all commands or good setup, but i just want to share how do i setup my Vim.

The Basic Tweak

I assume you guys already know (at least) about how to use Vim, like navigation, searching, etc, you can googling "Vim cheat sheet" to get an overview of all the commands. Here is the very basic setup:

  set autoread                                                                " Set to auto read when a file is changed from the outside
  set backspace=indent,eol,start                                              " more powerful backspacing
  set clipboard=unnamed                                                       " access your system clipboard
  set cmdheight=2                                                             " Height of the command bar
  syntax enable                                                               " enable syntax highlighting and plugin (for netrw)
  set encoding=utf8                                                           " Set utf8 as standard encoding and en_US as the standard language
  set expandtab                                                               " convert tabs into spaces
  set ffs=unix,dos,mac                                                        " Use Unix as the standard file type
  set foldmethod=indent                                                       " Code folding
  set foldlevel=99
  set history=500                                                             " Sets how many lines of history VIM has to remember
  set incsearch                                                               " incremental search
  set laststatus=2                                                            " Always show the status line
  set list                                                                    " Show trailing white space
  set listchars=tab:>·,trail:~,extends:>,precedes:<,space:.                   " eol:¬,tab:>·,trail:~,extends:>,precedes:<,space:.
  set mouse=nicr
  set magic                                                                   " For regular expressions turn magic on
  set nocompatible                                                            " enter the current millenium
  set number                                                                  " always show line numbers
  set hidden
  set ruler                                                                   " Always show current position
  set scrolloff=3                                                             " when scrolling, keep cursor 3 lines away from screen border
  set shiftwidth=2                                                            " amount of block indenting
  set smarttab                                                                " uses the shiftwidth instead of tabstop to delete indented line
  set synmaxcol=200                                                           " performance ???
  set tabstop=2                                                               " press tab, 2 spaces forward, 1 tab == 2 spaces
  set wrap                                                                    " Wrap lines
  filetype plugin indent on

As you can see, there are a bunch of set command for each configuration settings, you can read the notes for each settings i set or you can check on Vim website for more details about the settings.

Try to copy and paste in your empty .vimrc file if you're on *nix system or _vimrc on Windows, then try re-open your vim and edit a file, you'll see (at least) there's color on the text and dots for each spaces. It actually does a lot than you think since i just mention about color and dots, that's why i write a note for each settings so you can read it :D.

Let's move to the next phase, we'll set some shortcuts to interact inside Vim.

  let mapleader = ","
  let maplocalleader = ","
  set termguicolors
  nnoremap <leader>N :setlocal number!<cr>            " Toggle line numbers

  " comma+s to save, comma+q to quit (does not save!), quit all without saving
  nnoremap <leader>ss :w<cr>
  nnoremap <leader>q :q!<cr>
  nnoremap <leader>qa :qa!<cr>

  let $MYVIMRC="/home/you_username/.vimrc"
  nnoremap <leader>rv :source<Space>$MYVIMRC<cr>       " Reload vimrc
  nnoremap <leader>ev :tabnew $MYVIMRC<cr>             " Edit vimrc

  " Copy & paste to clipboard
  noremap <Leader>Y "+y
  noremap <Leader>P "+p

  " change Escape key behaviour
  imap <leader>q <Esc>
  inoremap jj <Esc>

  nnoremap <leader> z                                  " Enable folding with the z

  " Buffer key mappings
  nnoremap <leader>l :bn<cr>
  nnoremap <leader>h :bp<cr>
  nnoremap <leader>0 :bf<cr>
  nnoremap <leader>9 :bl<cr>
  nnoremap <leader>dd :bd<cr>

  " Managing tabs
  nnoremap tn :tabnew<Space>
  nnoremap tk :tabnext<CR>
  nnoremap tj :tabprev<CR>
  nnoremap th :tabfirst<CR>
  nnoremap tl :tablast<CR>
  nnoremap tc :tabclose<CR>

  " :W sudo saves the file
  " (useful for handling the permission-denied error)
  command W w !sudo tee % > /dev/null

  " navigate split screens easily
  nmap <silent> <c-k> :wincmd k<CR>
  nmap <silent> <c-j> :wincmd j<CR>
  nmap <silent> <c-h> :wincmd h<CR>
  nmap <silent> <c-l> :wincmd l<CR>

  " Pressing Shift < or Shift > will let you indent/unident selected lines
  vnoremap < <gv
  vnoremap > >gv

  " comma-1 insert "!" commenting
  nnoremap <leader>1 :norm i!<cr>
  vnoremap <leader>1 :norm i!<cr>

  " comma-' insert """ commenting
  nnoremap <leader>' :norm i"<cr>
  vnoremap <leader>' :norm i"<cr>

  " comma-3 insert "#" commenting
  nnoremap <leader>3 :norm i#<cr>
  vnoremap <leader>3 :norm i#<cr>

  " comma-- insert "--" commenting
  nnoremap <leader>- :norm i--<cr>
  vnoremap <leader>- :norm i--<cr>

  " comma-6 uncomment
  nnoremap <leader>6 :norm ^x<cr>
  vnoremap <leader>6 :norm ^x<cr>

  " Make Y yank everything from the cursor to the end of the line. This makes Y
  " act more like C or D because by default, Y yanks the current line (i.e. the
  " same as yy).
  noremap Y y$

I set mapleader with commad ( , ), you can change it if you want. So, those shortcuts help me a lot to interact inside Vim, for example, i can press , + ev to directly edit my .vimrc file on new buffer and press , + ss to save a file. It's easy right? But you might want to use :verbose imap <your_key> to check is there a conflict for each key-mappings that has been set.

And the last phase for the basic setup is just a miscellaneous setup, you can read the notes for a little information.

  " Ignore compiled files
  set wildignore=*.o,*~,*.pyc
  if has("win16") || has("win32")
      set wildignore+=.git\*,.hg\*,.svn\*
  else
      set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.DS_Store
  endif

  " autocompletion of files and commands behaves like shell
  " (complete only the common part, list the options that match)
  set wildmode=list:longest

  " FINDING FILES: **********************************************************
  " search down into subfolders
  " provides tab-completion for all file-related tasks
  set path+=**

  " display all matching files when we tab complete
   set wildmenu
   set wildmode=list:longest,full
   set lazyredraw

  " NOW WE CAN:
  " - hit tab to :find by partial match
  " - use * to make it fuzzy
  " THINGS TO CONSIDER:
  " - :b lets you autocomplete any open buffer
  " END FINDING FILES: **********************************************************

  " FILE BROWSING: *********************************************************
  " tweaks for browsing
   let g:netrw_banner=0                                    " disable annoying banner
   let g:netrw_browse_split=4                              " open in prior window
   let g:netrw_altv=1                                      " open splits to the right
   let g:netrw_liststyle=3                                 " tree view
   let g:netrw_list_hide=netrw_gitignore#Hide()
   let g:netrw_list_hide.=',\(^\|\s\s\)\zs\.\S\+'

  " NOW WE CAN:
  " - :edit a folder to open a file browser
  " - <CR>/v/t to open in an h-split/v-split/tab
  " - check |netrw-browse-maps| for more mappings
  " END FILE BROWSING: *********************************************************

  " Enable 256 colors palette in Gnome Terminal
  if $COLORTERM == 'gnome-terminal'
      set t_Co=256
  endif

  set background=dark

  " Set extra options when running in GUI mode
  if has("gui_running")
      set guioptions-=T
      set guioptions-=e
      set t_Co=256
      set guitablabel=%M\ %t
      set cursorcolumn!

      " Set up the gui cursor to look nice
      set guicursor=n-v-c:block-Cursor-blinkon0
      set guicursor+=ve:ver35-Cursor
      set guicursor+=o:hor50-Cursor
      set guicursor+=i-ci:ver25-Cursor
      set guicursor+=r-cr:hor20-Cursor
      set guicursor+=sm:block-Cursor-blinkwait175-blinkoff150-blinkon175
  endif

  " better backup, swap and undos storage
  set directory=~/.vim/dirs/tmp     " directory to place swap files in
  set backup                        " make backup files
  set backupdir=~/.vim/dirs/backups " where to put backup files
  set undofile                      " persistent undos - undo after you re-open the file
  set undodir=~/.vim/dirs/undos
  set viminfo+=n~/.vim/dirs/viminfo

  " create needed directories if they don't exist
  if !isdirectory(&backupdir)
      call mkdir(&backupdir, "p")
  endif
  if !isdirectory(&directory)
      call mkdir(&directory, "p")
  endif
  if !isdirectory(&undodir)
      call mkdir(&undodir, "p")
  endif

This setup is actually not from my own experiment, but i do a lot of searching and pick one by one setup that i found and combine it. For now, let's stop here just for the basic setup, i'll continue on part 2 article for the complete setup.

I hope it is useful for you, and you can grab all phase configs from here.

vimtext editor

Reading Time6 min