_       _       
  (_) ___ | |_ ____
  | |/ _ \| __|_  /
  | | (_) | |_ / / 
 _/ |\___/ \__/___|
|__/               

Remapping Vim keys

This is my ~/.vimrc file

" Disable compatibility with vi
set nocompatible

" Filetype detection and configuration
filetype on
filetype plugin on
filetype indent on

" Syntax highlighting
syntax on

" Remap up and down arrow keys to move cursor
" one screen line in normal and visual modes
:noremap <Up> gk
:noremap <Down> gj

" Remap up and down arrow keys to move cursor
" one screen line in insert mode
:inoremap <Up> <C-o>gk
:inoremap <Down> <C-o>gj

" Enable line numbers
set number
"set nonumber

" Set tab width
set shiftwidth=4
set tabstop=4
set expandtab

" Set colour scheme
colorscheme ron 

" Disable Vim mouse interaction, which re-enables
" regular terminal highlighting with mouse
set mouse=

" Set up Ctrl-C as a visual mode shortcut to copy selected
" text to the system clipboard
:vnoremap <C-C> y:call system('xclip -selection clipboard', @0)<CR>

If "vim-gtk" is installed (even if vim is running in the terminal), then the "+clipboard" option is likely enabled. To verify this, run "vim --version". Assuming "+clipboard" is enabled, then the following command can be used to copy the entire contents of the current buffer to the system clipboard (equivalent to Ctrl-A followed by Ctrl-C in most apps):

:%y+

A similar command can be used to copy an arbitrary visual selection to the system clipboard. Press "v" to enter visual mode, move the cursor to select the required text, then type:

:'<,'>y+

Alternatively, in visual mode, highlight the required text and then type the following, which yanks text into the "+" register (the system clipboard).

"+y

The "'<,'>" part of the above command should appear automatically once you type ":".

To swap the caps lock and escape keys in X windows:

setxkbmap -option "caps:swapescape"

Alternatively, to just reassign the caps lock key as a second Esc key:

setxkbmap -option "caps:escape"

Another way to swap Esc and Caps Lock keys, which works in terminal as well as in X windows is to edit "/etc/default/keyboard" (as root) to add "caps:swapescape" to XKBOPTIONS:

# KEYBOARD CONFIGURATION FILE

# Consult the keyboard(5) manual page.

XKBMODEL="pc105"
XKBLAYOUT="gb"
XKBVARIANT=""
XKBOPTIONS="caps:swapescape"

BACKSPACE="guess"

To apply the above changes, run:

sudo setupcon -k -f

Side note: When vim is running in tmux, there is a delay of approximately 1 second when you type "Esc" to exit from insert mode. This seems to be due to tmux. To get rid of this delay, I ran the following command in tmux (Type "Ctrl+B :" first):

set-option -g escape-time 10

That could be added to "~/.tmux.conf".

To add a live word count to the Vim status bar:

set laststatus=2
set statusline+=%{wordcount().words}\ words

To insert some Lorem Ipsum text into a Vim buffer:

:% ! curl -s https://loremipsum.de/downloads/version5.txt

To split the first sentence in a line into a separate line (with no trailing space). This leaves the cursor on the remaining part of the line.

:norm 0)i^M^[kg_a ^[Dj0

To repeat the above command to separate multiple sentences on individual lines, use

@:

To join lines in a paragraph into one continuous line, use the following. In normal more, with the cursor positioned anywhere in the paragraph:

vipJ

By default, the join operation (J) places two spaces after a full stop at the end of a line. To use a single space only (my own personal preference), do this:

:set nojoinspaces

A single space will still be inserted between lines, even if none was present at the end of each line. To join lines without any additional spaces at all,

gJ

To generate a list of random 8-digit integers between 20000000 and 25000000:

:.!shuf -i 20000000-25000000 -n14

To perform multiple string substitutions at once, a command like the following can be used:

:%s/21396189/22999421/e | %s/246525/535976/e | %s/224965/873980/e | %s/20715215/24697130/e | %s/20958005/21880293/e | %s/207078/126027/e | %s/20702297/23514775/e | %s/23038804/21362782/e | %s/24285876/23395658/e | %s/23145555/20225147/e | %s/24637244/22602456/e | %s/21334480/23195125/e | %s/23714782/20987812/e | %s/21897067/22409780/e 

Clearly, this command is too long to type directly in command mode, so to paste it into the command buffer from the unnamed register (where yanked text is stored by default), do the following:

:CTRL-R"

Function to split paragraph into sentences for "MEMRISTORS" sentence-wise editing

Add this to .vimrc to create a function to split a paragraph into sentences and then bind that function to <leader>ss, which by default will be "\ss".

" MEMRISTORS helper function
:set nojoinspaces " Join lines with a single space, rather than two

func! SplitParagraphIntoSentences()
    exe "normal! vapJA\r\ek"
    s/\([.!?]\+['"”]*\)\s*\([^$]\)/\1\r\2/g
endfunc

nnoremap <leader>ss :call SplitParagraphIntoSentences()<CR>

To reload the modified .vimrc file:

:so $MYVIMRC

Alternative version to space out sentence lines

This version:

" MEMRISTORS helper function
:set nojoinspaces " Join lines with a single space, rather than two

func! SplitParagraphIntoSentences()
    exe "normal! vapJA\r\ek"
    s/\([.!?]\+['"”]*\)\s*\([^$]\)/\1\r|\r|\r\2/g
endfunc

func! JoinSentencesIntoParagraph()
    exe "normal! vipduvip"
    exe "'<,'>g/^|$/d"
    exe "normal! vipJ"
endfunc

nnoremap <leader>ss :call SplitParagraphIntoSentences()<CR>
nnoremap <leader>jj :call JoinSentencesIntoParagraph()<CR>

Note: The line with "vipduvip" in the JoinSentencesIntoParagraph function is kind of a bug fix. For some reason, the command on the next line which removes the separator lines wasn't matching (or just not deleting?) some of the separator lines. However, when the entire paragraph is deleted and then undeleted beforehand, the removal of the separator lines works perfectly. I have no idea why this didn't work without the delete-undelete step (i.e. just doing "vip" rather than "vipduvip"), but it seems to be working with the fix in place, so I've left it as is for now.

A slighty modified version that removed separator lines with any single character, plus optional whitespace before and after the single character:

" MEMRISTORS helper function
:set nojoinspaces " Join lines with a single space, rather than two

func! SplitParagraphIntoSentences()
    exe "normal! vapJA\r\ek"
    s/\([.!?]\+['"”]*\)\s*\([^$]\)/\1\r|\r|\r\2/g
endfunc

func! JoinSentencesIntoParagraph()
    exe "normal! vipduvip"
    exe "'<,'>g/^[[:blank:]]*[^[:blank:]][[:blank:]]*$/d"
    exe "normal! vipJ"
endfunc

nnoremap <leader>ss :call SplitParagraphIntoSentences()<CR>
nnoremap <leader>jj :call JoinSentencesIntoParagraph()<CR>

Clearing the Vim registers

" Function to clear registers
func! ClearRegisters()
    let @0 = ''
    let @1 = ''
    let @2 = ''
    let @3 = ''
    let @4 = ''
    let @5 = ''
    let @6 = ''
    let @7 = ''
    let @8 = ''
    let @9 = ''
    let @q = ''
    let @+ = ''
    let @* = ''
endfunc

To call the above function:

:call ClearRegisters()