Jump to a word in a command in Bash - bash

Consider:
ssh -i "key.pem" root#server.com
Is there a quick way to jump to the beginning of "root" in such a Bash command in the terminal, without iterating over every word/character?

Yes. Bring up the last command using up arrow. These shortcuts will help:
Ctrl + E - go to the end of the line
Ctrl + A - go to the start of the line
Alt + left - go back one word
Alt + right - go right one word
Ctrl + W - delete the last word
To search the command line and the command line history, use:
Ctrl + R and then type the search string, e.g. "roo" to search backwards for "root#...". In the example you have given, however, it will be easier just to jump forward or back by words.
Ctrl + S search forwards (rarely useful). (If forward searching doesn't appear to work, try this other Stack Overflow answer.)
See also this page and google "Bash command line editing" for more tricks.

Related

mac Terminal app:what is the shortcut for "goto next tab"?

In terminal app,when I enter "command + {" or "command + {",it will go next or before window, not next tab,who can tell me ,why?
You probably entered cmd[ and cmd] accidentally, since they do change the window.
Make sure to enter cmdshift[ and cmdshift] as they produce cmd{ and cmd} respectively.
This is because shift[ = {, etc., depending on your keyboard layout. (Just as you would press shift[ to type {, provided you're using the English or some similar keyboard layout.)
There are plenty of ways to switch tabs.
You can use command + shift + }, swipe with three fingers, or use command + [tab number, i.e. 1].
final ,I found ,the answer is "shift + command + }"。 but,in teminal menu,it show the "command + {" is shortcut for "show pervious tab"。

How to move right or left by 'x' characters in Bash?

In bash sometimes I have very long commands where I need to edit some words. Right now I use End/Home to move end/start of the command, but what if I have to move say x characters in a line?
I need something like xb/xw of VI, but instead of words I need to move characters.
What about ditching emacs mode and switching to vi mode editing?
set -o vi
and you have all the power of vi-like command line editing, like 3l to go left three characters and 5B to go back 5 words. The Pos 1 key then becomes 0 and End becomes $.
In emacs mode, you can use Meta3Controlb to move back 3 characters, and Meta3Controlf to move forward 3 characters. For multi digit counts, you need to precede each digit with the Meta key (e.g., to move 10 characters back, Meta1Meta0Controlb).
Meta is usually the Alt key, but may be the Esc key instead (on Mac OS X, for instance).
(Yes, vi-command mode makes it easier.)
There is a command, universal-argument, that allows you to type all the digits at once, but it is unbound by default. Bind it with, say,
bind "\C-a":universal-argument
then typing Control-a will enter you into an "argument" mode, prefixing the current line (arg: 4), and allowing you to type digits to change the argument used by the next non-digit character you type. (See universal-argument in the bash man page for the full details.)
You could use the command as below
Command:
cp some_file1 some_file2 some_file3 /root/Desktop
After executing the command do the following
^some_file2^some_file4
and it will execute the command
cp some_file1 some_file4 some_file3 /root/Desktop ;
What happened is the some_file2 is replaced by some_file4 and the command is executed

Sublime - delete all lines containing specific value

I have a 900mb log file which I can open in SublimeText 3. This file is bloated with lines similar to the following.
10/08/2014 23:45:31:828,Information,,,,ExportManager: ,No records to send and/or not connected
How can I filter out all the lines which contain No records to send and/or not connected
You can do a regular expression search-and-replace:
Click Find > Replace.
Ensure that the Regular Expression button is pressed.
For the Find What field, put:
^.*No records to send and/or not connected.*\n
Leave the Replace With field empty.
Click Replace All
For people that don't want to write a regex - you can just select the search string, hit ctrl+cmd+g or pick "Quick Find All" from the menu, which will get you selections for each matching string; from there Home will move every selection cursor to the start of the line, shift+End will select every matching line, and del, del will delete all of them.
Multiple cursor editing is fun!
i could not get the regex to work so I used Alt-F3 approach from this answer:
https://superuser.com/questions/452189/how-can-i-filter-a-file-for-lines-containing-a-string-in-sublime-text-2/598999#598999
Select string of interest
Hit Alt+F3 to go into multi-cursor mode on all occurrences (Ctrl+CMD+G on Mac OS X)
Hit Ctrl+L [see comments] (Cmd+L on Mac)
Copy-paste selection to another buffer
Del
This is what i found for the windows users:
Select the string (every line containing this string is to be removed).
Press ALT+F3 .
Press Ctrl+L .
Press Delete .
Neither of the regex code suggested above worked in my case, but this did work:
.*(text in question).*
A simple way of doing it is:
1 Open Sublime Text
2 Find => Replace (Ctrl + H)
3 in Find write the desired text
4 click Find All
5 press ctrl + shift + K to remove all the lines where this search is present
This is a quick solution to remove some lines that contains some text
Above answers are the correct ways, but if you want to get rid of the rows with even a single string then do,
Find -> Replace -> put ^.*[a-zA-Z]+.*\n In the find section and keep replace with blank. Hit the replace all button this will delete all the rows with even a single string in it.
I like the manual edition solution, very good.
But.. have you tried to use cat and grep -v to filter out the lines and redirect to another file? Maybe better than learning regex.. (personally I always start with regex and end with editing the files myself).
In Windows you use findstr /v.
So you would do:
# in bash
cat my.log | grep -v "No records to send and/or not connected" > new.log
or
# in cmd
cat my.log | findstr /v "No records to send and/or not connected" > new.log
I ran into a similar problem editing a sitemap
This worked for me:
Copy the last word in the lines that you want to delete
Find all
Press delete to delete the entire line
Find -> Find all (this will mark the lines having the keyword)
Then go to Edit->Line->Delete line

Bash keybinding with "bind" - avoid recursion like noremap in Vim

I've remapped the following keys in Bash:
bind '"a" "b"'
bind '"b" "c"'
If I press a or b both times a c will be printed.
How can I map the keys so that by pressing a and b will be printed and only by pressing a and c will be printed (like with Vims **nore**-map)?
You can make a char in the right part a literal with a preceeding "^V":
bind '"x":"^Vx "'
The key sequence to enter the text after colon is:
" Ctrl-v Ctrl-v x space "
In bash "Ctrl-v x" results in a literal x, without key-mapping interpretation .
You may find it useful to build your own keymapping file which maps the numeric signal you get from the keyboard to a character. This is done with the loadkeys command i.e. if you're switching to the dvorak layout you can do (this is a verbose way to show you the location of the map files:
loadkeys /usr/share/keymaps/i386/dvorak/dvorak.map.gz
You could copy the map file for the layout you use and change the relavent chars, and load your modified mapping. I think loadkeys only affects the command line, though there are similar methods for changing the behaviour in X I believe.

Why do some Vim mappings include <C-U> after a colon?

I'm trying to figure out the syntax of the mapping commands, like onoremap, in vim.
Specially, I am confused over this line in the manual, regarding the use of <C-U>:
The CTRL-U (<C-U>) is used to remove the range that Vim may insert.
Can someone explain this?
That isn't part of the syntax for the onoremap command, that is explaining what a particular mapping does. That mapping is:
onoremap <silent> F :<C-U>normal! 0f(hviw<CR>
So, when the F key is used while an operator is pending vim will replace that with the bits in the next argument to the onoremap command. That starts with a : to begin an ex mode command. If there is a visual selection when the mapping is used, vim will automatically insert the range '<,'> so that the following ex command will apply to the visual selection, leaving the command line looking like:
:'<,'>
The <C-U> in the mapping tells vim that after the : is entered the Control+U combination should be used to clear the command line, eliminating the automatically inserted range leaving the command line looking like:
:
Then the remainder of the mapping is used.
You can see this for yourself by using V to begin a line-wise visual selection, then : to start entering a command. The range will show up, you can then use Control+U to clear it just as the example mapping does.
The portion of vim help that contains that mapping explains the remainder of it.
The Ctrl-U Vim-map operates as the same short-cut from the terminal command line. Check: https://www.howtogeek.com/howto/ubuntu/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc/
Avoid remapping few of them (like which breaks out a process on a terminal), but the majority (like Ctr-A or Ctrl-X) can be remapped.
If your VIM is no terminal one (like gVim), you can remap them all inconsiderately.
Btw: Ctrl-Shift-Letter is like Ctrl-Letter map for VIM-terminal.
Some terminal short-cuts:
" copy-paste
" <C-S-c> copy
" <C-S-v> paste (or replace visual selected)
" manage running processes
" <C-c> break out of a command or process on a terminal. This will stop a running program immediately.
" <C-z> send a running program in the background
" <C-d> If you are using an SSH connection, it will be closed. If you are using a terminal directly, it will be closed
" control what appears on the screen
" <C-l> clear terminal screen
" <C-s> Stop all output to the screen. This is particularly useful when running commands with a lot of long, verbose output, but you don’t want to stop the command itself with Ctrl+C.
" <C-q> Resume output to the screen after stopping it with Ctrl+S.
" Moving the Cursor
" <C-a> or Home: move cursor to beginning of line
" <C-e> or End: "" end ""
" <C-xx> Move between the beginning of the line and the current position of the cursor. This allows you to press Ctrl+XX to return to the start of the line, change something, and then press Ctrl+XX to go back to your original cursor position. To use this shortcut, hold the Ctrl key and tap the X key twice.
" <A-b> go left 1 word
" <C-b> "" char (like left-arrow)
" <A-f> go right 1 word
" <C-f> "" char (like right-arrow)
" Cutting and Pasting
" <C-u> erases everything from the current cursor position to the beginning of the line
" <C-k> erases everything from the current cursor position to the end of the line
" <C-w> erase the word preceding to the cursor position. If the cursor is on a word itself, it will erase all letters from the cursor position to the beginning of the word.
" <C-y> paste the erased text that you saw with Ctrl + W, Ctrl + U and Ctrl + K shortcuts
" Deleting Text
" <C-d> or Delete: Delete the character under the cursor
" <A-d> Delete all characters after the cursor on the current line.
" <C-h> Backspace: Delete the character before the cursor.
" Fixing Typos
" <A-t> Swap the current word with the previous word.
" <C-t> Swap the last two characters before the cursor with each other. You can use this to quickly fix typos when you type two characters in the wrong order.
" <C-_> Undo your last key press. You can repeat this to undo multiple times.
" Capitalizing Char
" <A-u> Capitalize every character from the cursor to the end of the current word
" <A-l> Uncapitalize every character from the cursor to the end of the current word
" <A-c> Capitalize the character under the cursor. Your cursor will move to the end of the current word.
" Command History
" <C-p> like up-arrow: press it repeatedly to keep on going back in the command history
" <C-n> like down-arrow: use this shortcut in conjugation with Ctrl+P. Ctrl+N displays the next command
" <A-r> revert any changes to a command you’ve pulled from your history if you’ve edited it.
" <C-r> search in your command history. Just press Ctrl+R and start typing. If you want to see more commands for the same string, just keep pressing Ctrl + R.
" <C-o> Run a command you found with Ctrl+R
" <C-g> Leave history searching mode without running a command

Resources