My development environment setup (Part 2)
Tags: vim, nerdtree, zsh
Published: 2018-09-26 00:39:40

---
title: My development environment setup (Part 2)
published: true
description: How to get a good Linux development environment setup
tags: vim,NerdTree,zsh
cover_image: http://inet.blog/wp-content/uploads/2018/06/Ubuntu-Linux-18.04-LTS-1024x576.jpg
---

In the [last post](https://dev.to/jorge_rockr/my-development-environment-setup-part-1-4a33) 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:

```bash
curl -L https://raw.githubusercontent.com/sohjiro/.vim/master/run_config.sh | sh
```

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:

```bash
mkdir -p ~/.vim/colors

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

```

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:

```bash
stty erase '^?'
```

Solved 🔨.

## 7. Install vim plugins

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

To install them, open _vim_ and type: 

```vim
:PlugInstall
```

Just wait until everything is installed:

![](https://thepracticaldev.s3.amazonaws.com/i/p98quqngb9bv8ru52bkm.png)

## 8. Mastering vim: NerdTree is your friend

One of the plugins I told you on step 7 is [NerdTree](https://github.com/scrooloose/nerdtree) that will let you open the current directory, browse files, add/rename/remove/move/copy a file and more. Isn't that cool?.

![](https://thepracticaldev.s3.amazonaws.com/i/9tag38pxlfyfvqulmk5k.png)

To use it, open _vim_ and type:

```vim
:NerdTreeToggle
```

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:

```vim
noremap <C-w>n :NERDTreeToggle<cr>
```
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`:

![](https://thepracticaldev.s3.amazonaws.com/i/071l7d3kosglh8gakn2v.png)

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:

```vim
noremap <C-w>- :split<cr>
noremap <C-w>\| :vsplit<cr>
```

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

![](https://thepracticaldev.s3.amazonaws.com/i/jwyew58k9ojt9m16e1pn.png)

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

![](https://thepracticaldev.s3.amazonaws.com/i/sk2y62efchsj15quad0g.png)

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!

![](https://thepracticaldev.s3.amazonaws.com/i/oigfmnpr4keskv91a143.png)

## 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](http://vim.wikia.com/wiki/Deleting_a_buffer_without_closing_the_window)):

```vim
" 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>
```
Then, edit the file `~/.vim/vim_config.vim` and add the following line:

```vim
noremap <C-w>b :Bclose<cr>
```

Restart _vim_, open any file:

![](https://thepracticaldev.s3.amazonaws.com/i/5j2yv9r8vj5be1nboc4z.png)

Press `Ctrl-w + b` and **voilá**!

![](https://thepracticaldev.s3.amazonaws.com/i/69fspgbha3ch0myp0p1y.png)

That's all for now, folks.

Feel free to ask any questions!

Thanks for reading ❤️