Skip navigation.

Beyond the Sky

The place where surface stop and share the experience of life

Posts tagged with "vim"

continue to use vim, forget about learning emacs

,

I was moved, when my friend show me how good Emacs is about. I try out Emacs, try to read up tutorials about Emacs. Emacs is a fancy tools, emacs support a lots of stuff, it allows you to read emails, news in emacs. Emacs allows you to do coding efficiently, auto indent, you can highlight the codes and indent it nicely, which it is one of the reasons I give Emacs a try. Emacs have a lots of mode, I can't remember all and also not sure when I in which mode and when I in other. I just know when I in Emacs, I am out of control and I mess up everything.

For me, I just need a simple editor, editor that does editing / coding jobs and also I can customize features that I want to use, such as do shortcut key mapping, add in plugins which I like to have. Just like firefox, just like fluxbox, lightweigth but flexible. I find emacs too complecated, I don't want to read news, mails, I just want to do some coding and scripting. I dislike the combo key of emacs. I have to press so much key to do a simple thing. Control something meta something, what the heck is meta man, alt. Again I can't find a way to get tabview working in emacs. If you know, show me how.

Therefore I stop learning up emacs. On other hand, I look into what vim can do to fulfil my needs.

Bellow are my simple needs:

1. Auto indent
2. Auto complete
3. Tabs
4. Define own shortcut key
5. Syntax with different colors
6. Plugins to enable more features that I haven't think of

Vim 7 can fulfil all my needs, maybe emacs also can, but vi is easy, available on every unix/linux/bsd. Not agree? show me with your emacs power. With a simple .vimrc in home directory, it works. I am still changing the .vimrc to makes my key map better to serve my humble needs. Bellow are my current .vimrc

" syntax color and scheme
syntax enable
colorscheme evening

" correct the backspace
set backspace=2

" indent and others plugin based on filetype
filetype plugin indent on

" indent setting
set smarttab autoindent
set tabstop=8
set expandtab
set sw=4

" turn of the highlighter by default
set hlsearch!

" key mapping
" tab navigation
:map <F3> :tabprevious <cr>
:map <F4> :tabnext <cr>
:map <C-Tab> :tabnext <cr>
:map <F2> :tabnew . <cr>

" others
:map <F5> : set nu! <CR>
:map <F6> : set hlsearch! <CR>
:map <F9> : shell <CR>

" indent
:map <F7> <ESC> ggv<S-g>=


First two line, I enable syntax coloring, and also use a nice scheme, black background, white text. Migrate from vim 6 to vim 7, backspace becomes not functioning the way I want it, set backspace=2 fix that. filetype plugin indent on enable all the plugin and indentation base on different file format. You can check out what is the rules and how it set up the rules to make indentation and other plugins works at
 /usr/share/vim/vim70 


