DEV Community

Cover image for My development environment setup (Part 2)
Jorge Ramón
Jorge Ramón

Posted on • Updated on

My development environment setup (Part 2)

In the last post I talked about:

  • Install essential tools
  • Install zsh and using it by default
  • Install Oh My Zsh
  • Install vim and why could be a good option for development

It's time for the part 2.

6. Configure vim

I know it's hard to learn and get used to vim but here is my secret 🤐.

Doing some research on internet about vim tips and configuration I found this: https://github.com/sohjiro/.vim

Yep, a shell script to setup vim and add some steroids.

Just run the follow command:

curl -L https://raw.githubusercontent.com/sohjiro/.vim/master/run_config.sh | sh
Enter fullscreen mode Exit fullscreen mode

And that's all 💪.

6.1. Fix 0x7A69 color error on vim (optional)

Now try to open vim (with the command vim of course).

For some reason it threw an error about a missed 0x7A69 color. It didn't happened to me before but if it happens to you too here is the solution.

Download the file 0x7A69_dark.vim from here:

https://www.vim.org/scripts/script.php?script_id=4695

And just do the following:

mkdir -p ~/.vim/colors

# <path> is the path where the 0x7A69_dark.vim file is located
mv <path>/0x7A69_dark.vim ~/.vim/colors

Enter fullscreen mode Exit fullscreen mode

And the next time you open vim it will be fixed.

6.2. Fix weird backspace character on vim (optional)

After solving the color error I started testing the keyboard and I found that when pressing backspace always got ^? character 😫.

What is that? I don't know but the way to solve this is editing the file .zshrc (remember we are using Zsh, right?) and add the next line at the bottom:

stty erase '^?'
Enter fullscreen mode Exit fullscreen mode

Solved 🔨.

7. Install vim plugins

The configuration you installed on step 6 had some built-in plugins.

To install them, open vim and type:

:PlugInstall
Enter fullscreen mode Exit fullscreen mode

Just wait until everything is installed:

8. Mastering vim: NerdTree is your friend

One of the plugins I told you on step 7 is NerdTree that will let you open the current directory, browse files, add/rename/remove/move/copy a file and more. Isn't that cool?.

To use it, open vim and type:

:NerdTreeToggle
Enter fullscreen mode Exit fullscreen mode

every time you need to open the current directory... kinda boring but the good thing is that you can map the command to a keyboard combination.

Edit the file ~/.vim/vim_config.vim and add the following line:

noremap <C-w>n :NERDTreeToggle<cr>
Enter fullscreen mode Exit fullscreen mode

Restart vim and now press Ctrl-w + n and you've got it. To close NerdTree just press the combination again.

For last, to do operations over a directory or a file (move, copy, delete, etc) open NerdTree, move your cursor to the directory or file you want and press m:

Select your desired option.

9. Mastering vim: Window splitting

Sometimes you need to open 2 or more files at the same time, so here comes window splitting to help.

Again, edit the file ~/.vim/vim_config.vim and add the following lines:

noremap <C-w>- :split<cr>
noremap <C-w>\| :vsplit<cr>
Enter fullscreen mode Exit fullscreen mode

Restart vim and now press Ctrl-w + - to split the current window horizontally:

And Ctrl-w + | (pipe character) to split the current window vertically:

To switch between panels, just press Ctrl+ w twice. To close a panel just press Ctrl-w + c.

Now, mix NerdTree and window splitting to open as many files as you want!

10. Mastering vim: Delete file buffers

Using Sublime Text, Visual Code or any IDE when you finished to work on some file you just close it. Well, this is not the same on vim, you can close your panel (Ctrl-w + c) but the buffer will be still there 😫. Fortunately I found a way to close the current buffer! 🤓

Create a new file on ~/.vim/plugin/bclose.vim (if the plugin directory doesn't exist, please create it too) and paste the following (credits to vim.wikia):

" Delete buffer while keeping window layout (don't close buffer's windows).
" Version 2008-11-18 from http://vim.wikia.com/wiki/VimTip165
if v:version < 700 || exists('loaded_bclose') || &cp
  finish
endif
let loaded_bclose = 1
if !exists('bclose_multiple')
  let bclose_multiple = 1
endif

" Display an error message.
function! s:Warn(msg)
  echohl ErrorMsg
  echomsg a:msg
  echohl NONE
endfunction

" Command ':Bclose' executes ':bd' to delete buffer in current window.
" The window will show the alternate buffer (Ctrl-^) if it exists,
" or the previous buffer (:bp), or a blank buffer if no previous.
" Command ':Bclose!' is the same, but executes ':bd!' (discard changes).
" An optional argument can specify which buffer to close (name or number).
function! s:Bclose(bang, buffer)
  if empty(a:buffer)
    let btarget = bufnr('%')
  elseif a:buffer =~ '^\d\+$'
    let btarget = bufnr(str2nr(a:buffer))
  else
    let btarget = bufnr(a:buffer)
  endif
  if btarget < 0
    call s:Warn('No matching buffer for '.a:buffer)
    return
  endif
  if empty(a:bang) && getbufvar(btarget, '&modified')
    call s:Warn('No write since last change for buffer '.btarget.' (use :Bclose!)')
    return
  endif
  " Numbers of windows that view target buffer which we will delete.
  let wnums = filter(range(1, winnr('$')), 'winbufnr(v:val) == btarget')
  if !g:bclose_multiple && len(wnums) > 1
    call s:Warn('Buffer is in multiple windows (use ":let bclose_multiple=1")')
    return
  endif
  let wcurrent = winnr()
  for w in wnums
    execute w.'wincmd w'
    let prevbuf = bufnr('#')
    if prevbuf > 0 && buflisted(prevbuf) && prevbuf != w
      buffer #
    else
      bprevious
    endif
    if btarget == bufnr('%')
      " Numbers of listed buffers which are not the target to be deleted.
      let blisted = filter(range(1, bufnr('$')), 'buflisted(v:val) && v:val != btarget')
      " Listed, not target, and not displayed.
      let bhidden = filter(copy(blisted), 'bufwinnr(v:val) < 0')
      " Take the first buffer, if any (could be more intelligent).
      let bjump = (bhidden + blisted + [-1])[0]
      if bjump > 0
        execute 'buffer '.bjump
      else
        execute 'enew'.a:bang
      endif
    endif
  endfor
  execute 'bdelete'.a:bang.' '.btarget
  execute wcurrent.'wincmd w'
endfunction
command! -bang -complete=buffer -nargs=? Bclose call s:Bclose('<bang>', '<args>')
nnoremap <silent> <Leader>bd :Bclose<CR>
Enter fullscreen mode Exit fullscreen mode

Then, edit the file ~/.vim/vim_config.vim and add the following line:

noremap <C-w>b :Bclose<cr>
Enter fullscreen mode Exit fullscreen mode

Restart vim, open any file:

Press Ctrl-w + b and voilá!

That's all for now, folks.

Feel free to ask any questions!

Thanks for reading ❤️

Top comments (1)