TLDR; VIM is hard but learning the basics will pay of quickly. Drop nano today and be more effective tomorrow. If you have some familiarity with cli interfaces I promise you will be more effective within just one hour.
A word of warning at the beginning: You won’t learn VIM by reading and you won’t get the real benefits unless you try it yourself! Dozens of people will tell you it’s awesome, but unless you experience it yourself, you won’t know what I or anybody else advocating VIM is really talking about.
In my previous post I was comparing my setup from when I started out to now and said I would discuss some of the changes. Changing my shell based texteditor from nano to vim is one of my most recent changes. The experience is very fresh for me. It was not my first try to do this but two years ago I failed missearbly. This time I did things differntly and I want to share my learnings with you. This blog post is about motivating you to start using VIM as your shell based editor and targeted at novices.
Some background and why this matters to me (and maybe to you):
I do a lot of my work with kaldi speech recognition toolkit. From a user’s perspective it consist mostly of shell scripts that tie together a lot of C++ binaries. As speech recognition needs a lot if computing power this is mostly done on remote systems. So when experimenting and trying things out, you have to change a couple of values in a shell script every now and then, maybe add some new condtions or a simple flag/value you want to pass from the shell, nothing to complicated really. So I did either use nano or vscode over ssh to do those edits. Both came with their own issues and advantages.
my evaluation/ observations of using nano
- quick - easy - not very powerfull - hard to undo stuff (not impossible as I thought, but do you know that key binding?) - no real support for tabs/spaces - search not very powerfull - code navigation rather slow - no coding support(completin, brace matching... ) - works for minor edits, no added benefit in using it
my evaluation/ observations of using vscode over ssh
- almost a full blown IDE, very powerfull - permissions not instantly visable - not very flexible with the directory mounting - starts remote process - slow (connect ssh, mount location, open file, edit, close, go back to the shell, run) - does not work on all remote systems - if you have to do more, surely a good option, but for the occasional change, that does not include huge refactorings, the whole process seems a bit complicated
My overalll experience using nano over the years, was a bad one. I had real meltdowns with nano, as changing seemed easy and intuitive but everything just went to shit the moment I used nano. It just didn’t support me enough. I had to concentrate harder than I should have to with a minor change as it did not offer any support.
So Please do yourself a favor and stop using nano now!
I will not argue to drop your IDE and switch to VIM altogether!
Don’t drop your IDE just yet. I won’t argue that. Without massive configuration, work and effort VIM just won’t be a viable alternative to a full blown IDE. And let’s be honest, just editing your code faster, won’t make you a better programmer or make you think faster. Usually typing and editing is not the limiting factor when it comes to programming (unless you are in a hacker movie ;)). I don’t think there really is a point to be made in that direction. But maybe switching keybindings to the VIM bindings will help you to become more efficient and never again need context switching and remembering different keybindings.
Why is VIM hard?
VIM is different than other editors. It does not work like you would expect a texteditor to work. It is hard, because it has modes which is very uncommon for most software today. The modes are:
- normal mode
- insert mode
- visual mode
To edit text you have to switch from normal mode to edit mode. To navigate or process text based on commands, you have to be in normal mode. For copy and pasting you need visual mode. As soon as you wrap your head around this concept it should be relatively easy to figure out. Don’t let yourself be demotivated by VIM being “hard”, you probably are just not used to the usage concept. You’re probably smart and had to learn a lot of hard things like coding to even be reading this.
Top reasons not to use VIM:
you never use the shell
Why are you even here? Do you know how computers work? ;)
nano is so easy…
Yes, nano is easy. But it’s not powerful. Ever tried to undo something? Nano is just like your regular code editor, but worse. You can’t use your mouse properly and besides inserting text it does not have any extra benefit. So better just use textmate, sublime, vscode or notepad++. Shortcuts are bad and copy/pasting/undoing stuff is complicated. You won’t improve the longer you’re using nano (or most other basic texteditors for that matter). After a couple of hours using it your learning will plateau. There is nothing new to gain or learn.
I don’t now how to start, vim is so complicated…
Yes, it’s complicated and I agree that most people who accidently open a file with vim will have trouble exiting it, but you’re not most people.
VIM seems like a monster at first if you don’t know how to get startet and just try to use it right away withouth knowing about the basics. But I promise even if you are just an occasional shell user (once / twice a week) you will be more effective and save a lot of time compared to nano. For this you will only need about one hour and an open mind. Before starting this little experiment, put your initial or maybe long groomed objection to vim aside and you will probably have a very similar experience than I had and never look back.
VIM is not intuitive in the way that it does not function like your typical word processor, but it’s intuitive in a sense, that when you know about the basics, the usage concept becomes evident and you will start learning as you go. Because many of the concepts are foreign to most users at first, there is an easy programm to get startet: vimtutor.
Benefits within only one hour time investment
- Learning about mode based systems if you haven’t already.
- faster edits, with a focus on changeing code, than using nano
- more secure edits
- edit support (e.g. navigating matching braces using % )
- fast, mousefree code navigation
- precise, search and replace with sed like regex
What convinced me to stick with it and learn more about it after vimtutor:
1. Precise sed like search and replace
What convinced me the most was the regex style find and replace that you can use the way you use sed. You can limit it to a specific range of lines and use it as “interactive sed”. e.g. change all occurances of a variable between lines 10 and 20 in your file.
press : type: 10,20s/my_old_var/my_new_var_name/g
2. Code Navigation
To be able to properly use a computer with keyboard and mouse you actually need three hands. There is constant context change between editing and using the mouse. I find this sometimes distracting and rather anoying. VIM does not need a mouse. You use command + motion to get where you want to go or type the line number right away. No need to switch to the mouse. Also marking parts of the code is supported very well and even in ways, your mouse is usually not capable of, which brings my to my next point.
3. Visual Block Mode
Multiline edits at multiple coursers is easy with blockmode. Selecting “blocks” of code/text is also something you often need, but end up doing by hand, as keybindings for many editors are complicated. Often people don’t even know about this kind of feature.
Have a complicated edit for a line you want to repeat at the next?
Just use a macro! You start by hitting
q + [a-z] and after that all edits are recorded.
When you’re finished, just hit
Navigate to the next line/paragraph and replay the same comand sequence by pressing
This is immensly powerful and easy to apply!
5. Multitab/ Multiwindow
In your window based operating system you’re used to windowing/ multiwindow etc. When using the shell there are a couple of methods for this, e.g. tmux. But VIM gives a good opportunity to not have to leave the current editwindow and open previews of files side by side easily from the same navigation.
How to get started
Remember that VIM has modes, a usage concept that is almost completly missing in modern software. In expert applications you will sometimes find it. VIM is an expert system and people who use it are most likely heavy computer users. You probably are too. You just don’t/ didn’t know about modes. vimtutor is therefore your first step to learning vim.
Most systems that have vim installed will also have vimtutor. Just type
in your shell and get started. You will be in a plane vim like environment and have a textbased instruction manual in front of you. It will guide you through every step and you will learn by doing.
when using vimtutor, when applying the commands, say to yourself what you are doing and it won’t feel like memorizing One quick example for this:
delete the next 10 words
d 10 w
change text inside round braces:
c i (
This will delete text in surrounding brackets and put you in insert mode. You typically have to do this when changing function arguments.
Remember and focus on the basics of VIM. Most actions consist of three basic concepts that are then composed to make the magic happen.
Count: Well instead of doing one thing over and over again, COUNT is the smart way. It just quantifies how often a certain action or movement should be performed.
Operator: Basic operation
* delete (d (~cut)) * change (c) * yank (y (~copy)) * paste (p)
Motion: how to move / what unit/ level operations are supposed to be run on
* word (w) * char level (hjkl) * lines/sentences/paragraphs....
move 10 words forward:
change the next 3 words:
3 c w
You get the idea and now go, get started!
A really nice and motivating introductory video on VIM and the thinking behind it can be found in: Lost Semster VIM
Advice on not getting frustrated early:
- Don’t use it at work right away if you have to get shit done. Play around in your freetime first.
- Don’t (just) memorize the commands
- look at other people’s .vimrc, my confiugration can be found at my vim config
- think of vim as a “programming language”
- think what you want to do and spell it out using this language
General advice on starting using vim:
- put your .vimrc in version control
- learn regularly (also a fun way to start a conversation with fellow computer scientists, maybe the equivalent to talking about the weather for normal people)
- Plugins: see if you can enhance your experience with plugins that fit your needs
- Plugins to start with:
- LightLine: a neat looking, more prominent mode indicator, so you will always know in which mode you’re in
- NerdTree: full blown file manager/ file navigation, very usefull when working in a crammed folder structure
- MRU Plugin: quickly see your most recently used / opened files and open right away or into a new window/ tab
- Git gutter: simple git status indicator where your line numbers are displayed. Indicating if a line was modified/deleted or inserted
- don’t try to just mindlessly memorize stuff, talk to your self, e.g. d+i+( means and does: delete inside (), don’t be that guy: source: http://redd.it/gh3i5r
- just google stuff, there are usually people that already did what you are trying to achieve
- remap “CAPS” to “ESC” key, this makes switching modes a lot smoother and easier (also keeps you from screaming on social media…)
- ask other proficient users for advice/ their favorite VIM plugin
I hope I could spark some interest in VIM and the thinking behind it. To me it was a really nice journey since I started using VIM about three month ago. I never would have thought it would change my way of thinking and working that much. I got more comfortable doing changes in the shell. As a regular shell user concepts just made sense. Since I started using VIM I switched all keybindings in my other IDE’s to VIM mode, as well as my iPython shell. Beside some minor problems with the vscode extension, I am surprised how easy the change was and how relaxing it is to use one set of keybindings in all my editors. So, what keeps you? Go ahead and try it for yourself!