find, grep and vim for Windows

win_appsI posted before on the powerful grep and find utlities. If you are a regular Linux user you’ll find them extremely useful. If you need to work on Windows and are missing these and the omnipotent vi editor, there are some free and light utilities available for you:

  • WinGrep – Has a GUI. Feature packed but often hangs when you use it on a huge set of files like the Linux kernel source code. Good for a few hundred files.
  • GNU grep & GNU find – Good old Linux grep and find ported to Windows. Fast and dependable.
  • GVim – Vim ported for Windows.

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

Turn vim into Source Insight

An earlier article on ctags and cscope may be a good starting point for the current one.

Source Insight is a very popular code editor cum browser on Windows and can be used on Linux over Wine as well. However, the same can be done using vim with some practice. The Trinity plugin for vim does it all. Trinity makes 3 more plugins work in unison to deliver you an IDE like experience. You can have the similar look and feel and functionality of Source Insight over vim at a reduced number of CPU cycles. And when using remotely… heaven!

Transform vim into the most powerful and lightweight IDE you ever used!

Features

  • Automatic Display of Declarations in the Context Window on the bottom in the (G)VIM window using the Source Explorer plugin
  • Symbol Windows For Each File on the left in the (G)VIM window (G)VIM using the Taglist plugin
  • Quick Access to All Files on the right in the (G)VIM window using the NERD Tree plugin

Installation

  • Download the latest version of Trinity
    $ git clone https://github.com/wesleyche/Trinity.git
  • Copy the trinity.vim and NERD_tree.vim files under plugin directory into the vim plugin directory e.g. ~/.vim/plugin

Configuration

Add the keybinds for the 3 plugins to your ~/.vimrc

" Open and close all the three plugins on the same time
nmap    :TrinityToggleAll

" Open and close the srcexpl.vim separately
nmap    :TrinityToggleSourceExplorer

" Open and close the taglist.vim separately
nmap   :TrinityToggleTagList

" Open and close the NERD_tree.vim separately
nmap   :TrinityToggleNERDTree

Webpage: Trinity

Some more useful tips:

ctags & cscope: the fastest IDE

vim_compIf you are a Linux developer, there is no alternative to the deadly combination of ctags and cscope when it comes to source code browsing and editing. I remember years back, out of mere curiosity I started learning them one evening and ended up in practicing throughout the night to get accustomed. I am a fan of ctags and cscope since then. They are much much lighter and faster than any other IDE I have laid my hands on.

ctags and cscope are the solutions to all your code browsing needs

Installation

To install on Ubuntu:

$ sudo apt-get install exuberant-ctags cscope

Usage

I added the following in my .bashrc to generate the tags and cscope file data:

function ta ()
{
    #clean older info
    rm -rf tags
    rm -rf cscope.files
    rm -rf cscope.out
    # generate new info
    find $PWD | egrep -i "\.(c|h|cpp)$" > cscope.files
    ctags -R . *.c *.h *.cpp --tag-relative=yes ./
}

To generate tags and cscope file information, navigate to the root directory of your project and run

$ ta

Troubleshooting on SuSE

I got the following error repeatedly on SuSE while trying to open tag search results (though it worked well on Ubuntu):

E429: File "/path/to/file.c" does not exist

Here are the steps I followed to fix it:

  1. Generate cscope.files with absolute path
    $ find /path/to/project/files | egrep -i "\.(c|h|cpp)$" > cscope.files
  2. Generate the tags file
    $ ctags -R . *.c *.h *.cpp --tag-relative=yes ./

A list of excellent tutorials, tips etc. to learn ctags & cscope:

The only plug-ins I use:

  • MiniBufExplorer
    NOTE: Add ‘set hidden‘ in your ~/.vimrc not to lose syntax highlighting when you close a buffer. This hides the buffer instead of closing them. vim has a bug which causes loss of syntax highlighting on a buffer quit.
  • Taglist
  • CScope maps
  • a.vim

Some pointers:

  • search word under cursor: <Shift-#>
  • find file with pattern in cscope: cs f fe file //check cscope_maps.vim for other switches
  • to list all possibilities: type :ts IPPR and then press <Ctrl-d>
  • <Ctrl-]> jumps to definition while <Ctrl-o> returns to previous location (double <'> does the same too). <Ctrl-i> jumps forward.

Some handy .vimrc entries to enjoy your mug of vi:

set ai //leads to scattered code when pasting in remote terminal (like PuTTY) on Windows,
//run set noai first to avoid.
set cindent //c like indentation
set ic //ignore case while searching
set incsearch //incremental search during typing
set hlsearch //highlight search matches
set ts=4 //tab length
set sw=4 //shift width, amount on shift you want in new lines
set nu //show line numbers

Happy coding! 🙂