As a VIM lover, have you ever wondered the possibility of using vim bindings to edit commands in your shell? Or perhaps you’re curious about how to manage lengthy commands effortlessly, without getting lost in endless flag combinations? If so, buckle up! You’re in for a treat!
In this blog post, we will introduce you to an indispensable yet often overlooked component of the Bash environment - the .inputrc file. This secret sauce allows users to customize readline, the built-in line editor for Bash, unveiling hidden productivity boosters and streamlining command-line workflows.
Say goodbye to monstonous manual edits and hello to a powerful editing experience inspired by vim!
Toggling modes
VIM has several modes, but we will care about 3 modes here
- Normal mode - The “default” mode of vim (or when you press
Esc). In this mode, the keyboard inputs control movements, commands, and operators instead of inserting text - Insert mode - Accessed by pressing certain buttons like
iin Normal mode. This mode enables to input text, allowing to type characters (as you would expect in any single mode text editor) - Visual mode - Accessed by pressing
vin normal mode. This allows you to select blocks of text for operations like copy (yanky), deletedor changing the contentcSimilarly, we will use 3 modes in our commandline. - By default the command line will be in command (or normal) mode (marked by the prepended
Cto indicate command mode and have a blinking block in my configuration). - Insert mode similarly triggered by pressing buttons like
i,aetc in command mode - Pressing
vto edit commands in visual mode? (more about it later)
Herein lies an exciting opportunity! Instead of pressing the Esc key each time to alternate between insert and command modes, let’s bind a convenient button or sequence of buttons for seamless transitions. I personally recommend using ;;, a combination that resides comfortably within the home row of your keyboard and is not commonly used in other command sequences.
However, please be aware that when typing
;;while in insert mode, it’s essential to allow a brief pause between the two key presses to avoid triggering any unintended actions
Useful editing motions in vim
There are a couple of motions and commands which you would like to (or you might have unconsciously wished to) leverage in your command line, but they need to be configured in .inputrc
- Repeating the last action or command - By default, pressing
.to repeats the last change made. You can bind!!command ofbashto re-input the last run command. - Deleting text - VIM provides several ways to delete text quickly. For example,
ddto easily delete the entire line andD(i.eshift-d) to delete the text in front of the cursor. - Additional groupings - Additionally other text groupings like forward word
w, backward wordb, inside somethingican be paired with deleted(e.gdw) to quickly delete specific texts groups - Changing stuff - Even easier, just delete the said stuff and go in insert mode, no need to manually type
iwith same combinations of the text groups - Finding characters: Vim allows you to find a character forward (
f) or backwards (F) to the cursor. Once you find the character, you can use;to go to the next occurrence and,to the previous occurrenceNOTE: incremental search with
/and?just like vim is not really supported
My thoroughly commented inputrc
Check the latest version in my dotfiles in an event I forget to update my findings here
How to source .inputrc
You can instruct readline to re-read the .inputrc file
Alternatively you can change the keymap of your preference to re-read-init-file
Editing long commands
Are you having trouble editing lengthy bash commands, such as those with numerous flags, and finding it tedious to search for and navigate to the specific section that needs modification?
Fear not! Pressing v in visual mode (you guessed it right, cause its vim) allows you to input the current command into a preferred text editor (as specified in your $EDITOR environment variable, shame if its not set to vim in your .bashrc ).
With this feature, you can efficiently make the necessary changes leveraging the full powers of your favorite text editor.
Upon saving the edited text, the updated command will be executed. No more wasted time manually updating flags while risking errors; breathe new life into repetitive, mundane workflows.
Join the ranks of true CLI wizards by embracing this underutilized technique, today. Witness the seemingly insurmountable challenge of managing convoluted commands
Caveats
-
Keep in mind that quitting without saving (from the visual mode) will still execute the original command.
TIP: Adding a “#” at the beginning of a command can help prevent its accidental execution!
-
Multiline commands, separated by a backslash (“"), may appear differently in searchable command history due to replacement with spaces. For example, entering
echo hello \n worldbecomesecho hello worldin the history log.PRO TIP: Make it a habit to review your entered command before executing to ensure accuracy and consistency in your workflow!
-
Separate entries are created for multiple commands in history search, meaning each command is treated independently, regardless of whether they were entered together or not.
References