While using bash there are many time you may need to copy an argument you have already typed. Instead of typing the argument again you can use the following shortcuts:
Ctrl-w : cut the word before the cursor
Ctrl-u : cut the part of the line before the cursor
Ctrl-y : paste at cursor location
Now if you want to copy the second argument video.mkv at the end in the following line and add .orig:
$ cp video.mkv
In a situation you need to run a command before the current one you already typed, use Ctrl-u to cut the whole line you typed, run the required command and again use Ctrl-y as:
$ vi myfile.c<
Ctrl-u> //to cut the whole line
$ dos2unix myfile.c //run the command you missed
<Ctrl-y> //copy back the old line
Here are some random bash and vim tips and tricks which might come handy every now and then:
- Instead of running multiple commands using sudo, issue any of the following once and then run all subsequent commands as root:
$ sudo bash
- To change the default base directory, add the following in ~/.bashrc:
- Run a shell from inside vi:
- Run a command from within vi:
- Split vi vertically and open a new file:
- vsplit opens a new file in the left. To swap the panes use
- To highlight the current line in vim:
- To force vim to remember the last position in a file opened earlier, edit /etc/vim/vimrc and uncomment the 3 lines as shown in the snippet:
" Uncomment the following to have Vim jump to the last position when
" reopening a file
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
Users will need to logout and login back for this to take effect.
- Quick spelling suggestions/completions (case-insensitive):
$ look spellin
- A smart way to remove all blank lines in a file:
$ cat filename|awk NF
- If you have missed running a command with sudo and want to do so, run:
$ sudo !!
- To add some colour to your bash prompt, uncomment the following in ~/.bashrc:
- To paste in vi without auto-indentation:
//to get back to normal mode
- To count items in a directory:
$ ls -1 | wc -l // count visible items
$ ll -a | wc -l //count hidden files (includes . & ..)
fish shell has a builtin function to do this:
$ count (ls -1)
<Ctrl-x-e> in the terminal and your default editor will open up.
ss command is similar to
netstat. it can show more information on TCP and state.
tree command shows the current directory structure in a tree format.
pstree does the same for processes.
- If you are looking for a restricted environment for users of your server, check out rbash.
- Lookup IP address and geographic info in bash:
$ curl ipinfo.io/10.10.10.10
$ wget -qO- ipinfo.io/10.10.10.10 | cat
// To check your own public IP address
$ curl ifconfig.me/ip
$ wget -qO- ifconfig.me/ip | cat
// For whois information of your IP
$ whois $(wget -qO- ifconfig.me/ip | cat)
- To repeat the last colon command executed in vim press
<@:> in command mode.
- To copy (yank) lines 26 to 41:
- To indent, use
>. To indent 10 lines:
10>> To visually mark a block of lines and indent:
vjj> (v for visual mode, j to select one line and move to next, > to indent. To indent a block within curly braces, place cursor on one of the curly braces and use
>%. To auto-indent text while coping and pasting a block, use
]p in place of the usual
- Keyboard shortcuts for people who are too lazy to type in when they want to exit vim: To save and exit from command mode:
<Shift-z-z> To discard changes and exit:
- To save a file opened as a regular user and save as root from vim:
:w !sudo tee % Just reload the file when asked for.
delete keys don’t work in vim, add the following in ~/.vimrc:
- Quick stopwatch:
$ time read
<Ctrl-d> to stop.
- Clearer mount output:
$ mount | column -t
Here are my ~/.vimrc contents, if interested:
It is rather difficult to remember the case of each and every filename. And while trying to look for it, or open it from vi, you must remember the filename if you are relying on TAB completion. There is a way to ignore the case for tab completion as I could find from the bash manual.
Add the following line in /etc/inputrc:
set completion-ignore-case on
Now as long as you remember the name of the file TAB completion will always work!
The following website has a great collection of Linux cmdline (bash/shell) tips, tricks and hacks submitted by enthusiasts over years. Most of them run on bash. Learn and boost your geekdom!
There are many instances when you want to redirect the whole output from a command in a file for further analysis. Sometimes you can redirect easily and sometimes the output just floods your terminal instead of the file specified. For example:
$ dmesg > output.txt //works
$ strace ls > output.txt //doesn't work
There is a simple way to make the second line do what you intend to do. Simply add a 2>&1 at the end of the command:
$ strace ls > output.txt 2>&1 //works now
If you know about the common Linux file descriptors you already know the explanation ;).
While we are at it, we should not forget about tee. It allows you to see the output as well as write the output to a file. Example usage:
$ cat .rtorrent.rc | tee torrent_settings
min_peers = 5
max_peers = 15
min_peers_seed = 1
max_peers_seed = 1
max_uploads = 1
max_uploads_global = 1
max_downloads_global = 3
download_rate = 0
upload_rate = 1
dht = auto
dht_port = 6881
peer_exchange = true
If you are on SliTaz you are using the ash shell and not bash. Here‘s the information from the official SliTaz docs on how to set aliases or make bash your default shell in SliTaz. If you prefer to stick to the ash shell here‘s all you need to know about it.
To add some glamour to the bash terminal and add ASCII art check in ArchWiki. It guides you on how to use cowsay/cowthink and fortune-mod.
Not sure what the shell is? Learn shell basics.
More useful websites related to Linux here.
If you spend a lot of time at the shell prompt, recording shell history can save time and typing. But there are a few annoyances with history, if left unmodified: History records duplicate commands, and multiple shell instances can clobber each other’s history. Both complications are easily overcome. Add two lines to your .bashrc:
shopt -s histappend
The first line removes consecutive duplicate commands from your shell history. If you want to remove all duplicates independent of sequence, change
erasedups. The second line appends a shell’s history to your history file when the shell exits (more on shopt). By default, the Bash history file is named (yes, a dot file) ~/.bash_history. You can change its location by setting (yes, an environment variable) HISTFILE. If you want to save a shell’s most recent 10,000 commands in a history file with 100,000 entries, add
export HISTSIZE=10000 HISTFILESIZE=100000 to your shell startup file. To see a shell’s history, type
history at any prompt.
Saving a history of commands is of little use if you cannot recall it. That’s the purpose of the shell
!, or bang, operator:
!! (“bang bang”) repeats the last command in its entirety.
!:0 is the name of the previous command.
!^ is the first argument of the previous command.
!:3, and so on, ending with
!$ are the second, third, and eventually the last argument of the previous command.
!* is all the arguments of the last command, except the command name.
!n repeats the command numbered
n in history.
!handle repeats the last command that begins with the string of characters in
handle. For example,
!ca would repeat the last command that began with the characters
ca, such as
!?handle repeats the last command that contains the string of characters in
handle. For example,
!?READ would also match
^original^substitution replaces the first occurrence of
substitution. For example, if the previous command was
cat README, the command
^README^license.txt would yield a new command
!:gs/original/substitution replaces all occurrences of
!:gs means “global substitution.”)
!-2 is the penultimate command,
!-3 is third most recent command, and so on.
- You can even combine history expressions to yield sigil soup such as
!-2:0 -R !^ !-3:2, which would expand to the command name of the penultimate command, followed by
-R, the first argument of the previous command, and the second argument of the third most recent command. To make such cryptic commands more readable, you can expand history references as you type. Type the command
bind Space:magic-space at any prompt, or add it to a startup file to bind the Space key to the function magic-space, which expands history substitutions inline.
Quick tip: Add a space before a command and it won’t be saved in history.
Very useful shortcuts for efficiently using bash:
|Ctrl + A
||Go to the beginning of the line you are currently typing on
|Ctrl + E
||Go to the end of the line you are currently typing on
|Ctrl + L
||Clears the Screen, similar to the clear command
|Ctrl + U
||Clears the line before the cursor position. If at the end of the line, clears entire line
|Ctrl + H
||Same as backspace
|Ctrl + R
||Lets you search through previously used commands
|Ctrl + C
||Kill whatever you are running
|Ctrl + D
||Exit the current shell
|Ctrl + Z
||Puts whatever you are running into a suspended background process (fg restores it)
|Ctrl + W
||Delete the word before the cursor
|Ctrl + K
||Clear the line after the cursor
|Ctrl + T
||Swap the last two characters before the cursor
|Ctrl + X
||Jump between current cursor position and beginning
|Ctrl + X + E
||Edit the command in text editor and run
|Esc + T
||Swap the last two words before the cursor
|Alt + F
||Move cursor forward one word on the current line //Refer this post.
|Alt + B
||Move cursor backward one word on the current line
|Alt + T
||Auto-complete files and folder names