In this folder, contains couple of sub folders such as indent, plugins, syntax etc. Take a look at them, it is all scripts for vim. You can add more plugins, find more plugins and tips at the official website (http://www.vim.org/)

Next four lines is for indentation, tabstop is when you press tab at insert mode it leaves how many blanks, sw is the blanks it leaves when you do indentation. set hlsearch! is to highlights the keyword you select. I donno why by default it is turn on, I have no idea to turn it off elsewhere by default, so I did that at here.

Tab navigation is the best part which vim opens files into tabviews and I map the key for navigate between the tabs. set nu! is to toggle the line number besides the code for better reference. Without putting ! it works to enable, with ! it allows toggle. The last line is to do indentation for the source code. When I press F7, the source code will be indented nicely.

If you understand how the key mapping works, you can easily define your own key mapping. First of all, You have to understand vim have 3 simple modes ( maybe more, but I concentrate on 3 simple one)

1. Normal
2. Visual
3. Edit

When you in normal mode, which is the default mode, you cannot insert text, but you can navigate around, search for string to match, copy , cut and paste text. When you want to edit or insert text, you can type "i" ( without quote) in normal more and it will leads you to edit mode. After that if you want to return back to normal mode, you need to press ESC. Thats it you need to know for the mode switching. Visual mode is lessly use, it is when you want to highlight strings to perform copy (yank) or indentation. To enter visual mode, you press "v" (without quote) in normal mode. When you want to exit vim and save your file, you must first come back to normal mode, press ESC and type
":x" (without quote, it is colon and character x)

Vim supports a lots of command when you are in the normal mode, one of them is to exit and save shows at above. Most of the commands start with ":", key mapping is also start with ":". You can do key mapping when your are using vim. The reason why we put all key maps into .vimrc is because you want it to run everytime you use vim. So "map" is a command, it uses to map a key to series of actions.

I map F6 to another command call hlsearch and it allows toggle with ! , CR is actually carry return, which is to execute the commands. So it becomes like this:
:map <F6> : set hlsearch! <CR>


Lets look at another example:
:map <F7> <ESC> ggv<S-g>=

I map F7 to do the list actions. They are press ESC to go back to normal mode in case it is at another mode. Then gg is to go to the first position of the opened file. v is to get in visual mode. S-g is Shift g which force the cursor to go to last line. This leads the entire file is selected, and last action =, performs the indentation.


The snapshot above is the console vim with tabviews.

By default if you open multiple files with vi, it will not open in tabs view, you need to do -p like
vim -p file1 file2 file3


Sometimes I may forget to type -p, seems I always like to open with tabviews, I can insert an alias to .bashrc
 alias vi="vim -p" 


Next time when I type vi with list of filename, it will automatically open in tabs.

Auto Complete:
Another best feature of vim is auto complete, type to type something halfway and press Control p. It will list down all the possible words in list. This is useful when you are searching for the functions of c/c++. Okay, I know Emacs can do that too.


There are more plugins in http://www.vim.org including tetris game :smile:

Summary:
If you are vim user, and thinking of switching to Emacs or you already switched to emacs. Its time to come back for vim. If you are nano, gedit, kate user, I have an advice for you. Vim is not as simple as the editor you use, but it is not difficult to learn up compare to emacs. Give it a try, type vimtutor in your terminal. If you are windows user, don't worry, go download gvim for windows, it works fine. If you are EMACS user, and read until this line, heh! I am glad you are still reading. Show me how EMACS can do better P:

Ref :
1. http://www.vim.org/
2. http://www.ukuug.org/events/linux2004/programme/paper-SMyers/Linux_2004_slides/vim_tips/
3. http://mysurface.no-ip.org/blog/?cat=15

vim 7.0.12

, ,

Finally I got my vim 7.0.12 compiled and installed.

I download vim latest source code from www.vim.org based on svn

svn co https://svn.sourceforge.net/svnroot/vim/vim7


Then, get update in the vim folder

cd vim7
svn update


I configure with --enable-gui and --enable-scope
./configure --enable-gui --enable-scope
make
make install


I have no problem with make and make install, so I am not sure what dependencies to solve. gvim seems cool, I have to say bye bye to gedit, I am using gvim now :smile:

vim with cscope

,

Another tools that improve c source code reading beside ctags. With cscope, it make function call search more effective. It allows you to search the function, enum, class within the source code in same directory and also library header such as stdlib.h stdio.h etc.

Its easy to start working with cscope, first apt-get install cscope, at the source code directory type 'cscope -R'. Copy cscope_maps.vim to vim/plugin location, if your vim is 6.x.x. Then, vi your source code and move your cursor to the keyword you wanna search, press 'ctrl \' 's'

The result will be shown at the list with numbers, pick one. To go back 'ctrl t'.

p/s:
my vim plugin directory at /usr/share/vim/vim63/plugin/

This is the complete guide line.
http://cscope.sourceforge.net/cscope_vim_tutorial.html

vim with ctags

,

If you are c/c++ programmer and vim user, maybe know ctags. Most of the time, source code are distributed to many folders and each of them consist of numbers of .cc/.c and .h. Sometimes to search the definition of a function, class, enums etc by using "/" is inefficient. ctags allow fast jumping to function call even the function definition source code are from other directories.

apt-get install ctags


In order to use ctags, first run the command at destination directory where the source codes are located.

ctags -R *

-R is to recursively generate ctags file across certain directories. A 'tags' file will be created. If you have learn vi from vimtutor, you may know that to jump from one keyword to targeted location is to press 'ctrl ]'. Same as ctags works.

Let say when you discover a function call which you wanna see the definition, simply point the cursor to that function and press 'ctrl ]' and it will brings you there. If you wants to go back to where you came from, simply press 'ctrl t'.

Bare in mind, if the function calls are from libraries such as stdio, curses etc, you won't be able to jump to.



make a different vim

, ,

Are you a vim user? Vim can be very efficient if you do some settings. Let me throw my 2 cents here

Create a file ~/.vimrc and paste bellow text
        syntax enable
        colorscheme evening

        map <F2> : edit
        map <F3> : bp <CR>
        map <F4> : bn <CR>
        map <F5> : set nu <CR>
        map <F6> : set nonu <CR>
        map <F7> : set hlsearch <CR>
        map <F8> : set nohlsearch <CR>
        map <F9> : shell <CR>


You may want to enable your syntax, where you can have different color on your syntax

My fav colorscheme, you can create you own color scheme

map is to map or function key to a useful command, <CR> means return ( Enter)

I map F2 to edit another file, with this you can press F2 and type in the file you want to edit. *

F3 is to go next and F4 is to go previous. with F2 you can edit many page and with F3 and F4 you may switch between them. Just like tab, me personally like this instead of split

F5 is to shows the line number besides every line and F6 is to off it

F7 is to make your search keyword highlighted and F8 is to off it

F9 is to temporary go back to shell, type exit on the temp shell to come back vim

* By pressing F2 and type in (dot) #:edit . You can browse the folder and select the file you wanna edit, press enter on folder to go to the folder, press enter on file to edit the file.

Please share your tips in comments, we learn together ! Lets vim!!!
:x

vi is vim

, ,

I discover that vi we used nowadays is not the conventional vi. It is vim instead.

root@matrix:~# which vi
/usr/bin/vi
root@matrix:~# ls -l /usr/bin/vi
lrwxrwxrwx  1 root root 20 Dec 17 22:00 /usr/bin/vi -> /etc/alternatives/vi
root@matrix:~# ls -l /etc/alternatives/vi
lrwxrwxrwx  1 root root 12 Dec 17 22:00 /etc/alternatives/vi -> /usr/bin/vim


To know more about what is the difference between them, http://www.polarfox.com/vim/manual/v61/vi_diff.html

Anyway it claims that vim are 99% similar to vi, so you won't realized that you are using vim instead of vi.
December 2009
S M T W T F S
November 2009January 2010
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31