Skip to content

Instantly share code, notes, and snippets.

View kshenoy's full-sized avatar

Kartik Shenoy kshenoy

  • AMD
  • Fort Collins, CO
View GitHub Profile
@kshenoy
kshenoy / typedef.c
Created July 12, 2012 01:34
Use of typedefs with struct and enum in C/C++
// 1a) Struct declaration and instantiation w/o typedef
struct veg_t{
int cost;
char c;
};
struct veg_t potato;
// 1b) or its equivalent with declaration and instantiation combined…
struct veg_t{
int cost;
@kshenoy
kshenoy / MapKey.vim
Last active October 7, 2015 16:37
Function to complement the maparg() function in Vim. Given the 'rhs' of a mapping, this function returns the 'lhs'
function! MapKey( rhs, mode )
" Description: Get LHS of a mapping. Inverse of maparg().
" Note that hasmapto() returns a binary result while MapKey() returns the value of the LHS.
" Pass in a key sequence and the first letter of a vim mode.
" Returns key mapping mapped to it in that mode, else '' if none.
" Eg:
" :nnoremap <Tab> :bn<CR>
" :call Mapkey(':bn<CR>', 'n')
" returns <Tab>
" TODO:
@kshenoy
kshenoy / ReTab.vim
Last active October 7, 2015 16:37
Vim function to perform easy retabs ( eg. change file with softtabstop=4 to softtabstop=2 )
function! ReTab( tabsize )
" Description: Change indentation when tab size is changed
" Primarily used to convert an indentation of eg. 4 to 2 or vice-versa
if &expandtab
let l:tabstop_old = &tabstop
let l:softtabstop_old = &softtabstop
let l:shiftwidth_old = &shiftwidth
let &tabstop = &softtabstop
set noexpandtab
" Motion for "next/last object".
" For example, 'din(' will go to the Next '()' pair and delete its contents.
" and 'dip(' will go to the Previous '()' pair and delete its contents.
onoremap an :<c-u>call <SID>NextTextObject('a', 'f')<cr>
xnoremap an :<c-u>call <SID>NextTextObject('a', 'f')<cr>
onoremap in :<c-u>call <SID>NextTextObject('i', 'f')<cr>
xnoremap in :<c-u>call <SID>NextTextObject('i', 'f')<cr>
onoremap ap :<c-u>call <SID>NextTextObject('a', 'F')<cr>
@kshenoy
kshenoy / FoldAllBut.vim
Last active December 15, 2015 18:09
Function to open folds that have less than the specified number of lines
function! FoldAllBut( foldminlines )
" Description: Function to open folds that have less than the specified number of lines
" We assume that the folds are initially closed
" If a fold exists and is closed and has lesser number of lines than specified, open it and all nested folds
" Note: This does not work on nested folds
folddoclosed
\ if (( foldclosed(".") >= 0 ) && ( foldclosedend(".") - foldclosed(".") + 1 < a:foldminlines ))
\ exe 'normal! zO'
\ endif
endfunction
@kshenoy
kshenoy / vim_re.txt
Created April 11, 2013 21:59
Vim Regular Expression demonstrating use of look-ahead zero-width assertion to replace all the commas on a line that aren't a part of a quoted string
This uses positive look-ahead to check if there's a string ahead to substitute commas present outside quoted strings.
s/\v,(([^"]*"[^"]*")*[^"]*$)@=/|/g
Explanation:
* "[^"]*"
Match a double-quoted string
" Statusline modifications, added Fugitive Status Line & Syntastic Error Message
let g:last_mode = ''
function! Mode()
let l:mode = mode()
if l:mode !=# g:last_mode
let g:last_mode = l:mode
hi User2 guifg=#005f00 guibg=#dfff00 gui=BOLD ctermfg=22 ctermbg=190 cterm=BOLD
hi User3 guifg=#FFFFFF guibg=#414243 ctermfg=255 ctermbg=241
@kshenoy
kshenoy / buffers_tabs.vim
Created May 16, 2013 19:40
Mapping to toggle between buffers and tabs
nnoremap <silent> <F8> :let notabs=!notabs<Bar>:if notabs<Bar>:tabo<Bar>:else<Bar>:tab ball<Bar>:tabn<Bar>:endif<CR>
@kshenoy
kshenoy / VaryTabs.vim
Last active December 17, 2015 19:39
Insert tabs at start of line and spaces elsewhere
function! VaryTabs()
" Description: Make <Tab> put tabs at start of line and spaces elsewhere
if &expandtab
return "\<Tab>"
else
let nonwhite = matchend(getline('.'),'\S')
if nonwhite < 0 || col('.') <= nonwhite
return "\<Tab>"
else
let pos = virtcol('.')
@kshenoy
kshenoy / FoldText.vim
Last active April 21, 2019 06:03
Function to set the foldtext
function! FoldText()
" Description: Folding configuration
let nucolwidth = &fdc + &number*&numberwidth
let winwidth = winwidth(0) - nucolwidth - 3
let foldlinecount = foldclosedend(v:foldstart) - foldclosed(v:foldstart) + 1
let dashtext = strpart(string(v:folddashes),1,len(string(v:folddashes))-2)
let foldinfo = "+" . dashtext . " Fold: " . string(v:foldlevel) . ", " . string(foldlinecount) . " lines "
let firstline = strpart(getline(v:foldstart), 0 , winwidth - len(foldinfo))
let fillcharcount = winwidth - len(firstline) - len(foldinfo)
return firstline . repeat(" ",fillcharcount) . foldinfo