On this page I show some commands which you might forgot. For me it's to quickly look up how to do it again. So let's start.
Showing posts with label Vim. Show all posts
Showing posts with label Vim. Show all posts
Thursday, December 12, 2013
Thursday, December 5, 2013
Vim - Nice little handy tips and tricks
I found some nice little tricks to perform some actions a little bit faster
Toggle toggle toggle...
If you want to set line numbers you do like this:
:set number
you can even make it a bit shorter"
:se nu
To disable line numbers:
set no number
or
set no nu
But you can also toggle by adding an exclamation mark '!'
:se nu!
Another toggle:
:se cul!
This is to put a line under the cursor, some like it, some not. I really like it!
Sources: http://learnvimscriptthehardway.stevelosh.com/
Another one:
I often like to load a bunch of files at once and when I'm done editing with a file, I'll get another buffer to edit (":h buffer" for more information). But sometimes I want 2 files next to each other, just to see if they look similar (but not identical). In that case I'd like to have 2 files open next to each other, so I want the window split in two vertical windows. This is how we can do that:
:vert sb x
Where x is the buffer number you want, assuming you already loaded a bunch of files. Now you get two windows with current buffer on the right window and buffer x on the left. Cool!
Compare 2 buffers:
Ok, so the above one creates 2 windows, each containing a buffer. Suppose you have a big file like a few thousand lines and want to check the difference, no problem with Vim. On each window you execute the command:
:diffthis
or shorter,
:difft
To turn off:
:diffoff!
With the exclamation mark, it turns off for all windows.
To jump to the next difference:
]c
read as "go to the next compare". The other way around is:
[c
To obtain the difference from the other file to the current file:
do
Note, this is per difference, so not the whole file at once. The other way around:
dp
Exercise with it, you'll love it!
By the way, I mapped diffthis and diffoff as:
nmap
nmap [Leader]
replace [ with <, this blogger automatically removes '<', I guess because of security point of view (xss).
Other cute commands:
If you have multiple windows, you can close them all except the current one by this:
:only
There is also:
:hide
which closes the current window, but than I rather use Ctrl+w c, which is faster.
Follow the breadcrumbs:
One of the important things an editor should have is navigating forward and backwards my cursor positions. Say you edited a function or a method in your source code, than you jump to another function to do some other editions there or copy a snippet of code, the next thing you want to do is go back to the previous position. In normal mode you type:
Ctrl+o
for jumping back to the previous position, to jump forward again:
Ctrl+i
This is a very useful and powerful Vim command, a must-have desperately tool.
Forgot to sudo:
Very often I forget to add sudo if I want to edit a file with root permission, here is the remedy:
:w !sudo tee %
This one is hackish :)
Let's hex :
As a programmer, coder or hacker you need to check the binary data of a file. For example, you want to check the jfif segment (wiki-jpeg) of a jpeg file. To analyze the data, you can issue the command:
:%!xxd
to go back to the original view:
:%!xxd -r
So light, yet so powerful.
Load multiple files from within Vim:
Sometimes when writing code in Vim, I just need some other files to edit or maybe just to look at some code. Here's how you can do that:
:args somefolder/*.php
or if you want to browse to a folder and than load you can do the following:
:e .
This makes Vim as file browser, than you go to a certain folder, i.e. 'somefolder', than select those files with regular expression by first typing:
mr
than type:
*.php
and than type:
me
So by typing mr you open a regular-expression shell to select the matching files, than type the matching expression and than me to edit the files that match with the regular expression.
Insert date:
I like this one too, inserting date in the current file on the current cursorposition:
:r !date
Are you ready for this:
I sometimes accidentally close a file I was working on and to type
:e myfolder/subfolder/somecrazyfile.crz is just too annoying. But check out the power of Vim:
:ls!
shows you the recently closed files, than type:
:xb
and boom! Your recently closed file is back, where x is the id number of one of the closed files.
But this can be done faster! Check this, really amazing, hit the following keys:
Ctrl+o
You're back to the last cursor position in you last closed file! Amazing, just ONE key combination, amazing!
No plugins, just Vim! I'm sorry, I don't know if Emacs can do that :P
Copy/paste tricks:
I was a bit frustrated to copy a word and than replace another word with the copied one. I don't know why I didn't figured it out much earlier, but check this how to do it:
yiw is to copy the word where your cursor is on it, than do:
viwp this selects the word and than paste it with the yanked word earlier. Cool huh.
If you want to do this for another word, do this:
viw"0p meaning replace it with the word from register 0. When you yank or delete a word, the word is automatcially saved in register 0. You select the register by " and than a number. And of course, p is for paste.
When you yank a complete line with yy, you can than replace another line with:
Vp
To do this again for another line:
V"0p
I found this handy trick on: http://vim.wikia.com/wiki/Replace_a_word_with_yanked_text
Wednesday, January 23, 2013
Pathogen, the Vim plugins manager
Pathogen
After installing a few plugins I started to get a little bit annoyed by copying the plugin files to the .vim folder after downloading. It's not a big problem though, but after some plugins, it starts to get messy. You can't see directly what plugins you've installed or what could cause problems and so on...
So to manage the plugins in a proper way, I use Pathogen to do the job. What you have to do is download the plugin from:
https://github.com/tpope/vim-pathogen
Copy the 'autoload' folder to your .vim folder. Actually, the first time you use Vim you should install pathogen. Create another folder in .vim and call it 'bundle'. The bundle folder is where you put all your future Vim plugins:
/.vim
|___ autoload/
|___ bundle/
|___ snipmate/
|___ taglist/
|___ .../
So every time you download a plugin, you just copy the complete plugin folder in .vim/bundle/
You could also go in the .vim/bundle/ folder and download a plugin using Git, like this:
Assuming you're in the .vim/bundle/ folder.
# git clone https://link.to.vimplugin.git
You just need a small piece of script added in your .vimrc:
That's it! Try this out with snipmate plugin, as this plugin doesn't need any other configuration in your .vimrc file. If it works for you, than you can download all your favorite plugins and all are hierarchically structured.
After installing a few plugins I started to get a little bit annoyed by copying the plugin files to the .vim folder after downloading. It's not a big problem though, but after some plugins, it starts to get messy. You can't see directly what plugins you've installed or what could cause problems and so on...
So to manage the plugins in a proper way, I use Pathogen to do the job. What you have to do is download the plugin from:
https://github.com/tpope/vim-pathogen
Copy the 'autoload' folder to your .vim folder. Actually, the first time you use Vim you should install pathogen. Create another folder in .vim and call it 'bundle'. The bundle folder is where you put all your future Vim plugins:
/.vim
|___ autoload/
|___ bundle/
|___ snipmate/
|___ taglist/
|___ .../
So every time you download a plugin, you just copy the complete plugin folder in .vim/bundle/
You could also go in the .vim/bundle/ folder and download a plugin using Git, like this:
Assuming you're in the .vim/bundle/ folder.
# git clone https://link.to.vimplugin.git
You just need a small piece of script added in your .vimrc:
execute pathogen#infect()
syntax on
filetype plugin indent on
That's it! Try this out with snipmate plugin, as this plugin doesn't need any other configuration in your .vimrc file. If it works for you, than you can download all your favorite plugins and all are hierarchically structured.
Tuesday, January 22, 2013
Vim plugins for C/C++ development
To use Vim as an editor for C/C++ (and other programming languages as well), we need to extend Vim with some handy plugins. Vim is a powerful editor, it really is, but you need some plugins to make it more powerful to your needs. Vim is not a code editor out of the box, so you can not expect to have cool stuff like Visual Studio's intellisense, or jumping to definition of a function and so on. So to make our coding life easier, we can install Vim plugins to have more fun while coding.
First, here's a list of Vim plugins below I use it for writing code:
Matchit
This is a nice plugin too, especially when dealing with html files. It searches for matching tags using the existing % key. In a C file, if you're on one of the brackets like {}, [], () and so on, you'll see those brackets highlighting in pairs. When hitting % the cursor jumps to the matching pair, but normally this won't work for a html tag. Without this plugin this won't work for a html tag.
Link: http://www.vim.org/scripts/script.php?script_id=39
SnipMate
Snipmate is really one of my favourite, it's simple but yet so powerful, it's brilliant. You simply download the plugin, copy it to your .vim folder and you're ready to go, just that simple! No, extra additions to .vimrc, no long docs reading, just download, copy and start playing. When you've done that, open a C file and type as follows:
for and than hit the tab key, voilĂ ! A complete for loop snippet is placed and we're not finished yet! You can replace the default words by hitting tab again, change the word in the selected field, than hit tab again, edit the selected field until you're done. This is fantastic!
Link to download snipmate plugin: https://github.com/garbas/vim-snipmate
Surround
This plugin is a must-have when writing html code, it surrounds a selected block of text/code with div tags (in case you're writing html code) or brackets (in case you're writing C code). It's also very easy to use.
For example, type a simple text and than go back to normal mode and type: yss"
When using visual selection (v or V) than use capital S for surrounding, i.e. veS' is the same as ysw'.
The first one is in visual mode and the second one is in normal mode.
The key combinations can be seen as follows:
ysmn surround word m with tag n
csmn change surround with tag m to tag n
dsn delete surround with tag m
For more detailed information go to link: https://github.com/tpope/vim-surround
NERDTree
This plugin works as a file browser, you open it, search the file you want to edit and than hit enter. The file will be displayed and you can edit all along. So, you don't have to get out of Vim to look for files to edit, you simply stay in Vim, open NERDTree and look for the files. But I tell you this, a real good experienced Vim user doesn't even need NERDTree to do such a magic.
I've seen die-hard Vim users loading a whole bunch of files at once and switch buffers as they drink coffee and do crazy stuff! Vim is so powerful, you can't call it just an editor, it's actually an editor that you have a conversation with. When you say, 'dd', it responds with "listen baby, if you want I can also delete multiple lines", than you move 4 lines down and Vim says "baby, just say 4j and I do what you want, but anyway sweety, you want me to delete this line too? Just press . and I'll do it for you". This is Vim and I'm also a beginner, Vim is just incredibly good, you just need to be patient with her and once you have control over her, you can let her do whatever you want ;-)
By adding this line .vimrc, you can toggle NERDTree on or off by pressing on the F4 key:
:noremap :NERDTreeToggle
Link: https://github.com/scrooloose/nerdtree
TagList
TagList plugin is a sourcecode browser, it shows you a list of classes, defines, global variables, functions, methods etc. for the currently displayed file. This a very useful feature, this way you have an overview of all the functions and you can navigate to a function or a method easily. If you want to jump to a function and edit right away, you simply move to the function name in the taglist and hit enter. If you just want to view the function without jumping to it, you simply press 'p' key (which means preview), this way you stay in the taglist window.
It supports most programming languages, so it's a simple and useful plugin without putting a bunch of tweaks in the vimrc file.
I added these lines in the .vimrc script, to enable and disable TagList with F5 key:
:noremap :TlistToggle
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
The last line tells TagList to show on the right side of the window.
Clang_complete
This plugin is important when developing C/C++ code. Clang_complete together with clang and libclang-dev is the best replacement for omnicppcompletion, the old completion plugin which doesn't work well. omnicppcompletion uses ctags to perform completion of code, but it's not perfect like Visual Studio's intellisense or Eclipse. But clang is here to save us, developed by llvm (www.llvm.org). First you need to install clang, I did the following:
#sudo apt-get install clang
#sudo apt-get install libclang-dev
Than download clang_complete from:
https://github.com/Rip-Rip/clang_complete
Copy the files to your .vim folder and you're done. That's actuall all I did.
I tested with BOOST C++ and it works without tweaking or adding creepy scripts, it just works! See below for an example:
First, here's a list of Vim plugins below I use it for writing code:
- Surround
- Matchit
- Snipmate
- Clang_complete
- NERDTree
- TagList
Matchit
This is a nice plugin too, especially when dealing with html files. It searches for matching tags using the existing % key. In a C file, if you're on one of the brackets like {}, [], () and so on, you'll see those brackets highlighting in pairs. When hitting % the cursor jumps to the matching pair, but normally this won't work for a html tag. Without this plugin this won't work for a html tag.
![]() |
| Put cursor on head tag and enable V mode |
![]() |
| Hit % to jump to the matching tag |
Link: http://www.vim.org/scripts/script.php?script_id=39
SnipMate
Snipmate is really one of my favourite, it's simple but yet so powerful, it's brilliant. You simply download the plugin, copy it to your .vim folder and you're ready to go, just that simple! No, extra additions to .vimrc, no long docs reading, just download, copy and start playing. When you've done that, open a C file and type as follows:
for and than hit the tab key, voilĂ ! A complete for loop snippet is placed and we're not finished yet! You can replace the default words by hitting tab again, change the word in the selected field, than hit tab again, edit the selected field until you're done. This is fantastic!
![]() |
| Typing 'for' before hitting TAB |
![]() |
| Result after hitting TAB |
Link to download snipmate plugin: https://github.com/garbas/vim-snipmate
Surround
This plugin is a must-have when writing html code, it surrounds a selected block of text/code with div tags (in case you're writing html code) or brackets (in case you're writing C code). It's also very easy to use.
For example, type a simple text and than go back to normal mode and type: yss"
![]() | |
| Before typing yss" in normal mode |
![]() | |
| After typing yss" in normal mode |
![]() |
| After typing yss} in normal mode |
![]() |
| After typing cs}) in normal mode |
The first one is in visual mode and the second one is in normal mode.
The key combinations can be seen as follows:
ysmn surround word m with tag n
csmn change surround with tag m to tag n
dsn delete surround with tag m
For more detailed information go to link: https://github.com/tpope/vim-surround
NERDTree
This plugin works as a file browser, you open it, search the file you want to edit and than hit enter. The file will be displayed and you can edit all along. So, you don't have to get out of Vim to look for files to edit, you simply stay in Vim, open NERDTree and look for the files. But I tell you this, a real good experienced Vim user doesn't even need NERDTree to do such a magic.
I've seen die-hard Vim users loading a whole bunch of files at once and switch buffers as they drink coffee and do crazy stuff! Vim is so powerful, you can't call it just an editor, it's actually an editor that you have a conversation with. When you say, 'dd', it responds with "listen baby, if you want I can also delete multiple lines", than you move 4 lines down and Vim says "baby, just say 4j and I do what you want, but anyway sweety, you want me to delete this line too? Just press . and I'll do it for you". This is Vim and I'm also a beginner, Vim is just incredibly good, you just need to be patient with her and once you have control over her, you can let her do whatever you want ;-)
By adding this line .vimrc, you can toggle NERDTree on or off by pressing on the F4 key:
:noremap
![]() |
| Example of NERDTree |
Link: https://github.com/scrooloose/nerdtree
TagList
TagList plugin is a sourcecode browser, it shows you a list of classes, defines, global variables, functions, methods etc. for the currently displayed file. This a very useful feature, this way you have an overview of all the functions and you can navigate to a function or a method easily. If you want to jump to a function and edit right away, you simply move to the function name in the taglist and hit enter. If you just want to view the function without jumping to it, you simply press 'p' key (which means preview), this way you stay in the taglist window.
It supports most programming languages, so it's a simple and useful plugin without putting a bunch of tweaks in the vimrc file.
I added these lines in the .vimrc script, to enable and disable TagList with F5 key:
:noremap
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
The last line tells TagList to show on the right side of the window.
![]() |
| TagList example on the right side |
Clang_complete
This plugin is important when developing C/C++ code. Clang_complete together with clang and libclang-dev is the best replacement for omnicppcompletion, the old completion plugin which doesn't work well. omnicppcompletion uses ctags to perform completion of code, but it's not perfect like Visual Studio's intellisense or Eclipse. But clang is here to save us, developed by llvm (www.llvm.org). First you need to install clang, I did the following:
#sudo apt-get install clang
#sudo apt-get install libclang-dev
Than download clang_complete from:
https://github.com/Rip-Rip/clang_complete
Copy the files to your .vim folder and you're done. That's actuall all I did.
I tested with BOOST C++ and it works without tweaking or adding creepy scripts, it just works! See below for an example:
Thursday, October 6, 2011
Vim - Omnicompletion - ctags!
How to get intellisense like (omnicompletion) feature on Vim for standard C libraries!
If you're a Vim user and also code C sources in Vim, than you quickly find yourself switching between terminals or browser to look for the type of arguments you need for standard C functions like strcmp() or strtok() or whatever. Well, not anymore! I found a lot of examples how to use omnicompletion whitin Vim, but almost all of them assume you need omnicompletion for C++, php, python or whatever. I googled my ass off to find an example of omnicompletion for standard C libraries.
First, what I did that didn't work. I looked at the folders where those standard c include files reside and found those in /usr/include/ directory. So I was analyzing the files for C only headers like stdio.h and such. I was also looking for Linux's systemcall files, like sockets etc...
Since version Vim 7.0 Vim supports omnicompletion and to work with that feature Vim needs a ctagged file (a file that is generated by ctags). This tag files contains a list of functionnames, prototypes, defines and other C related stuff, formatted in an indexed way so it can be looked up in a fast and efficient way (I guess, I'd rather use an SQLite engine as a solution). So to put it all together, we need to make a tag file using ctags. On Linux distros like Ubuntu, it's installed by default, but if you don't have one, google for exuberant ctags, download it and install it.
To generate a ctags file, you need to do the following:
# ctags /usr/include/*.h
This will generate a ctags file named tags.
If you open a c file and try out omnicompletion on the word "prin" by hitting Ctrl+x Ctrl+o, Vim won't give you a printf in the list of possible functions. Well, that sucks :(
One of the solutions is figure out how ctags work! Well, I'm sorry, I just don't have the time to do that just because I happen to want omnicompletion for standard C libraries in Vim.
The other solution is this: Be sure you downloaded and installed apt-file. If you never used apt-file than run "apt-file update". After that do the following:
Create file "libcs-ignore" with the following content:
__attribute__
__attribute_deprecated__
__attribute_format_arg__
__attribute_format_strfmon__
__attribute_malloc__
__attribute_noinline__
__attribute_pure__
__attribute_used__
__attribute_warn_unused_result__
__wur
__THROW
__nonnull+
If you're a Vim user and also code C sources in Vim, than you quickly find yourself switching between terminals or browser to look for the type of arguments you need for standard C functions like strcmp() or strtok() or whatever. Well, not anymore! I found a lot of examples how to use omnicompletion whitin Vim, but almost all of them assume you need omnicompletion for C++, php, python or whatever. I googled my ass off to find an example of omnicompletion for standard C libraries.
First, what I did that didn't work. I looked at the folders where those standard c include files reside and found those in /usr/include/ directory. So I was analyzing the files for C only headers like stdio.h and such. I was also looking for Linux's systemcall files, like sockets etc...
Since version Vim 7.0 Vim supports omnicompletion and to work with that feature Vim needs a ctagged file (a file that is generated by ctags). This tag files contains a list of functionnames, prototypes, defines and other C related stuff, formatted in an indexed way so it can be looked up in a fast and efficient way (I guess, I'd rather use an SQLite engine as a solution). So to put it all together, we need to make a tag file using ctags. On Linux distros like Ubuntu, it's installed by default, but if you don't have one, google for exuberant ctags, download it and install it.
To generate a ctags file, you need to do the following:
# ctags /usr/include/*.h
This will generate a ctags file named tags.
If you open a c file and try out omnicompletion on the word "prin" by hitting Ctrl+x Ctrl+o
One of the solutions is figure out how ctags work! Well, I'm sorry, I just don't have the time to do that just because I happen to want omnicompletion for standard C libraries in Vim.
The other solution is this: Be sure you downloaded and installed apt-file. If you never used apt-file than run "apt-file update". After that do the following:
Create file "libcs-ignore" with the following content:
__attribute__
__attribute_deprecated__
__attribute_format_arg__
__attribute_format_strfmon__
__attribute_malloc__
__attribute_noinline__
__attribute_pure__
__attribute_used__
__attribute_warn_unused_result__
__wur
__THROW
__nonnull+
# apt-file list libc6-dev | grep -o '/usr/include/.*\.h'> ~/.vim/tags/libc6-filelist
Finally do this:
# ctags --sort=foldcase --c++-kinds=+p --fields=+iaS --extra=+q -I./libc6-ignore -f ~/.vim/tags/libc6 -L ~/.vim/tags/libc6-filelist
And voila! You're done, you're finished! Try it out, but don't forget to set the tags in Vim:
# :set tags+=path/to/tag
Somehow, when ctags process the files, it gets fooled by those attributes listed in libc6-ignore file. -I option tells ctags to ignore thow words, so functionname will be parsed with the right functionname instead of __attribute__ etc...
.
I found this solution from the following link:
http://stackoverflow.com/questions/1632633/ctags-does-not-parse-stdio-h-properly
Subscribe to:
Posts (Atom)
-
To use Vim as an editor for C/C++ (and other programming languages as well), we need to extend Vim with some handy plugins. Vim is a power...
-
I wanted my RaspberryPi to autoconnect through wifi on a spcified Access Point (AP). To do this, first I copied /etc/wpa_supplicant/wpa_su...
-
How to get intellisense like (omnicompletion) feature on Vim for standard C libraries! If you're a Vim user and also code C sources i...











