When did you first learn about Vim? Where you one of those unfortunate souls, who just wanted to run a git command, but ended up trapped in vim? Many of us have been there. Ending up in vim accidentally is like being accidentally teleported to an unknown planet. How are you supposed to google your way out if you do not know where you even are?
I’m still not sure, is vim something everybody knows and uses or maybe just “knows and not-uses” or is it actually really obscure. I only started using it, when I took a deep dive into git commands. Once you start using it, you suddenly realize that GUIs might be overrated. There surely are many things, which are easier in a GUI, but it turns out many things are just as easy in the terminal.
How to first approach vim?
Just go through vimtutor
.
vimtutor
is a great vim instruction manual and exercise book. It is probably already installed on your computer, it comes together with the vim installation. To start it, you just run vimtutor
in your terminal:
$ vimtutor
and you are transported into the vim editor with lessons and instructions and examples to practise commands on.
...
5. Move on to Lesson 2.3 to understand what is happening.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Lesson 2.3: ON OPERATORS AND MOTIONS
Many commands that change text are made from an operator and a motion.
The format for a delete command with the d delete operator is as follows:
d motion
Where:
d - is the delete operator.
motion - is what the operator will operate on (listed below).
A short list of motions:
w - until the start of the next word, EXCLUDING its first character.
e - to the end of the current word, INCLUDING the last character.
$ - to the end of the line, INCLUDING the last character.
Thus typing de will delete from the cursor to the end of the word.
...
Once you go through the whole tutorial or at least the first few lessons, using vim becomes child’s play (including closing it). The next step is to modify vim to make it suit your needs. A vast number of settings and plugins is available for vim. But by default, most settings are turned off.
Pimp my vim
Curiously, lots of essential vim settings are disabled by default. A good example is showing line numbers:
To enable them in an open vim editor, type [ESC]
and then :set number
and [ENTER]
. This will make the line numbers immediately appear.
To disable it again, type [ESC]
+ :set number!
+ [ENTER]
.
Sometimes you might want to know the value of a setting, for this the command is :echo &<setting_name>
, i.e. :echo &number
.
To change the default behaviour, create a .vimrc
file in your home directory: vim ~/.vimrc
and add any number of settings.
Here is a list of default vim settings I have in my .vimrc
file:
" :W sudo saves the file when the file is open in readonly mode
command W w !sudo tee % > /dev/null
""""""""""""""""""""""""""""""""""""
" Line
""""""""""""""""""""""""""""""""""""
" show line numbers
set number
"""""""""""""""""""""""""""""""""""""
" Indents
"""""""""""""""""""""""""""""""""""""
" replace tabs with spaces
set expandtab
" 1 tab = 2 spaces
set tabstop=2 shiftwidth=2
" when deleting whitespace at the beginning of a line, delete
" 1 tab worth of spaces (for us this is 2 spaces)
set smarttab
" when creating a new line, copy the indentation from the line above
set autoindent
"""""""""""""""""""""""""""""""""""""
" Search
"""""""""""""""""""""""""""""""""""""
" Ignore case when searching
set ignorecase
set smartcase
" highlight search results (after pressing Enter)
set hlsearch
" highlight all pattern matches WHILE typing the pattern
set incsearch
"""""""""""""""""""""""""""""""""""""
" Mix
"""""""""""""""""""""""""""""""""""""
" show the mathing brackets
set showmatch
" highlight current line
set cursorline
Have fun testing out vim!