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:
$ su $ 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 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.
- Quick spelling suggestions/completions (case-insensitive):
$ look spellin spelling spelling's spellins
- 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:
:set paste //to get back to normal mode :set nopoaste
- 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.
sscommand is similar to
netstat. it can show more information on TCP and state.
treecommand shows the current directory structure in a tree format.
pstreedoes 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 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)
- 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
]pin 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.
deletekeys don’t work in vim, add the following in ~/.vimrc:
- Quick stopwatch:
$ time read
- 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