-
Command-Line mode
prompts
us to enter: -
In
Command-Line
mode we can type the name of a command and then execute it by pressing<CR>
. -
We can switch from
Command-Line
mode back toNormal
mode by pressing<Esc>
. -
We can use
Ex
commands to:-
read and write files
:edit and :write
. -
create tabs
:tabnew
or split windows:split
. -
show the list of buffers
:ls
. -
alot more...**
In fact, Vim has an Ex command for just about everything.
-
-
Command-Line
mode is similar toInsert
mode in that most of the buttons on the keyboard simply enter a character. InInsert
mode, the text goes into abuffer
, whereas inCommand-Line
mode the textappears at the prompt
.
- We can insert the contents of
any register
at thecommand line
using the<C-r>{register}
command.- In
Command-Line
mode, we can use the<Tab>
key toauto-complete
thename of a file
or thename of a command
.
-
Vim keeps a
history
of thecommands
we have entered inCommand-Line
mode, so we do not need to write all of them again. -
By default it keeps the
last 20 commands
we have entered, but you canchange
this number by changing thehisotry
option, say we want vim to remember the last200
commands that we have entered in theCommand-Line
mode. You can do this by writingset history=200
, in your.vimrc
file. -
We can use the
<Up>
key to go to the nextcommand
in thecommands' history
, and<Down>
key to go to theprevious command
in thecommands' history
. -
In this way of navigation through our command's history, we do not have the all power ov vim features, like
searching
for acommand
in thehistory
orfiltering
thehistory
to show only thecommands
thatmatch
apattern
. Likely vim know this and introduce us to theCommand-Line window
. -
The
Command-Line window
is like a regular vimbuffer
where each line contains a command from our history and we can naviagte through them usine our favoritehjkl
keys, and we can usesearch
andfilter
to find thecommand
we want. We can alsoexecute
the command in the current line by pressing<CR>
. -
We can open the
Command-Line window
by pressingq:
inNormal-Line
mode.
-
One of the mose important features of
Ex
commands, is their ability to be executed across many lines at the same time. -
Many
Ex commands
can be given a[range]
of lines to act upon. We can specify the start and end of a range with either:-
a
line number
. -
a
mark
. -
a
pattern
.
-
-
if we enter a
line number
without any commands inCommand-Line
mode, vim will jump to thatline
. -
Examples:
-
:1
$\to$ go to thefirst line
in thebuffer
. -
:5
$\to$ go to thefifth line
in thebuffer
. -
:$
$\to$ go to thelast line
in thebuffer
.
-
-
If we enter a command with a
line number
as a[range]
, the command will be executed on thatline
only. -
Examples:
-
:5d
$\to$ delete
thefifth line
in thebuffer
. -
:3,8d
$\to$ delete
from line number3
to line number8
in thebuffer
. -
:1,$d
or:%d
$\to$ delete
from thefirst line
to thelast line
in thebuffer
. -
:d
$\to$ delete
thecurrent line
in thebuffer
.
-
-
Ass you gussed from examples
2
and3
, that we can use the:{start},{end}
to specify a range of lines fromstart
toend
-
As we have seen that
d
$\to$ delete
, there are alot of other commands we can perform on text from theCommand-Line
mode. -
Examples:
-
y
$\to$ yank
orcopy
thetext
in thebuffer
to theregister
. -
p
$\to$ paste
thetext
in theregister
to thebuffer
. -
c
$\to$ change
thetext
in thebuffer
. -
s
$\to$ substitute
thetext
in thebuffer
. -
gU
$\to$ uppercase
thetext
in thebuffer
. -
gu
$\to$ lowercase
thetext
in thebuffer
. -
>
$\to$ indent
thetext
in thebuffer
. -
<
$\to$ unindent
thetext
in thebuffer
. -
m
$\to$ move
thetext
in thebuffer
.
-
-
We can also specify rnage of lines using
visual selections
. -
Try do the following:
-
Go to
visual mode
by pressing:v
fromnormal mode
-
Select some lines using
hjkl
keys. -
Press
:
to go toCommand-Line
mode. -
You will see that vim has already entered the
:'<,'>
for you. You can think of this as the range of lines that you have selected invisual mode
. -
Now you can enter the
command
you want to execute on theselected lines
or therange
.
-
-
Suppose we have the following text in our
buffer
:fn main() { let cat = 1; if cat == 1 { println!("x = {}", x); } let dog = 2; let z = 1 + 2; println!("z = {}", z); }
-
try to type
:/cat/,/dog/d
inCommand-Line
mode. -
this create a
range
of lines from the line that contains the wordcat
to the line that contains the worddot
, and then delete them.