• GPG Usage

    To encrypt and decrypt files in Linux there is a utility called gpg (Gnu Privacy Guard). This is a short GPG tutorial.

    Quick usage example

    gpg -c foo.txt
    

    It will prompt you for the passphrase and a confirmation. Now you will have the encrypted foo.txt.gpg file. To decrypt a file:

    gpg -d foo.txt.gpg
    

    This will forward the output to the console. You can output it into a file:

    gpg -d foo.txt.gpg > foo.txt

    GPG keyring

    This is all secure, but not quite enough if you are paranoid. Keys are what makes gpg great. Let’s generate a private key:

    gpg --gen-key
    

    And create an ASCII version of a public key:

    gpg --armor --export "John Doe" --output johndoe.txt
    

    Public key johndoe.txt can be freely distributed. Now you can encrypt files for yourself only:

    gpg -e -r "John Doe" foo.txt
    

    Now if you decrypt a file it will require the passphrase you specified while generating a key. To encrypt a file for someone else you should have this person’s public key.

    Let’s assume Stan Smith sent you a key, stansmith.txt. You import it using:

    gpg --import stansmith.txt
    

    And encrypt the file:

    gpg -e -r "Stan Smith" foo.txt
    
  • C strtok usage example

    I had some issues with the strtok function. Here’s a detailed explanation and usage for it.

    The strtok function is used to tokenize a string and thus separates it into multiple strings divided by a delimiter.

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        int j, i = 0;
        char delim[4];
        char user_input[81], *token[80];
        user_input[0] = 0;  // set first byte to 0 to detect empty string
    
        // validate the string length
        while (strlen(user_input) <= 1 || strlen(user_input) > 82) {
            printf("Feed me a string to tokenize: ");
            fgets(user_input, sizeof(user_input), stdin);
        }
    
        printf("And a delimiter (up to 4 chars): ");
        fgets(delim, sizeof(delim), stdin);
    
        token[0] = strtok(user_input, delim);  // first call returns pointer
                                               // to first part of user_input
                                               // separated by delim
        while (token[i] != NULL) {
            i++;
            token[i] = strtok(NULL, delim);  // every call with NULL uses
                                             // saved user_input value and
                                             // returns next substring
        }
    
        for (j=0; j<=i-1; j++) {
            printf("%sn", token[j]);
        }
    
        return 0;
    }
    

    Let’s compile and execute it:

    Feed me a string to tokenize: foo/bar/baz
    And a delimiter: /
    foo
    bar
    baz
    

    The first call to strtok returns the pointer to the first substring. All the next calls with the first argument being NULL use the string passed at the first call and return the next substring. The function returns NULL if no more substrings are available.

  • Create gitolite repository

    A reminder on how to initialize a fresh gitolite repository, assuming that gitolite has already been set up.

    All actions are performed on a local machine. In this case: ~/gitolite-admin is admin repository, ~/foo is desired repository, rosipov.com is gitolite hostname. Command vi stands for the text editor, but you may use whichever editor you prefer.

    cd ~/gitolite-admin
    vi conf/gitolite.conf
    

    Add lines (obviously you may want to use individual users instead of @all):

    repo foo
        RW+ = @all
    

    Save it. Next:

    git add conf/gitolite.conf
    git commit -m "Add foo repo for @all"
    git pull --rebase &amp;&amp; git push
    mkdir ~/foo
    cd ~/foo
    git init
    git remote add origin git@rosipov.com:foo.git
    

    Add some files at this point. In this example, only .gitkeep is added.

    git add .gitkeep
    git commit -m "Initialize repo"
    git push origin master
    

    The new repository is all set up now.

  • GUI git difftool for Windows

    A quick note on how to set up GUI difftool to use with git on Windows (Git Bash, Cygwin, etc…).

    Download and install GUI diff tool of your choice, get the path to executable.

    Create difftool.sh in directory included in your path (for example C:\Users\{username}\bin in Git Bash). Let’s take SourceGear’s DiffMerge as an example.

    #!/bin/sh
    "C:/Program Files/SourceGear/Common/DiffMerge/sgdm.exe" "$1" "$2" | cat
    

    And in your ~/.gitconfig:

    [diff]
        tool = diffmerge
    [difftool "diffmerge"]
        difftool.sh "$LOCAL" "$REMOTE"
    

    And difftool is available via git difftool command now.

  • Extendedly basic vim setup

    Ahoy Internet, here’s a neat and fairly simple vim configuration for programming. Feel free to copy over settings/plugins you find useful, or just download a repository from GitHub with settings and plugins.

    The .vimrc file

    """""""""""""""""""""""""""""""""""""""
    " => Editing
    """""""""""""""""""""""""""""""""""""""
    
    syntax on
    
    " Indentation settings
    set tabstop=4
    set shiftwidth=4
    set smartindent
    set autoindent
    set expandtab
    
    " Disable backups and .swp files
    set nobackup
    set nowritebackup
    set noswapfile
    
    " Ignore case when searching
    set ignorecase
    set smartcase
    
    """""""""""""""""""""""""""""""""""""""
    " => Looks
    """""""""""""""""""""""""""""""""""""""
    
    colorscheme darkburn
    set background=dark
    
    " Set terminal window title
    set title
    
    " Shorten press ENTER to continue messages
    set shortmess=atI
    
    " Show last command
    set showcmd
    
    " Highlight cursor line
    set cursorline
    
    " Ruler (line, column and % at the right bottom)
    set ruler
    
    " Enable wild menu (tab command autocompletion)
    set wildmenu
    set wildmode=list:longest,full
    
    " Warn if exceed 80 columns limit
    if (&ft == 'python')
        highlight OverLength ctermbg=red ctermfg=white guibg=#592929
        match OverLength /%81v.+/
    endif
    
    """""""""""""""""""""""""""""""""""""""
    " => Misc
    """""""""""""""""""""""""""""""""""""""
    
    " Use Unix as the standart file type
    set ffs=unix,dos,mac
    
    " Enable filetype plugins
    filetype plugin on
    filetype indent on
    
    " Ignore compiled files
    set wildignore=*.o,*~,*.pyc,*.pyo
    
    """""""""""""""""""""""""""""""""""""""
    " => Plugins
    """""""""""""""""""""""""""""""""""""""
    
    " EasyMotion: one leader key instead of two
    let g:EasyMotion_leader_key = '<Leader>'
    
    " NERDTree: auto open and close
    autocmd VimEnter * NERDTree
    autocmd WinEnter * call s:CloseIfOnlyNerdTreeLeft()
    
    " NERDTree: focus on text window (left)
    autocmd VimEnter * wincmd l
    autocmd BufNew * wincmd l
    
    " NERDTree: auto close if last window
    function! s:CloseIfOnlyNerdTreeLeft()
        if exists("t:NERDTreeBufName")
            if bufwinnr(t:NERDTreeBufName) != -1
                if winnr("$") == 1
                    q
                endif
            endif
        endif
    endfunction
    
    " Exuberant Ctags: autogenerate on py file write
    au BufWritePost *.py silent! !ctags -R &
    
    " Pydoc: open in new tab instead of split
    let g:pydoc_open_cmd = 'tabnew'
    
    " Pydoc: disable search term highlight
    let g:pydoc_highlight=0
    

    Plugins

    If you are not planning to use some of the plugins, make sure to remove plugin-specific rules from .vimrc => Plugins.

    • Exuberant Ctags: allows you to generate index of variables, functions and classes to freely move between them. A bit more info on how to use these in article Using vim for writing code.
    • Colo(u)r Sampler Pack is not really a plugin, but a collection of color shemes. 100 of them, to be precise. Feel free to choose whatever you like.
    • ScrollColors allows you to try out every theme in visual mode. Just type in :SCROLL.
    • EasyMotion allows you to move between words, lines and sentences with a lightning speed. Just hit <Leader> + w to jump to one of the words forward or <Leader> + b backward (leader key is a backslash by default). It can do more neat things, documentation is pretty self-explanatory.
    • NERDTree is a directory tree, like in all modern IDEs (but of course better).
    • tComment allows you to easily comment out blocks of code. Hit gcc to comment a line or gc while selecting a block in Visual mode.

    Source

    You may want to head over to GitHub and grab a version from here: https://github.com/ruslanosipov/dotfiles.