Practical Vim< /a> vit Practical Vim< /a> Note that the styling for a Visual selection is the... development I thought I knew Vim, but Practical" name="description"/>
Tải bản đầy đủ - 0 (trang)
Tip 34. Recall Commands from History

Tip 34. Recall Commands from History

Tải bản đầy đủ - 0trang

Recall Commands from History



• 69



Avoid the Cursor Keys When Recalling Commands from History

In addition to the and keys, we can also use the and chords

to go backward and forward through our command history. The advantage of using

these mappings is that we don’t need to move our hands from the home row to use

them. But there’s a disadvantage to the and commands: unlike and

, they don’t filter the command history.

We can get the best of both by creating the following custom mappings:

ex_mode/history-scrollers.vim

cnoremap

cnoremap



Suppose that we’re working on a simple Ruby script. Each time we make a

change, we find ourselves running the following two commands:









:write

:!ruby %



After running these two commands in quick succession a couple of times, we

realize that we could streamline our workflow by folding them into a single

command line. This way we can dial up one complete command from our

history and replay it:







:write | !ruby %



Each of these commands is already in our history, so we shouldn’t have to

type the entire command line from scratch. But how can we merge two records

from our history into one? Press q: and meet the command-line window (see

:h cmdwin ).

The command-line window is like a regular Vim buffer, where each line contains an item from our history. With the k and j keys, we can move backward

and forward through our history. Or we can use Vim’s search feature to find

the line that we’re looking for. When we press the key, the contents of

the current line are executed as an Ex command.

The beauty of the command-line window is that it allows us to change historical commands using the full modal editing power of Vim. We can navigate

with any of the motions we’re accustomed to using in Normal mode. We can

operate on visual selections or switch to Insert mode. We can even run Ex

commands on the contents of the command-line window!

Having summoned the command-line window by pressing q: , we could solve

our problem as follows:



www.it-ebooks.info



report erratum • discuss



Chapter 5. Command-Line Mode



Keystrokes



Buffer Contents



{start}



write

!ruby %



A ␣|



write |

!ruby %



J



write | !ruby %



:s/write/update



update | !ruby %



• 70



Pressing would then execute the :update | !ruby % command as though we

had typed it into the command line.

When the command-line window is open, it always gets the focus. That means

we can’t switch to other windows except by dismissing the command-line

window. We can close the command-line window by running the :q command

(just like any ordinary Vim window) or by pressing .

Note that when we press in the command-line window, the command is

executed in the context of the active window: that is, the window that was

active before the command-line window was summoned. Vim doesn’t indicate

which is the active window when the command-line window is open, so pay

attention if you’re using split windows!

What if halfway through composing an Ex command at the prompt, we realize

that we need more editing power? In Command-Line mode, we can use the

mapping to switch to the command-line window, preserving a copy of

the command that was typed at the prompt. This table summarizes a few of

the methods for summoning the command-line window:

Command



Action



q/



Open the command-line window with history of searches



q:



Open the command-line window with history of Ex commands



ctrl-f



Switch from Command-Line mode to the command-line window



It’s easy to mix up the q: and :q commands. I’m sure that we’ve all opened

the command-line window by accident when we actually meant to quit Vim!

It’s a shame, because this feature is really useful, but many people are frustrated by their first (accidental) encounter with it. Skip ahead to Tip 85, on

page 209, for another example of the command-line window in action.



www.it-ebooks.info



report erratum • discuss



Run Commands in the Shell



• 71



Tip 35



Run Commands in the Shell

We can easily invoke external programs without leaving Vim. Best of all, we

can send the contents of a buffer as standard input to a command or use the

standard output from an external command to populate our buffer.

The commands discussed in this tip work best when used from Vim inside a

terminal. If you’re using GVim (or MacVim), then things may not work quite

as smoothly. That shouldn’t come as a great surprise. It’s much easier for

Vim to delegate work to the shell if Vim itself is already running inside a shell.

GVim does some things better, but this is one area where terminal Vim has

the edge.



Executing Programs in the Shell

From Vim’s Command-Line mode, we can invoke external programs in the

shell by prefixing them with a bang symbol (see :h :! ). For example, if we

want to examine the contents of the current directory, we could run the

following:









:!ls

duplicate.todo

loop.js

emails.csv

practical-vim.html

foobar.js

shopping-list.todo

history-scrollers.vim

Press ENTER or type command to continue



Note the difference between :!ls and :ls—the former calls the ls command in

the shell, whereas :ls calls Vim’s built-in command, which shows the contents

of the buffer list.

On Vim’s command line, the % symbol is shorthand for the current file name

(see :h cmdline-special ). We can exploit this to run external commands that do

something with the current file. For example, if we’re working on a Ruby file,

we could execute it by running this:







:!ruby %



Vim also provides a set of filename modifiers, which allow us to extract

information from the current filename, such as its path or extension (see

:h filename-modifiers ). Skip ahead to Tip 44, on page 101, for an example of how

these can be used.



www.it-ebooks.info



report erratum • discuss



Chapter 5. Command-Line Mode



• 72



The :!{cmd} syntax is great for firing one-off commands, but what if we want

to run several commands in the shell? In that case, we can use Vim’s :shell

command to start an interactive shell session (see :h :shell ):



















:shell

$ pwd

/Users/drew/books/PracticalVim/code/ex_mode

$ ls

duplicate.todo

loop.js

emails.csv

practical-vim.html

foobar.js

shopping-list.todo

history-scrollers.vim

$ exit



The exit command kills the shell and returns us to Vim.



Putting Vim in the Background

The :shell command is a feature provided by Vim, which lets us switch to an interactive

shell. But if we’re already running Vim in a terminal, then we also have access to

built-in shell commands. For example, the bash shell supports job control, which

allows us to suspend a job by putting it into the background and then lets us resume

it later by bringing it back into the foreground.

Suppose that we’re running Vim inside a bash shell and we want to execute a series

of shell commands. Pressing Ctrl - z suspends the process that’s running Vim and

returns control to bash. The Vim process sits idle in the background, allowing us to

interact with our bash session as normal. We can inspect the list of jobs by running

this command:









$ jobs

[1]+ Stopped



vim



In bash, we can use the fg command to resume a suspended job, bringing it back

into the foreground. That brings Vim back to life exactly as we left it. The Ctrl - z and

fg commands are quicker and easier to use than Vim’s equivalent :shell and exit commands. For more information, run man bash and read the section on job control.



Using the Contents of a Buffer for Standard Input or Output

When we use the :!{cmd} syntax, Vim echoes output from the {cmd}. This works

fine if the command produces little or no output, but it’s not very helpful if

the command produces many lines of output. As an alternative, we can use

the :read !{cmd} command, which puts the output from the {cmd} into our

current buffer (see :h read! ).

The :read !{cmd} command lets us direct standard output into a buffer. As you

might expect, the :write !{cmd} does the inverse: it uses the contents of the



www.it-ebooks.info



report erratum • discuss



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tip 34. Recall Commands from History

Tải bản đầy đủ ngay(0 tr)

×