Posts

Showing posts with the label VIM Editor

Marking in VIM Editor

In this post, we will see how to mark a particular cursor position in VIM Editor. This is really helpful while navigating through a file. Set mark a at current cursor location ma m stands for marking and a represents register in which cursor location is stored. It can be any register. Jump to marked cursor location 'a :  jump to line of mark a (first non-blank character in line) ~a :  jump to position (line and column) of mark List all the current marks :marks 

Search and Substitute in VIM Editor

This post  will show yo u, how you can easily search and substitute in VIM Editor. Below are some mostly used operations: Search /{string} : search for string * : search for other instances of the word under your cursor n : go to the next instance when you’ve searched for a string N : go to the previous instance when you’ve searched for a string ; : go to the next instance when you’ve jumped to a character , : go to the previous instance when you’ve jumped to a character Substitute s : substitute from where you are to the next command (noun) S : substitute the entire current line :%s /foo/bar/g --- Change “foo” to “bar” on every line :%s /foo/bar/gc  --- Interactive change “foo” to “bar” on every line :s /foo/bar/g --- Change “foo” to “bar” on just the current line :s /foo/bar/gc  --- Interactive change “foo” to “bar” on just the current line

VIM Over VI

VI was written in 1976 by Bill joy and released in 1978 whereas VIM(VIMproved) was written by Bram Moolenaar and released in 1991. As name implies VIM is improved or extended editor of VI editor or we could probably consider VIM as a super set of VI.  Some Extended features in VIM Editor are : Vim has been ported to a much wider range of OS's than vi Vim includes support (syntax highlighting, code folding, etc) for several popular programming languages (C/C++, Python, Perl, shell, etc) Vim can be used to edit files using network protocols like SSH and HTTP Vim includes multilevel undo/redo Vim allows the screen to be split for editing multiple files Vim can edit files inside a compressed archive (gzip, zip, tar, etc) Vim includes a built in diff for comparing files (vimdiff) Vim includes support for plugins, and finer control over config and startup files Vim can be scripted with vimscript, or with an external scripting language (e.g. python, perl, shell) ...

VIM as a language

Have you ever thought, VIM can be interpreted as a language like any other language? YES, It can be. This post will explain, HOW? Like any other speaking language, VIM has its verbs, modifiers, nouns etc. Let's look at each one of these and then try to build  a sentence out of them. Verbs Verbs are the actions we take, and they can be performed on nouns. Here are some examples: d: delete c: change y: yank (copy) v: visually select Modifiers Modifiers are used before nouns to describe the way in which you’re going to do something. Some examples: i: inside a: around NUM: number (e.g.: 1, 2, 10 ) t: searches for something and stops before it f: searches for that thing and lands on it Nouns In English, nouns are objects you do something to. They are objects. With vim it’s the same. Here are some vim nouns: w: word s: sentence ): sentence (another way of doing it) p: paragraph }: paragraph (another way of doing it) t: tag (think HTML/XML) b:...

Copy, Cut and Paste in VIM Editor

Copying, Cutting and Pasting are top most three operations people use excessively. According to my knowledge VIM has made these operations extremely easy as compared to other editors. Here is how those are used: Copying text y: yank (copy) whatever’s selected yy: yank the current line Cutting text Cutting text is simple: it’s the same as deleting. So whatever syntax you’re using for that, you’re actually just pulling that deleted text into a buffer and preparing it to be pasted. dd: delete the current line x: exterminate (delete) the character under the cursor X: exterminate (delete) the character before the cursor Pasting text p: paste the copied (or deleted) text after the current cursor position P: paste the copied (or deleted) text before the current cursor position] You could use MODIFIERS & NOUNS  along with above operations which make your task so easy. I will give you a small example here, 1. How to delete a "communication" word...

Navigation in VIM Editor

Navigation in VIM is so easy and handy. Almost all the operation are used by only ONE keystroke, Don't you believe this? Go through this post... Basic motions j: move down one line k: move up one line h: move left one character l: move right one character Moving within the line 0: move to the beginning of the line $: move to the end of the line ^: move to the first non-blank character in the line t": jump to right before the next quotes f": jump and land on the next quotes Moving by word w: move forward one word b: move back one word e: move to the end of your word W: move forward one big word B: move back one big word Moving by sentence or paragraph ): move forward one sentence }: move forward one paragraph Moving within the screen H: move to the top of the screen M: move to the middle of the screen L: move to the bottom of the screen gg: go to the top of the file G: go to the bottom of the file ^U: move up half...

Insert a line with single keystroke in VIM Editor

If you are familiar with Vim Editor then you may know that inserting a blank line is quite different from other editors(e.g. gedit) and tedious too. So to make it more easy and handy I have mapped this operation with minus(-) and plus(+) keys trough  map command  of vim. nnoremap + m a o <esc> ` a nnoremap - m a O <esc> ` a nnoremap means normal non-recursive mapping. So whenever + or - key is pressed, vim does following: ( m ) marks current position and saves it in ( a ) register Insert a blank line below in case of plus sign( o ) and above in case of minus sign ( O )  Go back to Normal mode( <esc> ) because O or o takes you into insert mode Navigate to previously marked position which was in ( a ) register If you want to map a key for only one Vim session temporarily, then you don't need to save the map command in a file. When you quit that Vim instance, the temporary map definition will be ...

Ctrl+s to save changes in VIM Editor

In Vim editor if we want to save changes, typically we have to get into command mode by typing ":" and type "w" to save changes to file from current buffer but  I always wanted to save my changes with  ctrl+s . So, I have mapped writing operation of "w" with ctrl+s with following commands,   nnoremap <C-s> <Esc>:w<CR>   vnoremap <C-s> <Esc>:w<CR>   inoremap <C-s> <Esc>:w<CR> If you want to map a key for only one Vim session temporarily, then you don't need to save the map command in a file. When you quit that Vim instance, the temporary map definition will be lost. If you want to restore the key maps across Vim instances, you need to save the map definition command in a . vimrc file normally present in  $HOME. That's it !!! Let me give you brief what these mean, The general syntax of map command is (from vim.fandom.com) , {cmd} {attr} {lhs} {rhs} where {cmd} ...

vimrc customization

Here is the copy of my .vimrc file. Hope this would help you as well. " User - Raxesh Oriya " --------------------------- " Map ctrl+s to save changes   nnoremap <C-s> <Esc>:w<CR> vnoremap <C-s> <Esc>:w<CR> inoremap <C-s> <Esc>:w<CR> " Remove all trailing whitespace on F5 key press nnoremap <silent> <F5> :let _s=@/ <Bar> :%s/\s\+$//e <Bar> :let @/=_s <Bar> :nohl <Bar> :unlet _s <CR> " highlight when 80 characters crossed  highlight OverLength ctermbg=red ctermfg=white guibg=#592929 augroup vimrc_autocmds     autocmd!     autocmd BufEnter,WinEnter * call matchadd('OverLength', '\%>80v.\+', -1) augroup END " Map Ctrl+c to copy contain in clipboard register vmap <C-c> "+y " Map Ctrl+x to cut contain in clipboard register vmap <C-x> "+d " Set relativenumber so I don't need to count th...