Update cmdline mode chapter

pull/62/head
Igor Irianto 3 years ago
parent e4d22fc342
commit 8f259eb5c9

@ -4,7 +4,8 @@ In the last three chapters, you learned how to use the search commands (`/`, `?`
In this chapter, you will learn various tips and tricks for the command-line mode.
## Entering And Exiting The Command-Line Mode
## Entering And Exiting The Command-line Mode
The command-line mode is a mode in itself, just like normal mode, insert mode, and visual mode. When you are in this mode, the cursor goes to the bottom of the screen where you can type in different commands.
There are 4 different commands you can use to enter the command-line mode:
@ -14,18 +15,17 @@ There are 4 different commands you can use to enter the command-line mode:
You can enter the command-line mode from the normal mode or the visual mode.
To leave the command-line mode, you can use `<esc>`, `Ctrl-c, or Ctrl-[`.
To leave the command-line mode, you can use `<Esc>`, `Ctrl-c, or Ctrl-[`.
*Sometimes other literatures might refer the "Command-line command" as "Ex command" and the "External command" as "filter command" or "bang operator".*
*Other literatures might refer the "Command-line command" as "Ex command" and the "External command" as "filter command" or "bang operator".*
## Repeating The Previous Command
You can repeat the previous command-line command or external command with `@:`.
If you just ran `:s/foo/bar/g`, running `@:` repeats that substitution.
You can repeat the previous command-line command or external command with `@:`.
If you just ran `:.!tr '[a-z]' '[A-Z]'`, running `@:` repeats the last external command translation filter.
If you just ran `:s/foo/bar/g`, running `@:` repeats that substitution. If you just ran `:.!tr '[a-z]' '[A-Z]'`, running `@:` repeats the last external command translation filter.
## Command-Line Mode Shortcuts
## Command-line Mode Shortcuts
While in the command-line mode, you can move to the left or to the right, one character at a time, with the `Left` or `Right` arrow.
@ -42,23 +42,23 @@ Ctrl-u Delete the entire line
```
Finally, if you want to edit the command like you would a normal textfile use `Ctrl-f`.
This also allows you to search through the previous commands, edit them and rerun them by pressing `Enter` in "command-line editing normal mode".
This also allows you to search through the previous commands, edit them and rerun them by pressing `<Enter>` in "command-line editing normal mode".
## Register And Autocomplete
When programming, whenever possible, do not repeat if you can autocomplete it. This mindset will not only save you time but reduces the chances of typing the wrong characters.
While in the command-line mode, you can insert texts from Vim register with `Ctrl-R` the same way as the insert mode. If you have the string "foo" saved in the register "a, you can insert it by running `Ctrl-R a`. Everything that you can get from the register in the insert mode, you can do the same from the command-line mode.
You can insert texts from Vim register with `Ctrl-r` (the same way as the insert mode). If you have the string "foo" saved in the register "a, you can insert it by running `Ctrl-r a`. Everything that you can get from the register in the insert mode, you can do the same from the command-line mode.
In addition, you can also get the word under the cursor with `Ctrl-R Ctrl-W` (`Ctrl-R Ctrl-A` for the WORD under cursor). To get the line under the cursor, use `Ctrl-R Ctrl-L`. To get the filename under the cursor, use `Ctrl-R Ctrl-F`.
You can also autocomplete commands. To autocomplete the `echo` command, while in the command-line mode, type "ec", then press `<Tab>`. You should see on the bottom left Vim commands starting with "ec" (example: `echo echoerr echohl echomsg econ`). To go to the next option, press either `<Tab>` or `Ctrl-n`. To go the previous option, press either `<Shift-Tab>` or `Ctrl-p`.
You can also autocomplete existing commands. To autocomplete the `echo` command, while in the command-line mode, type "ec", then press `<Tab>`. You should see on the bottom left Vim commands starting with "ec" (example: `echo echoerr echohl echomsg econ`). To go to the next option, press either `<Tab>` or `Ctrl-N`. To go the previous option, press either `<Shift-Tab>` or `Ctrl-P`.
Some command-line commands accept file names as arguments. One example is `edit`. After typing the command, `:e ` (don't forget the space), press `<Tab>`. Vim will list all the relevant file names.
Some command-line commands accept file names as arguments. One example is `edit`. You can autocomplete here too. After typing the command, `:e ` (don't forget the space), press `<Tab>`. Vim will list all the relevant file names that you can choose from so you don't have to type it from scratch.
## History Window
## History Window And Command-line Window
You can view the histoy of command-line commands and search terms (make sure that your Vim build has `+cmdline_hist` when you run `vim --version`).
You can view the histoy of command-line commands and search terms (this requires the `+cmdline_hist` feature).
To open the command-line history, run `:his :`:
To open the command-line history, run `:his :`. You should see something like the following:
```
## cmd History
@ -67,37 +67,30 @@ To open the command-line history, run `:his :`:
4 s/foo/bar/g
```
Vim lists the history of all the `:` commands you run. By default, Vim stores the last 50 commands. To change the amount of the entries that Vim remembers to 100, you can run `:set history=100`.
Vim lists the history of all the `:` commands you run. By default, Vim stores the last 50 commands. To change the amount of the entries that Vim remembers to 100, you run `set history=100`.
A more useful use of the command-line history is through the command-line window,`q:`. This will open a searchable, editable history window. Suppose you have these expressions in the history when you press `q:`:
When you are in the command-line mode, you can traverse this history list by pressing `Up` and `Down` button. Suppose you had the command-line command history that looks like:
```
51 s/foo/bar/g
52 s/foo/baz/g
53 s/foo//g
51 s/verylongsubstitutionpattern/pancake/g
52 his :
53 wq
```
If you press `:` then press `Up` once, you'll see `:s/foo//g`. Press `Up` one more time to see `:s/foo/baz/g`. Vim goes up the history list.
If your current task is to do `s/verylongsubstitutionpattern/donut/g`, instead of typing the command from scratch, why don't you reuse `s/verylongsubstitutionpattern/pancake/g`? After all, the only thing that's different is the word substitute, "donut" vs "pancake". Everything else is the same.
Similarly, to view the search history, run `:his /`. You can also traverse the history stack by pressing `Up` or `Down` after running the history command `/`.
After you ran `q:`, find that `s/verylongsubstitutionpattern/pancake/g` in the history (you can use the Vim navigation in this environment) and edit it directly! Change "pancake" to "donut" inside the history window, then press `<Enter>`. Boom! Vim executes `s/verylongsubstitutionpattern/donut/g` for you. Super convenient!
Vim is smart enough to distinguish different histories. If you press `Up` or `Down` after pressing `:`, Vim automatically the command history. If you press `Up` or `Down` after pressing `/`, Vim automatically searches the search history.
Similarly, to view the search history, run `:his /` or `:his ?`. To open the search history window where you can search and edit past history, run `q/` or `q?`.
## Command-Line Window
The history window displays the list of previously used command-line commands, but you can't execute the command from the history window. To execute a command while browsing, use the *command-line window*. There are three different command-line windows:
```
q: Command-line window
q/ Forward search window
q? Backward search window
```
To quit this window, press `Ctrl-C`, `Ctrl-W C`, or type `:quit`.
Run `q:` to launch the command-line window for command-line commands. Vim will launch a new window at the bottom of the screen. You can traverse upward with the `Up` or `Ctrl-p` keys and traverse downward with the `Down` or `Ctrl-n` keys. If you press `<Return>`, Vim executes that command. To quit the command-line window, either press `Ctrl-c`, `Ctrl-w c`, or type `:quit`.
## More Command-line Commands
Similarly, to launch the command-line window for search, run `q/` to search forward and `q?` to search backward.
Vim has hundreds of built-in commands. To see all the commands Vim have, check out `:h ex-cmd-index` or `:h :index`.
## Learn Command-Line Mode The Smart Way
## Learn Command-line Mode The Smart Way
Compared to the other three modes, the command-line mode is like the Swiss Army knife of text editing. You can edit text, modify files, and execute commands, just to name a few. This chapter is a collection of odds and ends of the command-line mode. It also brings Vim modes into closure. Now that you know how to use the normal, insert, visual, and command-line mode you can edit text with Vim faster than ever.
Compared to the other three modes, the command-line mode is like the Swiss Army knife of text editing. You can edit text, modify files, and execute commands, just to name a few. This chapter is a collection of odds and ends of the command-line mode. It also brings Vim modes into closure. Now that you know how to use the normal, insert, visual, and command-line mode you can edit text with Vim faster than ever.
It's time to move away from Vim modes and learn how to do a faster navigation with Vim tags.
It's time to move away from Vim modes and learn how to do an even faster navigation with Vim tags.

Loading…
Cancel
Save