/ linux

Using Ansible to Set Up Vim

In this article, I'm going to set up my Vim editor using the awesomeness of Janus. This requires ruby and a few other things to be in place, but in my opinion it is entirely worth it. You could spend a long time tweaking and configuring vim yourself, or you can skip to the front of the line and take all these presets, conventions and plugins for granted.

This follows in a series of Ansible for Development Environment articles:

Also please check out the source code for the latest changes and issue tracking: https://github.com/ballPointPenguin/ansible-develop

Without further ado, here is my playlist, ~/ansible-develop/vim.yml:

---
- name: Set Up Vim
  hosts: localhost
  become_user: "{{ username }}"
  tasks:
    - name: install vim and requirements
      become: True
      become_user: root
      package: name={{ item }}
      with_items:
        - ack
        - ctags
        - ruby
        - rubygem-rake
        - vim

    - name: check if .vim exists
      stat: path=/home/{{ username }}/.vim
      register: vim_dir

    - name: check if .vim is a git repo
      when: vim_dir.stat.exists
      stat: path=/home/{{ username }}/.vim/.git
      register: vim_git

    - name: backup .vim if it is not a repo
      when: (vim_dir.stat.exists and vim_git.stat.isdir is not defined)
      command: mv /home/{{ username }}/.vim /home/{{ username }}/.vim.backup

    - name: ensure the ~/src-hub directory is present
      file: path=/home/{{ username }}/src-hub
            state=directory

    - name: clone janus
      git: repo=https://github.com/carlhuda/janus.git
           dest=/home/{{ username }}/src-hub/janus
           update=no

    - name: create symlink to janus
      file: path=/home/{{ username }}/.vim
            src=/home/{{ username }}/src-hub/janus
            state=link

    - name: rake janus
      shell: cd /home/{{ username }}/.vim && rake

    - name: copy vimrc.after to user home
      copy: src=vimrc.after
            dest=/home/{{ username }}/.vimrc.after
            force=no

    - name: ensure the .janus directory is present
      file: path=/home/{{ username }}/.janus
            state=directory

    - name: clone vim-colorschemes submodule
      shell: cd /home/{{ username }}/.janus && git init && git submodule add
             https://github.com/flazz/vim-colorschemes.git colorschemes
             creates=/home/{{ username }}/.janus/colorschemes

This setup requires a template for vimrc.after, put in ~/ansible-develop/files/vimrc.after:

set exrc
set secure
set t_Co=256

" Color Scheme!

colorscheme 256-jungle
"colorscheme Chasing_Logic
"colorscheme OceanicNext
"colorscheme solarized
"colorscheme vydark

" Disable auto-comment insertion
autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o


" Syntax Checking
syntax enable

" check on save
let g:syntastic_mode_map = { "mode": "active" }

" Don't show error location list by default
let g:syntastic_auto_loc_list = 0

" Use rubocop
let g:syntastic_ruby_checkers = ['rubocop']

" User EditorConfig
let g:EditorConfig_exclude_patterns = ['fugitive://.*']

" Use eslint
let g:syntastic_javascript_checkers = ['eslint']


" Set line length coloring
execute "set colorcolumn=81"
highlight ColorColumn guibg=#194550

With those files in place and the {{ username }} variable set in ~/ansible-develop/host_vars/localhost.yml we can run the playlist:

ansible-playlist -K vim.yml

Now you can tweak and modify your ~/.vimrc.after files to your liking, and add plugins as submodules to your ~/.janus directory. Try some different colorschemes!

After I got that working, I went ahead and updated my master playlist ~/ansible-develop/localhost.yml:

---
- name: Run all the local playbooks
  hosts: localhost

- import_playbook: packages.yml
- import_playbook: docker.yml
- import_playbook: vim.yml
- import_playbook: zsh.yml

Next playlist: Node.js (and NVM)

Benjamin Rosas

Benjamin Rosas

I am a constantly curious programmer with a background in science. I enjoy open-source software, programming languages, web development and server administration, along with music, travel and snark.

Read More
Using Ansible to Set Up Vim
Share this