"Damn! Now I have to use Vim"
Tags: bash, vim, devops, linux
Published: 2019-08-30 13:27:38

---
title: \"Damn! Now I have to use Vim\"
published: true
description: Thoughts on vim as a text editor and potential IDE
cover_image: https://thepracticaldev.s3.amazonaws.com/i/9y96205twrhxg3d740vp.jpg
tags: bash, vim, devops, linux
---

## That awkward moment
Let's say you arrive to some cloud console shell, or you `ssh` over to another host without the proper [flags](https://explainshell.com/explain?cmd=ssh+-Y) and end up having no [X11 forwarding](https://www.quora.com/What-is-X11-forwarding), which then results in a `Can't open display` message when trying to run [gedit](https://help.ubuntu.com/community/gedit). That's when you realize: You'll have to use [Vim](https://www.vim.org/).

*Trust me. We've all been there.*

![What have I done](https://thepracticaldev.s3.amazonaws.com/i/mczeauitn8gnubpysivt.gif)

## Not another Vim praising post
Don't get me wrong I actually *like* VIM!
My story with it is quite standard I think. When I was a physics undergrad, my first programming related classes were about `bash`, this ancient `matlab` clone [`octave`](https://www.gnu.org/software/octave/), and good old `C`.
The catch was that you were *expected to use the terminal for everything*, thus including Vim for text file editing. First it was a struggle, but I ended up becoming a Vim fan, and to this day I still use it for everyday work *(but not for every work!)*.

I must admit: it's also cool that Vim nowadays is like **arcane knowledge**, and it's fun when all your colleagues come to you to tell them how to do something like a search replace, or how to [EXIT IT](https://stackoverflow.blog/2017/05/23/stack-overflow-helping-one-million-developers-exit-vim/) whatsoever.

Still I'm not planning to convince anyone to use Vim for daily work, and there are many reasons for that. I recommend this thread from [Mac Siri](https://dev.to/maestromac) who's pretty much on the same page. People brought up a lot of good reasons there in the comments.
{% link maestromac/vim-wont-make-you-a-more-productive-developer-h9f %}

Long story short, a modern IDE or Text editor will help you boost your productivity. For bigger projects even I use [VSCode](https://code.visualstudio.com/).


## Still, there are those times
Anyhow, you can end up having no other option, or you just simply want to give Vim a try, here are some super simple basics you can start with.

Installation and things like navigating normal and insert mode are well covered in the post of [Hamza Tamenaoul](https://dev.to/hamza), here. It also covers saving, undo and redo, line cutting and so.
{% link hamza/vim-for-starters---the-minimum-you-need-to-know-3ob %}
Also, let me share a few more handy maneuvers from my toolbox, that are a bit beyond basics, but could also help.

#### Manipulating text blocks
**Visual mode** can help you manipulate blocks of texts.
Pressing `v` in normal mode gets you into visual mode.
Here you can select pieces of text by moving the cursor around. There are two sub-modes also, `Shift + v` is line mode (selecting only full lines), while `Ctrl + v` is block mode, which can help you select blocks starting at your current cursor position. This could really come in handy when you want to **remove the first or last few characters in several lines**.

#### Find and replace
Find-replace can be achieved by a vim command.
Commands can be entered after pressing `:` in normal mode.
There are many ways to do a [find-replace](https://vim.fandom.com/wiki/Search_and_replace) operation depending on what you exactly want, but here's a *general* solution that is good for almost any situations.
`%s/expression_to_replace/new_expression/gc`
The `g` option means that we're searching the whole text file, `c` means that we need confirmation for every replace. This way you'll have to press `y` or `n` for every replace operation, which makes it much safer.

#### Plugins and other ways of heresy
With the use of [vim plugins](https://vimawesome.com/) you can turn vim into a fully functional IDE adding code completion, linting and what not. But I wouldn't recommend that.

It's a personal opinion, but I believe that if you want an IDE but also Vim-like features, you better download any modern IDE or text editor and set a Vim [keymap extension](https://code.visualstudio.com/docs/getstarted/keybindings) on it. Vim is a great vanilla tool for basic editing work on a terminal. I better like it that way.

#### The dessert: my `.vimrc`
You can place a `.vimrc` file in your home directory which can help you set default options and configuration for every vim session. Here are some basic settings I always apply for my sessions, I hope it will serve you well too!

```vim
" Insert 4 spaces for a tab
set tabstop=4
" To change the number of space characters inserted
" for indentation with :retab
set shiftwidth=4
" To insert space characters whenever the tab key is pressed
set expandtab

" Color trailing whitespace and tabs
highlight ExtraWhitespace ctermbg=red guibg=red
au ColorScheme * highlight ExtraWhitespace guibg=red
au BufEnter * match ExtraWhitespace /\s\+$\|\t/
au InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$\|\t/
au InsertLeave * match ExtraWhiteSpace /\s\+$\|\t/

" Map Rmw command to remove unwanted whitespaces
command Rmw :%s/\s\+$//e

" Make backspace work like most other apps, if it doesn't do so
set backspace=2

" Show line numbers :set nonu for disabling
set number

" Display indentation guides
" If the indentation characters (·, ») do not appear right that
"   means you don't have latin character encoding.
"   You can use others characters, or you can set your encoding 
"   with: :set encoding=latin1
set list listchars=tab:»·,trail:·,extends:»,precedes:«

" Tab navigation like Firefox, when you have
"   more tabs open with :tabe command
nnoremap <C-Left> :tabprevious<CR>
nnoremap <C-Right> :tabnext<CR>

" Visual autocomplete for command menu
set wildmenu

" Highlight matching braces like [{()}]
set showmatch

" Highlight all search matches
set hlsearch

syntax on

```

I hope you've found something to take away. Have a nice day!

![plusone](https://thepracticaldev.s3.amazonaws.com/i/ypvzafveynm5q1njmrs3.gif)