vim高级用法
http://vim.wikia.com/wiki/Using_command-line_history
---------------------------------------------------------------------------------
When you press :
to enter a command, or /
to start a search, you often want to edit a previous command or search. That can be done using the up and down arrow keys to scroll through the history. Then you can edit a previous line. :help :
For example, type :s
and then press the up arrow key. The last command that starts with exactly what you typed will be displayed. Scroll through the history by pressing up or down. There is a history for commands, and another for searches (and more :help history). You can list the entire history using the :history command (:help :history). The command :his
lists the command history, and :his /
lists the search history.
In many situations, a better solution is the command-line window which you can open in two ways:
- Type
q:
for commands, orq/
for searches; or - Type
:
or/
to start entering a command or search, then press the 'cedit' key (default is Ctrl-f :help 'cedit').
The advantage of the command-line window is that you can use all Vim's editing power, including searching with '/' in normal mode, or using whole-line completion (:help compl-whole-line) in insert mode. After editing a command, you can:
- Press Enter to execute the current line (and close the command-line window); or
- Press Ctrl-c twice to close the command-line window (cancel).
Contents
[show]Details
Edit
Suppose you search for "horse", then for "Hello", then for "helium", then for "habit".
If you type /h
and repeatedly press up arrow, you will see /habit
, then /helium
, then /horse
("Hello" is skipped because the history navigation is case-sensitive and "Hello" does not start with a lowercase "h").
Register :
holds the last command, and /
holds the last search. Therefore ":p
will paste the last command, and "/p
will paste the last search.
Type @:
to repeat the last command.
You may have used several commands (for example, :%s/old/new/g
) on the current buffer, and now find you would like to repeat those commands on another buffer. Type q:
to enter the command window, then select and yank the commands you want. Press Ctrl-c twice to close the command window. If wanted, you can paste the commands into a buffer, then edit and save them. Later, you can source the file containing the commands, or you can yank the commands, then type :@"<CR>
to execute the yanked commands on the current buffer.
If you always want to use the command editing window, try these mappings:
nnoremap : q:i nnoremap / q/i nnoremap ? q?i
Keeping the window open
Edit
Executing a command closes the window but sometimes you want it to remain open.
Under :help q: we find a suggestion to map a key such as F5 to execute the current line, then re-open the command window: :autocmd CmdwinEnter * nnoremap <buffer> <F5> <CR>q:
This can be expanded to put us back on the same line in the command window, which is useful when repeating a whole series of commands. We use a global variable to remember which line we were on (buffer-local variables are lost when the window closes).
:autocmd CmdwinEnter * nnoremap <buffer> <F5> :let g:CmdWindowLineMark=line(".")<CR><CR>q::execute "normal ".g:CmdWindowLineMark."G"<CR>
References
Edit
- :help history
- :help Cmdline
- :help cmdwin
- :help 'cmdwinheight' option controls initial height of command-line window
- :help 'history' option controls number of history items stored in
viminfo
Comments
Edit
TO DO
- Fix Enhanced command window (or delete it if not helpful). If keep, add it as a "see also" here.
- The standard way to close the command-line window is with :q, or <C-W>q, or any of the standard "close this window" commands --- not with double Ctrl-C.
- What is the history file called? Where is it stored?
- The command window is not associated with a file at all. It is populated at startup from the .viminfo file, however. That may be worth mentioning somewhere...and the option to disable this
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2017-12-29 TMUX会话的使用