VIM controlled capslock

One of the things I really appreciate about VIM is the modal style of text editing. It was the first text editor introduced to me that allowed you to edit in this style.

When VIM was originally coded it used a keyboard where the escape key was where the capslock is on modern machines, this made it highly accessible while typing. Hence the escape key is what is by default used to leve the insert mode and enter the normal mode.

The problem I found is that modern keyboards have the escape key quite far from having my hands on the home row and I have found it quite hard to hit it without having my hands getting lost.

`1One solution to this is to use the mapping of ctrl+[ to escape from insert mode into normal mode. I find that this too is a bit of a faff. I found a better solution that works for me.

jj

I have mapped a double tap of the J key to escape, this allows me to easily move between the modes without having to move my fingers from the home row.

The problem I often face is if I am writing some text with capslock on and then try to escape with my usual JJ. This doesn’t get mapped correctly because the command is case sensitive. I did think about adding JJ to the mapping but then in my head when in normal mode my mind presumes I am typing in lower case.

To solve this I found a really neat solution: map JJ to exit insert mode but turn the capslock off!

The two mappings become:

imap JJ <Esc>:!python ~/.vim/caps.py<CR><CR>

imap jj <Esc>

The content of the python file which I found from this ask Ubuntu thread is as follows:


A side note for some other recent additions to my vimrc file

nmap yy :let @+=@"<CR>

nmap y% :let @+=@%<CR>


I run my tests using a pytest command in the terminal and have this running in a separate tmux window to the one in which I’m editing. One command I often want to run is to take the file location of the current file and run pytest against it.

I have added some yank command mappings that allow me to put the current buffer file location or the yank buffers content onto my computers clipboard.

Now to run the tests I normally run:

jjy%`1<ctrl>+r<ctrl>+<shft>+v<enter>

Or:

jjy%`1pytest <ctrl>+<shft>+v<enter>

That might seem overly complicated to someone who is using an IDE but given how I have managed to load this stream into my muscle memory it takes less than half a second to run.

It also does more than just running the tests. Breaking the command up we have:

Escaping normal mode:

jj

Loading the current file location into the buffer, this has a high specificity, in that I am running only the single file I am working on:

y%

Change the window to the terminal pane window, which I always have named 1. I name the editing terminal 2 and the stream of consciousness file in 3.

`1

If I know I have tested this file before I can use my FZF command history search using:

<ctrl>+r

That saves me having to type “pytest” but I can otherwise type pytest.

I then put the file location from the clipboard and run the tests.

I like to run it this way because it allows me to have the test run in full screen as opposed to at the bottom of the screen. The output of the tests more often than not is quite long so it’s nice to dedicate the whole screen to it.

There are ways to be more specific still, running only a subset of the tests, other than adding pytest.mark.skip ofcourse. 



Comments

Popular posts from this blog

Exploring development timing through building a simple trading game

The twelve fold way

THREE.js: minecraft in a weekend