DEV Community

Cover image for "Damn! Now I have to use Vim"
Richard Lenkovits
Richard Lenkovits

Posted on • Edited on

"Damn! Now I have to use Vim"

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.

What have I done

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.

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.


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



Enter fullscreen mode Exit fullscreen mode

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

plusone

Top comments (14)

Collapse
 
notsag profile image
Maxime Gaston

Hey Richard,

Nice beginners tutorial, I'm always glad when I see a Vim post 😉

Regarding recommending Vim with plugins as an IDE, I would say it depends.

I use Vim and a few plugins as an IDE, and would totally recommend it!
Although, I would not use it for everything.

The problem with vim mode/plugins/keymaps for any IDE is that it doesn't allow me to be completely free of using my mouse. And honestly, using hjkl to move and a few vim basic commands is mostly making it difficult for nothing IMO. If I have a graphical editor and ctl-F is available, why should I use / to search something?
So to me, it's the real Vim or nothing.

As a SRE (and not a dev), I spend most of my time in a terminal, runnning commands, writing scripts, ansible playbooks, kubernetes manifests...
Doing all that, I find it more productive for me to use Vim with plugins such as : YouCompleteMe, ansible-vim... and stay in my terminal.

But of course if I was working on a full website, desktop application... I would choose a full graphical IDE.

Collapse
 
pencillr profile image
Richard Lenkovits

Hi, thanks for the answer. Totally got your angle! I do both web development and DevOps, and I also spend a lot of time doing similar things, writing ansible playbooks bash/python/groovy scripts, configuring openstack, etc. If you spend much time in the terminal, vim is much more comfortable. It's fast and it's always around.

Collapse
 
lcoenen profile image
Loïc Coenen

I vim everyday in my 9-5, angular developer work and it's a nightmare. Not because of vim, because of Angular. You shouldn't need a gazplant-like IDE to develop, and I never have any issue with, say, React.

Collapse
 
intricatecloud profile image
Danny Perez

An former coworker of mine used to use the vim keyboard mapping in IntelliJ - I used to pair with him, but I would wind up accidentally locking myself out of edit mode when he would go to the restroom and I tried to use IntelliJ shortcuts when I forgot I was in vim mode. What a pain 😆

I also use a docker container called ubuntu-upstart in my local dev environment which I frequently SSH into. That container doesn't ship with vim, but it ships with vim.tiny which is good enough for doing 99% of things: i and :wq

Collapse
 
ssimontis profile image
Scott Simontis

My hidden gem is a seemingly obscure editor called Micro. I don't remember how I came across it, but it beats the crap out of Nano, has some pretty useful plugins, and is fairly powerful.

I'm making progress with Vim though...I no longer end up having to Google "how to save and quit vim" on a second computer when I finish my edits :P

Collapse
 
chaelgutierrez profile image
Michael Jyms Gutierrez

I really like vim. I'm also one of the developers who daily use vim on project. I use it on javascript development and etc. Using vim is a commitment 😅.

Collapse
 
tttfifo profile image
Todor Todorov

Hi Richard,

(disclaimer: vim lover here)

I agree with all said above, I even laughed at the HOW TO EXIT place :) I know these jokes very well, but I laugh every time :)

I use vim for IDE, also whenever ssh-ing somewhere I feel very comfortable with all machines having vim installed...

For me the ssh problem is: occasionally I ssh to some machine which has only vi (without the trailing m). Then I feel awkward :)

Collapse
 
strottos profile image
Steven Trotter

I personally use vim for practically everything I do and always have and the main reason is because it's simple and straightforward. I don't like IntelliJ or Eclipse because they make simple things easy and hard things hard for me at least, I find there's probably a way of doing it but it's hidden under layers of insanity. I understand vim really well and will always choose it over nearly anything else I suspect for that reason, with plugins it becomes every bit as good as an IDE.

Incidentally I did find debuggers were missing so I wrote my own. Admittedly I shouldnt have to but I now find I can literally do anything I might care about in an IDE.

Still, great post and I enjoyed reading it. Thanks.

Collapse
 
combinatorylogic profile image
combinatorylogic

Or just run emacs -nw if you don't have your X11 forwarding...

Collapse
 
jingxue profile image
Jing Xue

For people who have to use vim in a pinch, they can run "evim". It starts vim in an "easy mode" that works like a "normal editor".

Collapse
 
intricatecloud profile image
Danny Perez

I hadn't known about that - I just played with it on OSX using vim -y and couldn't figure out how to quit. Searching for "how to quit vim" is a lot easier than "how to quit evim".

I never knew I could turn vim into a point and click editor! 💥

Collapse
 
vlasales profile image
Vlastimil Pospichal

I use Vim for all projects, including compiling and running TDD tests. He's great.