Wiki

by yszheda

View project onGitHub

Tip 34: Recall Commands from History

Return to the top: <<Practical Vim>>

  • Recalling commands:
    • scrolling through past command-lines with the cursor keys
    • dialing up the command-line window
  • Change command history limit (By default, it is 20):
    set history=200
    
  • The history persists when we quit and relaunch vim.

References:

# If you open the file in vim,
# then you can \"ayy@a the next line to execute the vim command: 
:h viminfo

Avoid the Cursor Keys When Recalling Commands from History

  • Alternative of <Up> and Down: <C-p> and <C-n>.
    • Advantage of <C-p> and <C-n>: do not need to move our hands from the home row
    • Disadvantage of <C-p> and <C-n>: do not filter the command history
  • Solution: add the following mappings
    cnoremap <C-p> <Up>
    cnoremap <C-n> <Down>
    

Meet the Command-Line Window

  • q:: enter the command-line window

References:

# If you open the file in vim,
# then you can \"ayy@a the next line to execute the vim command: 
:h cmdwin
  • When we press <CR> (<ENTER>) in the command-line window, the command is executed in the context of the active window.
Command Action
q/ Open the command-line window with history of searches
q: Open the command-line window with history of Ex commands
<C-f> Switch from Command-Line mode to the command-line window