bash: cut paste on cmdline

terminalWhile 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<Ctrl+w><Ctrl+y><space><Ctrl+y>.orig

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

Random bash & vi tips


Here are some random bash and vim tips and tricks which might come handy every now and then:

  1. Instead of running multiple commands using sudo, issue any of the following once and then run all subsequent commands as root:
    $ su
    $ sudo bash
  2. To change the default base directory, add the following in ~/.bashrc:
    export CDPATH=/etc
  3. Run a shell from inside vi:
  4. Run a command from within vi:
  5. Split vi vertically and open a new file:
    :vsplit newfile
  6. vsplit opens a new file in the left. To swap the panes use <Ctrl-w-r>.
  7. To highlight the current line in vim:
    :set cursorline
  8. 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
    if has("autocmd")
      au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif

    Users will need to logout and login back for this to take effect.

  9. Quick spelling suggestions/completions (case-insensitive):
    $ look spellin
  10. A smart way to remove all blank lines in a file:
    $ cat filename|awk NF
  11. If you have missed running a command with sudo and want to do so, run:
    $ sudo !!
  12. To add some colour to your bash prompt, uncomment the following in ~/.bashrc:
  13. To paste in vi without auto-indentation:
    :set paste
    //to get back to normal mode
    :set nopoaste
  14. 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)
  15. Press <Ctrl-x-e> in the terminal and your default editor will open up.
  16. The ss command is similar to netstat. it can show more information on TCP and state.
  17. The tree command shows the current directory structure in a tree format. pstree does the same for processes.
  18. If you are looking for a restricted environment for users of your server, check out rbash.
  19. Lookup IP address and geographic info in bash:
    $ curl
    $ wget -qO- | cat
    // To check your own public IP address
    $ curl
    $ wget -qO- | cat
    // For whois information of your IP
    $ whois $(wget -qO- | cat)
  20. To repeat the last colon command executed in vim press <@:> in command mode.
  21. To copy (yank) lines 26 to 41:
  22. 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 p.
  23. 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: <Shift-z-q>
  24. 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.
  25. If backspace and delete keys don’t work in vim, add the following in ~/.vimrc:
    set backspace=indent,eol,start
  26. Quick stopwatch:
     $ time read

    Press <Ctrl-d> to stop.

  27. Clearer mount output:
    $ mount | column -t
    $ findmnt

Here are my ~/.vimrc contents, if interested:

set nu
set ai
set incsearch
set hlsearch
set ts=4
set shiftwidth=4
set ic
set cindent
set cursorline
set splitright

nmap  :TrinityToggleNERDTree
nmap  :TrinityToggleTagList

bash: case-insensitive filename completion

terminalIt 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!

Redirect full output of Linux commands to file

terminalThere 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

All about the ash/bash shell & Slitaz

terminalIf 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.

All about bash history


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:

export HISTCONTROL=ignoreboth
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 ignoreboth to 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. !:2!: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 cat README.
  • !?handle repeats the last command that contains the string of characters in handle. For example, !?READ would also matchcat README.
  • ^original^substitution replaces the first occurrence of original with substitution. For example, if the previous command was cat README, the command ^README^license.txt would yield a new command cat license.txt.
  • !:gs/original/substitution replaces all occurrences of original with substitution. (!: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.

bash shortcuts

terminalVery 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 Swap words
Tab Auto-complete files and folder names