Random bash & vi tips

terminal

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:
    :shell
  4. Run a command from within vi:
    :!ls
  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
    endif
    

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

  9. Quick spelling suggestions/completions (case-insensitive):
    $ look spellin
    spelling
    spelling's
    spellins
  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:
    #force_color_prompt=yes
  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 ipinfo.io/10.10.10.10
    OR
    $ wget -qO- ipinfo.io/10.10.10.10 | cat
    
    // To check your own public IP address
    $ curl ifconfig.me/ip
    OR
    $ wget -qO- ifconfig.me/ip | cat
    
    // For whois information of your IP
    $ whois $(wget -qO- ifconfig.me/ip | cat)
  20. To repeat the last colon command executed in vim press <@:> in command mode.
  21. To copy (yank) lines 26 to 41:
    :26,41y
  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

One thought on “Random bash & vi tips”

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s