(Should you) Get started with vim
Tags: vim, linux, beginners
Published: 2022-06-30 20:08:50


> NOTE: This blog might discuss ideas which might not exactly align with you. But at the end of the day, that’s all they are, my choices. I’m always open to discussion and learning something new though.

## It’s all about editing text

The world of text editors is very hostile from what I’ve experienced so far. You might’ve heard discussions surrounding what to use — vim, emacs, notepad, VSCode and all the other choices. I enjoyed using VSCode during my time in college. I still remember it was something new that had come out then and I enjoyed using it a lot. So why am I writing about vim now? 

## Why did I choose vim?

It was in a CS Lab in college when I first ran into `vi`. I was on a Fedora Machine and a `git commit` put me into vim  (`Ctrl + C :wq`is how you quit vim by the way). For some reason, the cryptic UI was fun to use. Ever since then, I have enjoyed it. Well, it’s improved version `vim`.

On the terminal, I’m usually running something close to Ubuntu. Whether it’s inside a container, a pod running in Kubernetes, my home server (a Raspberry Pi 4 running Ubuntu Server), my personal machine on Windows running WSL. And even though Ubuntu’s default editor is nano, I find it easy to just run a `sudo apt update -y && sudo apt install -y vim`

## So should you? Use vim?

Short answer, yeah, why not? Give it a try.

Long answer. Yes, but you might not like it right away. And it’ll take a time to get used to the shortcuts. So, let’s see how to get started and take the plunge.

## Getting started with vim

When I got serious to get into vim, I didn’t realise how less ***clear*** guides were there to get started. Albeit, I was looking for how to use vim as a code editor and not a general text editor. But let’s be real, vim doesn’t really make sense as a general text editor for me. 

### The absolute absolute basics

- Opening a file in vim — `vim <path-to-file>`
- Tap `i` to insert text
- Now write whatever you want
- To exit, `Ctrl C :wq`

### Plugin Manager

So, the first thing I started to look for is a package manager, or a plugin manager as it’s in the vim world. Plugins add a lot of extra features. I ended up using [Vundle](https://github.com/VundleVim/Vundle.vim).

To set it up, it’s just a single command — 

```bash
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
```

### .vimrc

Just like everything else in the shell, vim has its own rc file which we can use to customize it to our needs. Because vim is all about getting to my development environment as fast as possible, I have put my .vimrc file up on github — [https://github.com/cli-config/cli-config/blob/main/profiles/mrsauravsahu/.vimrc](https://github.com/cli-config/cli-config/blob/main/profiles/mrsauravsahu/.vimrc)

### Tabs v spaces

I like to use spaces because it is consistent across machines & I use 2 spaces. That’s what the following snippet does (it should also be in .vimrc)

```bash
" use spaces instead of tabs
set tabstop=2 shiftwidth=2 expandtab
```

### Time for some plugins

Because I have Vundle setup now, I can install some plugins. Here’s the entire `.vimrc` 

```bash
syntax on

set number

" use spaces instead of tabs
set tabstop=2 shiftwidth=2 expandtab

" git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'bling/vim-airline'
Plugin 'prabirshrestha/vim-lsp'
Plugin 'mattn/vim-lsp-settings'
Plugin 'prabirshrestha/asyncomplete.vim'
Plugin 'prabirshrestha/asyncomplete-lsp.vim'
Plugin 'junegunn/fzf'
Plugin 'junegunn/fzf.vim'
Plugin 'ledesmablt/vim-run'
Plugin 'dense-analysis/ale'
Plugin 'hashivim/vim-terraform'
call vundle#end()

" show spaces
set listchars=trail:·,lead:·
set list

" SHORTCUTS

" vim-run
nnoremap <silent> <C-x> :Run

" formatting
nnoremap <silent> <C-k><C-d> :gg=G <CR>

" fzf.vim
let fzf_default_command='find -L'
nnoremap <silent> <C-l> :Files <CR>
nnoremap <silent> <C-p> :GFiles <CR>

" UNUSED PLUGINS -- for now
" Plugin 'scrooloose/nerdtree'
" nnoremap <leader>n :NERDTreeFocus<CR>
" nnoremap <C-n> :NERDTree<CR>
" nnoremap <C-e> :NERDTreeToggle<CR>
" nnoremap <C-f> :NERDTreeFind<CR>
```

### Brief about Modes

vim uses modal editing. Keys behave differently in different modes. The modes are - normal, insert, command.

- Normal - Just viewing the file and basically navigating through a file
- Insert - You’re inserting/editing text in the file
- Command - Shortcuts you can execute to help your workflow (most of these are defined, and you can create your own too)

### Shortcuts

> Note: Few of these shortcuts depend on the plugins I’ve setup.
> 

Keyboard shortcuts are something that’ll become a key reason you’ll keep using vim. I discovered few of these by accident and some by searching online. (shown in the `.vimrc` above)

- `Ctrl P` - When writing stuff (in insert mode) - this shows you suggestions.
- `Ctrl P` - When in normal mode - this will show you files in the current directory’s git repo - Fails if not in a git repo. This helps to not pollute the list with ignored files in git.
- `Ctrl L` - When in normal mode - this shows all files in the current directory
- `gg` - Go to top of the file
- `Shift G` - Go to end of file
- `$` - Go to end of current line
- `0` - Go to beginning of current line

### Commands

> Note: Few of these commands depend on the plugins I’ve setup.
> 

Let’s see some commands that can help your workflow.

- `:<line-number>` - Go to line
- `:! <command>` - Run a command in your `$SHELL`
- `:sh` - Put vim process in background and go back to your shell. `exit` the shell to get back to `vim`

## Conclusion

Clearly, vim has a lot of things going for it but also comes with a pretty steep learning curve. It might fit your needs or you might learn some new things in the shell. I’ve been having fun and trying to learn vim by using it on a daily basis.

I’ll be updating this article as I learn about new plugins, shortcuts and commands. Make sure you bookmark it.

And yeah, give vim a try.

-S