Banner of Boost Vim Productivity: Streamline Command Execution with OutputCommands

Vim Tips: Simplify Command Execution with OutputCommands Function


Category: vim

Date: 3 months ago
Views: 404


Introduction:

Vim, the venerable text editor, offers a myriad of features and customization options that can enhance productivity for its users. One such feature is the ability to execute old commands quickly within the current file. In this article, we will delve into a custom Vim function that I called OutputCommands, which streamlines the process of executing previous commands effortlessly.

The function:

Add the followng to your .vimrc file :

    
function! OutputCommands()
  let history = []
  for i in range(histnr(':'), 1, -1)
    call add(history, histget(':', i))
  endfor
  enew
  call append(0, history)
  normal gg
  setlocal buftype=nofile bufhidden=wipe noswapfile nowrap
  "nnoremap <buffer> <CR> :call setreg('+', getline('.'))<CR>
  nnoremap <buffer> <CR> :let command = getline('.') \| bnext \| execute command<CR>
  nnoremap <buffer> <down> j
  nnoremap <buffer> <up> k
endfunction

:command! Cc     call OutputCommands()
:command! CC     call OutputCommands()
    

Understanding OutputCommands:

The OutputCommands function is a custom Vim function designed to simplify the execution of historical commands within the context of the current file being edited. Let's break down how this function works:

  • Gathering Command History:
    The function starts by creating an empty list named "history". It then iterates through the command history using a for loop, starting from the most recent command (histnr(':')) and moving backwards. For each iteration, it retrieves the command using histget(':') and adds it to the "history" list.

  • Creating a New Buffer:
    After collecting the command history, the function opens a new empty buffer using the "enew" command.

  • Populating the Buffer:
    The collected command history is appended to the newly created buffer using the "append()" function. This effectively displays the command history in the newly created buffer.

  • Setting Buffer Options:
    Several buffer-local options are set using the "setlocal" command to ensure proper behavior. These options include "buftype=nofile" (indicating that the buffer is not associated with a file), "bufhidden=wipe" (ensuring the buffer is wiped when it's no longer in use), "noswapfile" (disabling swap file creation), and "nowrap" (preventing line wrapping).

  • Mapping Key Bindings:
    Key bindings are mapped to facilitate navigation and command execution within the buffer. The "nnoremap" command is used to define mappings for the Enter key ("<CR>"), the Down arrow key ("<down>"), and the Up arrow key ("<up>"). These mappings allow users to navigate through the command history and execute commands easily.

  • Command Execution:
    The most crucial mapping is for the Enter key ("<CR>"). When pressed, it retrieves the command from the current line using "getline('.')", stores it in a variable named "command", switches to the next buffer using "bnext", and executes the stored command using "execute command".

  • Usage:

    To utilize the OutputCommands function, users can simply invoke it by typing ":Cc" or ":CC" in normal mode, depending on their preference. This will open a new buffer containing the command history, allowing them to navigate through previous commands and execute them with ease.

    Conclusion:

    The OutputCommands function offers a convenient way to access and execute historical commands within Vim, streamlining the editing process and enhancing productivity. By understanding its implementation and usage, Vim users can leverage this functionality to make their editing experience more efficient and enjoyable.



    404 views

    Previous Article Next Article

    0 Comments, latest

    No comments.