Try and download color schemes for vi editor from Bytefluent. You can try the same scheme for various programming languages in one page; no need to download the scheme and apply it.
Tag: vi editor
Power tips for vim
Post moved here.
find, grep and vim for Windows
I 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:
Random bash & vi tips
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:
export CDPATH=/etc
- Run a shell from inside vi:
:shell
- Run a command from within vi:
:!ls
- Split vi vertically and open a new file:
:vsplit newfile
- vsplit opens a new file in the left. To swap the panes use
<Ctrl-w-r>
. - To highlight the current line in vim:
:set cursorline
- 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:
#force_color_prompt=yes
- 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)
- Press
<Ctrl-x-e>
in the terminal and your default editor will open up. - The
ss
command is similar tonetstat
. it can show more information on TCP and state. - The
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 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:
:26,41y
- 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 usualp
. - 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>
- 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. - If
backspace
anddelete
keys don’t work in vim, add the following in ~/.vimrc:set backspace=indent,eol,start
- Quick stopwatch:
$ time read
Press
<Ctrl-d>
to stop. - 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:
Why use vi?
The following article shows some excellent usage examples as well as arguments on why vi is the most popular editor even today –
Why, oh WHY, do those #?@! nutheads use vi?
Some more usage specific examples –
Seven habits of effective text editing
ctags & cscope: the fastest IDE
If 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:
- Generate cscope.files with absolute path
$ find /path/to/project/files | egrep -i "\.(c|h|cpp)$" > cscope.files
- Generate the tags file
$ ctags -R . *.c *.h *.cpp --tag-relative=yes ./
A list of excellent tutorials, tips etc. to learn ctags & cscope:
- Browsing programs with tags
- The Vim/Cscope tutorial
- Jumping to the declaration of a local/global variable
- Vi and Vim Editor: 12 Powerful Find and Replace Examples
- ctags and cscope
- Ctags and Taglist
- Jumping to previously visited locations
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! 🙂