Richard Lenkovits
Posted on August 30, 2019
That awkward moment
Let's say you arrive to some cloud console shell, or you ssh
over to another host without the proper flags and end up having no X11 forwarding, which then results in a Can't open display
message when trying to run gedit. That's when you realize: You'll have to use Vim.
Trust me. We've all been there.
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
, 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 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 who's pretty much on the same page. People brought up a lot of good reasons there in the comments.
Vim won't make you a more productive developer
Mac Siri ・ Sep 5 '18
Long story short, a modern IDE or Text editor will help you boost your productivity. For bigger projects even I use VSCode.
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, here. It also covers saving, undo and redo, line cutting and so.
Vim for starters - the minimum you need to know
Hamza Tamenaoul ・ Dec 25 '17
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 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 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 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!
" 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!
Posted on August 30, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.