Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Thursday, October 14, 2010

vim: how to record and replay macros

To record a macro:
  1. Start recording: press ‘q’
  2. Choose a macro register: press ‘a’ to select ‘a’ as a location to save the macro to. You will see “recording” at the bottom left of the vim window.
  3. Perform editing actions: for example, suppose you want to delete any line containing the string, “Stage:” You can do this by pressing:
    Esc
    /Stage:
    dd
    

  4. Stop recording: press ‘q’
To replay a macro:
  1. Choose a macro register: In our case, we want the macro we just saved to register ‘a’.
  2. Repeat the saved macro: by pressing “@[register_name]” which in our case is:
    @a

  3. Multiple-repeat: press “[count]@[register_name]”, for example:
    8@a

Thursday, April 29, 2010

vim: my ~/.vimrc file

set number
set incsearch
set shiftwidth=4
set tabstop=4
set autoindent
set smartindent
set paste
set expandtab
set showcmd
 
" set search highlighting on
set hls
 
set scrolloff=2
 
" Quit without fuss on :Q
:command -nargs=0 Quit :qa!

" Write without fuss on :W
:command -nargs=0 Write :w 
 
 
" fix the vim+backspace problem in cygwin - might NOT be needed on native linux shells!
set backspace=indent,eol,start
 
" set syntax highlighting on (for all possible file types)
syntax on
 
" always show current cursor position (row, column) at bottom right
set ruler
 
" choose colors that look good on a dark background, if possible
" set background=dark

" set more suitable colors for the line numbers
highlight LineNr gui=NONE guifg=black    guibg=grey 
highlight LineNr cterm=NONE ctermfg=darkgrey  ctermbg=grey


" This highlights the background in a subtle red for text that goes over the 80 column limit
" http://stackoverflow.com/questions/235439/vim-80-column-layout-concerns
" press F3 to toggle 80 column overlength highlighting
let ColHL='off'
highlight OverLength ctermbg=darkred ctermfg=white guibg=#592929
" match OverLength /\%81v.\+/

function! Toggle80ColumnHighlight()
    if g:ColHL == 'on'
        match OverLength //
        let g:ColHL='off'
    elseif g:ColHL == 'off'
        match OverLength /\%81v.\+/
        let g:ColHL='on'
    endif
endfunction

nnoremap  :call Toggle80ColumnHighlight()

" mark text after column 80 ( >= vim7.3 )
" set colorcolumn=80


" function to show color scheme in use
" source: http://stackoverflow.com/questions/2419624/how-to-tell-which-colorscheme-a-vim-session-currently-uses
function! ShowColorSchemeName()
    try
        echo g:colors_name
    catch /^Vim:E121/
        echo "default
    endtry
endfunction

" set a better search highlight colors
" http://stackoverflow.com/questions/7103173/vim-how-to-change-the-highlight-color-for-search-hits-and-quickfix-selection
highlight Search cterm=NONE ctermfg=white ctermbg=darkblue
highlight Search gui=NONE   guifg=white  guibg=darkblue
" set better incremental search highlight colors
highlight IncSearch cterm=NONE ctermfg=darkgreen ctermbg=grey
highlight IncSearch gui=NONE   guifg=darkgreen  guibg=grey

Monday, March 8, 2010

vim: mapping the F2 key to a sequence of commands

Macros are a great way to automate tasks in vim. You can record a sequence of keystrokes, assign them to a register, and then playback the macro saved in that register any number of times.

However, sometimes you come across a macro that is so useful that you use it frequently. Perhaps you use it across several vim sessions. You wish you could create a keyboard shortcut for this macro, and then save it in your ~/.vimrc file for anytime usage.

This is how you can do that, using vim’s map command:
map <F2> 0i\<Esc>A\n\<Esc>j

This mapping will bind the F2 key to perform this sequence of operations:

number operation vim command
1 go to the hard BOL (beginning of line) 0
2 insert the '\' character i\
3 go back to command mode <Esc>
4 append \n\ to the end of the line A\n\
5 go back to command mode <Esc>
6 go down one line (so that you can just press F2 again on the next line, to continue) j

Friday, November 27, 2009

vim: create a custom ex command to quit on ":Q"

I put this in my ~/.vimrc so that it is available in every vim session:
:command -nargs=0 Quit :qa!

Finally! I wanted this little vim tweak since quite a while. I don't want the nagging "E492: Not an editor command: Q" error message every time I type ":Q" instead of ":q".

This command simply tells vim to do a ":qa!" when the user enters ":Quit" or an abbreviation of ":Quit" such as ":Q".

Monday, August 24, 2009

vim: commenting-out lines of code esaily

For short ranges of basic prefixing comments (# or //), I've become fond of vim's visual block mode and block insert. To do this:
  1. start visual block mode (with Ctrl-V)
  2. adjust the selection with movement keys as needed. (marks can be used here)
  3. once the selections are made, start block insert with 'I' (or append with 'A')
    NOTE: don't use the usual lowercase 'i' or 'a'
  4. type the comment text (e.g. "#")
  5. when you hit escape, comments will be added to all selected lines

source