{
  "$type": "site.standard.document",
  "canonicalUrl": "https://justingarrison.com/blog/2020-05-28-shell-shortcuts",
  "coverImage": {
    "$type": "blob",
    "ref": {
      "$link": "bafkreicajycb7ed5lrybsjllz6ggz737u3bowbudwnselvwd5dhco7v4se"
    },
    "mimeType": "image/gif",
    "size": 60300
  },
  "description": "My experience switching from an engineer to a developer advocate",
  "path": "/blog/2020-05-28-shell-shortcuts",
  "publishedAt": "2020-05-28T00:00:00.000Z",
  "site": "at://did:plc:p7uix7mresfq4nfzxp3klgfa/site.standard.publication/3mmdn7mg2qm2d",
  "textContent": "If your an engineer you probably spend a lot of time using a terminal. Some of your best productivity gains will come from understanding it better and making small improvements to repetitive tasks.\n\nMany people know the basics for default shell shortcuts like Ctrl+a and Ctrl+e to go to the begining and end of a line. There are also a few keyboard shortcuts you’ve probably found by accident like Ctrl+s, Ctrl+z, or Ctrl+c.\n\nHere are some keyboard shortcuts I use regularly that you might find helpful too. I’m focusing on zsh because that’s my preferred interactive shell but many of these shortcuts will work in bash or other shells too.\n\nIf you want more shell tips, check out my mastering zsh workshop https://github.com/rothgar/mastering-zsh.\n\nWe’ll start with some basic keyboard shortcuts and get progressivly more advanced. Many of these shortcuts are default behavior but some will need code added to your configuration to enable them.\n\nBasics\n\nI’m amazed how long it took me to learn these keybord shortcuts and see that people still don’t know them.\n\nCtrl+r: Reverse search through your command history. Kinda like pushing the up arrow with search.\n\nCtrl+c: kill/interupt the current running process. If you don’t want it to print ^C every time you press it you can add set echo-control-characters Off to your ~/.inputrc file.\n\nCtrl+d: Close your current terminal. Same as typing exit but way shorter.\n\nCtrl+l: clear the screen. Similar, but slightly different, to typing clear.\n\nCtrl+z: Suspend process. This is really helpful if you’re in your editor and you need to run a command real quick. Instead of opening a new terminal press ctrl+z, run your command, and then type fg to bring the command back to the foreground.\n\nDelete word behind your cursor\n\nIf you have a command typed out or selected from your history with Ctrl+r you probably want to change one or two words from it.\n\nFirst you can move your cursor back a word with Alt+b. Press it multiple times to keep moving back.\n\nOnce your cursor is at the end of the word you need to change/delete press Ctrl+w to delete it. If it doesn’t delete up to the characters you expect try setting your $WORDCHARS variable to characters that should be considered part of the word.\n\nDon’t repeat your whole arguments\n\nIf you need to move a file to something like a $FILE.bak you can do that easily with {,}.\n\nDon’t do this\n\nDo this\n\nIt works anywhere in the command too! This will copy name.txt into new-folder (assuming it exists).\n\nEnter last argument from previous command(s)\n\nThere is a default shell variable $? which represents the last argument from the preceeding command (e.g. echo $?) Alt+. is the keyboard shortcut to do the same thing. Not only is it easier to type but you can push it multiple times to cycle through the last argument from previous commands.\n\nPrepend sudo\n\nI need this a lot. You type out a command or find it from your history and need to run it with sudo but forgot it at the begining of the line. Normally you would Ctrl+a type sudo then Ctrl+e. This shortcuts does all that with a single shortcut.\n\nAdd this code snippet to your .zshrc I prefer esc, s for the keyboard shortcut because it works well in vim mode and doesn’t clobber the default Ctrl+s shortcut.\n\n!img\n\nIf you’re using oh-my-zsh you can add the sudo plugin and the default key binding is esc, esc.\n\nOpen command in $EDITOR\n\nThere is a really handy command fc which will open your last command in your $EDITOR. It’s great for long or multi-line command edits.\n\nThere are lots of other ways to accomplish editing the last command you can check out here.\n\nTo edit your current command in your editor add this snippet to your .zshrc\n\n!img\n\nNow next time you copy/paste a multi-line command from somewhere it’ll be easy to edit.\n\nCut current line and then paste it on your next prompt\n\nIf you need to run a command before the command you currently have typed you can cut/yank it and then paste/pop it at your next prompt with yank-pop. This gif explains it better.\n\n!img\n\nIn zsh this is called push-line-or-edit and it’s a default widget you can enable with.\n\nA bonus feature is your cursor line will be in the same place after the command is pasted to the new prompt.",
  "title": "Shell Shortcuts"
}