Vim Tips Wiki
(→‎Details: Register names are : and /, not @: and @/)
(→‎Comments: restore partially deleted word)
 
(9 intermediate revisions by 6 users not shown)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
When you press <tt>:</tt> to enter a command, or <tt>/</tt> 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|:}}
+
When you press <code>:</code> to enter a command, or <code>/</code> 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 <tt>:s</tt> 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 <tt>:his</tt> lists the command history, and <tt>:his /</tt> lists the search history.
+
For example, type <code>:s</code> 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 <code>:his</code> lists the command history, and <code>:his /</code> lists the search history.
   
 
In many situations, a better solution is the '''command-line window''' which you can open in two ways:
 
In many situations, a better solution is the '''command-line window''' which you can open in two ways:
*Type <tt>q:</tt> for commands, or <tt>q/</tt> for searches; or
+
*Type <code>q:</code> for commands, or <code>q/</code> for searches; or
*Type <tt>:</tt> or <tt>/</tt> to start entering a command or search, then press the 'cedit' key (default is Ctrl-f {{help|'cedit'}}).
+
*Type <code>:</code> or <code>/</code> 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:
 
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:
Line 26: Line 26:
 
Suppose you search for "horse", then for "Hello", then for "helium", then for "habit".
 
Suppose you search for "horse", then for "Hello", then for "helium", then for "habit".
   
If you type <tt>/h</tt> and repeatedly press up arrow, you will see <tt>/habit</tt>, then <tt>/helium</tt>, then <tt>/horse</tt> ("Hello" is skipped because it does not start with "h").
+
If you type <code>/h</code> and repeatedly press up arrow, you will see <code>/habit</code>, then <code>/helium</code>, then <code>/horse</code> ("Hello" is skipped because the history navigation is case-sensitive and "Hello" does not start with a lowercase "h").
   
Register <tt>:</tt> holds the last command, and <tt>/</tt> holds the last search. Therefore <tt>":p</tt> will paste the last command, and <tt>"/p</tt> will paste the last search.
+
Register <code>:</code> holds the last command, and <code>/</code> holds the last search. Therefore <code>":p</code> will paste the last command, and <code>"/p</code> will paste the last search.
   
Type <tt>@:</tt> to repeat the last command.
+
Type <code>@:</code> to repeat the last command.
   
You may have used several commands (for example, <tt>:%s/old/new/g</tt>) on the current buffer, and now find you would like to repeat those commands on another buffer. Type <tt>q:</tt> 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 <tt>:@"<CR></tt> to execute the yanked commands on the current buffer.
+
You may have used several commands (for example, <code>:%s/old/new/g</code>) on the current buffer, and now find you would like to repeat those commands on another buffer. Type <code>q:</code> 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 <code>:@"<CR></code> to execute the yanked commands on the current buffer.
   
 
If you always want to use the command editing window, try these mappings:
 
If you always want to use the command editing window, try these mappings:
Line 40: Line 40:
 
nnoremap ? q?i
 
nnoremap ? q?i
 
</pre>
 
</pre>
  +
  +
==Keeping the window open==
  +
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:
 
<code>:autocmd CmdwinEnter * nnoremap <buffer> <F5> <CR>q:</code>
  +
  +
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).
  +
  +
<code>:autocmd CmdwinEnter * nnoremap <buffer> <F5> :let g:CmdWindowLineMark=line(".")<CR><CR>q::execute "normal ".g:CmdWindowLineMark."G"<CR></code>
   
 
==References==
 
==References==
Line 46: Line 56:
 
*{{help|cmdwin}}
 
*{{help|cmdwin}}
 
*{{help|'cmdwinheight'}} option controls initial height of command-line window
 
*{{help|'cmdwinheight'}} option controls initial height of command-line window
*{{help|'history'}} option controls number of history items stored in <tt>viminfo</tt>
+
*{{help|'history'}} option controls number of history items stored in <code>viminfo</code>
   
 
==Comments==
 
==Comments==
Line 54: Line 64:
 
*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.
 
*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?
+
* 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.
+
** 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.
   
  +
* The cmdwin would be very useful to use as an interpreter to debug and quickly craft things (with the above trick to keep it open). Since however I can't see my :echo results, it is almost useless for that purpose. I think it's because the wincmd redraws the screen when it reappears, still I wonder if there is a workaround?
*Is it possible to execute a command from the command-line window without the command-line window subsequently closing?
 
::No, however under {{help|q:}} we find a suggestion to map a key to execute the current line, then re-open the command window. I just tried mapping with CR rather than the F5 in help, and it seems to work ok:
 
:::<tt>:autocmd CmdwinEnter * nnoremap <buffer> <CR> <CR>q:</tt>
 
::If it works without bad side effects, we might put it in the tip. [[User:JohnBeckett|JohnBeckett]] 21:15, January 13, 2010 (UTC)
 

Latest revision as of 16:24, 25 February 2019

Tip 45 Printable Monobook Previous Next

created 2001 · complexity basic · version 5.7


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, or q/ 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).

Details[]

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[]

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[]

Comments[]

 TO DO 

  • 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.
  • The cmdwin would be very useful to use as an interpreter to debug and quickly craft things (with the above trick to keep it open). Since however I can't see my :echo results, it is almost useless for that purpose. I think it's because the wincmd redraws the screen when it reappears, still I wonder if there is a workaround?