Below you will find pages that utilize the taxonomy term “Vi”
Checking spelling
Vim has a built-in spell check that you can enable with :set spell
. You can
also set the language with :set spelllang=
followed by the two-letter code of
the language and optionally a dash and the region (e.g., :set spelllang=en
or
:set spelllang=en-GB
).
After enabling the spell checker, you can move between different mistakes with
]s
and [s
.
I knew about the spell check, I knew Vim highlighted spelling mistakes, and I knew how to move between the mistakes. I usually fix them manually and I never bothered to learn how to make Vim fix them for me. Until today.
`:read` the output of a command into the current buffer
If you want to use the output of a command inside your file, you don’t need
to exit Vim, run the command, copy the output, return to Vim and paste the
contents. You can use :read
(or abbreviated to :r
) and run a command
with !
. For example, :r !ls -a
will insert the output of ls -a
at
the current cursor position.
You can also insert the content of another file with :r
, except this time
you don’t use !
, just the file name. For example, :r second_file
will
insert the content of second_file
at the current cursor position.
Changing capitalisation of words
In Vim, while in normal mode, if you type ~
, the capitalisation of the
letter under the cursor changes and the cursor is advanced to the right.
You can also change the capitalisation of multiple letters at the same time
with g
command. Type gU
and a movement to make every letter corresponding
to the movement to become uppercase. gu
makes it lowercase and g~
toggles
the case. You can repeat the last character to affect the whole line (guu
,
gUU
, g~~
).
Counting with `g` followed by increment/decrement
In Vim, you can type ctrl-a
or ctrl-x
to increment or decrement the next
number in the line. For example, suppose you have the following line:
1. Do something
If you put the cursor on number one and type ctrl-a
repeated times, you notice
the number will increase to 2, 3, etc. Similarly, if you type ctrl-x
, it will decrease
going into negative numbers if you do it enough times.
Let’s say you want to create a numbered list. You have a list of people:
Line movement with `g`
If you have a long line in Vim, Vim wraps that line visually at window length.
It’s not a hard wrap, no newline character is inserted. Since it’s only one
line, if you try moving up or down with j
and k
, you’ll notice that the
cursor moves up or down the group of lines (visually) corresponding to one line
in the file. The same happens when you type 0
or ^
to go to the beginning
of the line and $
to go to the end.
The `:normal` command in Vim
You can use :normal
(or the abbreviated form, :norm
) in Vim to execute
a normal command on a range of lines.
Example:
:%norm I1. <cr>
Here, <cr>
means ‘press enter key’. Also, notice the space after the dot.
That command will act on all line (%
) and insert at the beginning of the line (I
)
the number 1 followed by a dot, followed by a space.
After pressing enter, the command is executed and Vim returns to normal mode.
Use `gv` to reselect
If you selected part of the text, then did something else, then decided
to select the same text again, instead of using v
or V
and redoing the
selection, you can type gv
in normal mode to reselect the previous text.
Photo by the author
Using `ctrl-v` to type control characters
When in command-line mode in Vim, for example, when typing Ex commands, you can
use ctrl-v
to type control characters. Usually, if you type the control
character, Vim will execute it. For example, if you simply press enter, you’re
going to execute whatever you have typed so far in command-line mode. But
if you type ctrl-v
followed by enter, you will see a ^M
inserted and
Vim will treat it as if you typed the enter character instead.
Using `gq` or `gw` to format text
If you type a really long line in Vim, Vim soft-wraps it at the end of the
window, but in the file it’s still one line. If you type gq
or gw
followed
by a movement, Vim formats the text based on the movement you selected. For
instance, gwip
will format the paragraph under the cursor (gw
inside i
paragraph p
), gwG
will format from the cursor to the end of the file (G
).
Using `o` in visual mode to change direction of selection
If you select some lines or characters with V
or v
and want to change
the direction of expansion of the selection, type o
.
For example, suppose you have the following text:
Holmes was certainly not a difficult man to live with. He was quiet in his
ways, and his habits were regular.
It was rare for him to be up after ten at night, and he had invariably
breakfasted and gone out before I rose in the morning.
Sometimes he spent his day at the chemical laboratory, sometimes in the
dissecting-rooms, and occasionally in long walks, which appeared to take him
into the lowest portions of the City.
Nothing could exceed his energy when the working fit was upon him; but now and
again a reaction would seize him, and for days on end he would lie upon the
sofa in the sitting-room, hardly uttering a word or moving a muscle from
morning to night.
On these occasions I have noticed such a dreamy, vacant expression in his eyes,
that I might have suspected him of being addicted to the use of some narcotic,
had not the temperance and cleanliness of his whole life forbidden such a
notion.
Suppose your cursor is in the line starting with Sometimes. You pressed V
and started selecting down with j
. Then you realised you should have started
from the line It was rare two lines above your initial choice. If you press
k
to move up, you deselect what you had selected. However, you can type o
,
the cursor changes to the beginning of selection and now when you type k
,
you expand the selection upwards.
Vim `:g` and :`v`
If you want to execute a command on all lines matching a pattern, you
can use the Ex command :g
. For example, if you want to delete all
blank lines, you can do
:g/^\s*$/d
It will get all lines that matches this: from the beginning (^
), it may
contain 0 or more whitespace (\s*
) until the end of line ($
). In other
words, if the line is empty or contain only whitespace.
Some Vim tips
I decided to compile a list of Vim tips as TIL (Today I learnt), although most of these tips I already knew. The following posts will follow a TIL format.
Photo by the author