Hacker News new | past | comments | ask | show | jobs | submit login
Handy Keymaps in Vim (aonemd.github.io)
59 points by axiomdata316 on May 27, 2019 | hide | past | favorite | 37 comments

Since I can't control the mouse so well anymore due to health reasons I grew to hate Kanban boards and Trello. Nowadays, the following goes into my init.vim along with vimwiki and it has made my work much more enjoyable :)


" Close board tab

map <leader>bc :tabclose<CR>

" Main board: soon, doing, today, done

map <leader>bb :tabnew /media/datanix/code/mdwiki/wiki/kanban/soon.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/doing.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/today.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/done.md<CR>

" Full board: triage, later, soon, doing, today, done, archive

map <leader>bf :tabnew /media/datanix/code/mdwiki/wiki/kanban/triage.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/later.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/soon.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/doing.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/today.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/done.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/archive.md<CR>

" Triage board: triage, later, soon, doing

map <leader>bt :tabnew /media/datanix/code/mdwiki/wiki/kanban/triage.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/later.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/soon.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/doing.md<CR>

" Archiving board: done, archive

map <leader>ba :tabnew /media/datanix/code/mdwiki/wiki/kanban/done.md<CR>:vnew /media/datanix/code/mdwiki/wiki/kanban/archive.md<CR>

I loved this but wanted to generalize it a bit to clean it up and make it portable across different computers.

By no means am I a vimscript expert so any improvements welcome. (I've defined g:settings.kanban_board_path above).

    function! g:Kanban_file(stage)
        return g:settings.kanban_board_path . a:stage

    let $KBsoon=g:Kanban_file('soon.md')
    let $KBdoing=g:Kanban_file('doing.md')
    let $KBtoday=g:Kanban_file('today.md')
    let $KBdone=g:Kanban_file('done.md')
    let $KBarchive=g:Kanban_file('archive.md')
    " Close board tab

    map <leader>bc :tabclose<CR>

    " Main board: soon, doing, today, done

    map <leader>bb :tabnew $KBsoon<CR>:vnew $KBdoing<CR>:vnew $KBtoday<CR>:vnew $KBdone<CR>


Lovely, it was a bit of a hack I came up with that I was trying out, but your cleaned up version makes it look wonderful so into my init.vim it goes! Thanks :)

I've found a system wide mapping of 'Caps Lock' to 'Esc'(and vice versa) a much better use of keyboard estate / ergonomics.

Although the mapping was prompted by Vim usage, it's surprisingly useful for general computing as well.

Me too. I put escape on my caps lock key on my Ergodox and it's awesome. I miss it whenever I use another keyboard. Escape gets a lot of use everywhere and is in kind of an awkward spot, while for me at least caps lock is literally never used and is in a very comfortable spot.

Me too. When I get a new system, the first thing I do is map CapsLock to Escape.

On Windows, I use the Uncap [ https://github.com/susam/uncap ] tool because it does not require install or restart. The tool has good documentation to map CapsLock to Escape for Linux and MacOS too.

On the flip side, I struggle to use Vim when I need to do so someone else's computer. :-D

IMO making it a control key is a better choice (I tried both at some point) because chording with awkward keys is worse than just pressing them, and the control keys are placed awkwardly on most keyboards. Since you can end insertion mode with ^[ you then have a pretty comfortable chord that doesn't stray far from the home row to replace the escape key.

I had a tool on a mac that would make caps lock equivalent to escape if it was pressed and released alone, and control if it was chorded. Sounded great on paper but I underestimated how often i press control before realizing I don't want to perform a chord.

Make it both! From http://www.economyofeffort.com/2014/08/11/beyond-ctrl-remap-...

  xcape -e 'Caps_Lock=Escape;Control_L=Escape;Control_R=Escape'

Karabiner Elements is great for doing this kind of remapping on macOS


The equivalent for Windows would probably be AutoHotkey


OS X can natively set Caps Lock (or any of the other modifiers) to CTRL,ALT,CMD,Esc,Caps. in the Keyboard Pref Pane at least as of 10.13.

Of course Karabiner can do quite a bit more than that. But if you can't or don't want to install more software it's a choice available to you.

That's an interesting idea as well. I've always found reaching for the 'Esc' key to be more awkward than the 'ctrl' key, but undoubtedly I use the 'ctrl' key more often than the 'esc' key outside of Vim, so perhaps the sheer volume of use would make it worth it.

My favourite mapping for 'Esc' usage is to map kj to Escape ... super handy

noremap! kj <esc>


I found jk to be the best. If I use it in normal mode it is a no op (cursor up and down). The english language (and most all programming languages) don’t have words with a jk sequence in them (unlike kj). It works universally across all keyboards and terminals. My fingers are already there (unlike esc which is just as bad as lifting my hands to use the mouse). Plus in the rare case I do need jk inputed (very very rare) first the default is a safe default (esc) and second the mental load to pause while typing (pressing j wait past the half second timeout then press k) is no different then the mental load to choose to accept the expansion of abbreviations or delay typing to get around the auto expansion triggers.

Jk for the win!!! (Also jj works well to).

I also set my mapleader to <Space> you would be surprised how useful that is. Not to mention , (comma) is a valid and useful vi movement key.

Heh, kj is a no-op too of course (down then up).

I have literally never found myself needing to pause to wait for a k and then a j. What are these words with "kj" in them that you're wary of? I'm genuinely curious :)

Blackjack? In english there are words that end in k while I don’t know of any that end with J.

However my vim set up maps jk, kj, jj, and kk to Esc to in reality it is safe to just mash those two keys and one of the maps will work.

What if you need to type something like blackjack?

The remapping would trigger <esc> when both j and k are held down at the same time, so typing "blackjack" could be an issue if it were typed quickly and/or chorded.

If you needed to type it, you'd have to pause after typing the k (about a second?) then type the j.

By far my favourite remap in (Neo)vim is to map : to ; and ; to ,

I don't know how many thousand key presses I've saved by not having to hit "Shift" for every command.

    " Use ; rather than : for commands
    nore ; :
    nore , ;

I just switch ':' and ';' along with '-' and '_' in insert mode, cut's out probably 90% of shift key presses. Swapping the numbers and their shift counterparts would probably save another 90%.

YMMV depending on what you are actually typing, though. I think I tend to spend significantly less time issuing ex commands than I spend actually typing things.

As far as Vim split navigation goes (and especially for those of us using Tmux), check out https://github.com/christoomey/vim-tmux-navigator.

It simply allows you to use Ctrl+{h,j,k,l} to navigate across Vim and Tmux splits as if they all were part of one cohesive IDE. Additionally, it spares you from having to maintain a configuration blob in your .vimrc thanks to being bundled as a plugin.

I don’t think it auto-creates splits, though.

I've noticed that there are not as many Vim/Emacs related posts on HN during the past two years than before. And the discussions and upvotes have been less enthusiastic.

If it's indeed the case (I might well be off the mark), I can think of a few possible reasons, Maybe because VS Code has won. It is much easier for beginners than Vim/Emacs (duh) but still powerful, so less questions and issues are raised. Its default setup is very good so customization is minimum, its plugins system are both vast and easy to use, etc.

Or, Vim itself has become easier to operate since version 8, tutorials are better than before, plugins are better,

Or on HN people these days have much more interests in talking about other topics. What do you think?

Maybe everything to be said about vim has already been said. You can look at those threads from 3 years ago and read about it if you want.

I really like how his website displays so well in mobile. Beautiful work

Is there any reason for not using Tmux instead of using panes in Vim?

Utilizing cross-window copy/paste, cross-window repeat, opening a new file (window) with a single command vs new tmux pane then initializing vim. Those are just a few of the many reasons. IMO, using a new tmux pane for each vim instance would be a HUGE anti-pattern. Most people with the tmux/vim setup will use a single tmux pane for vim, then N number of other tmux panes for other required needs (compiling, tests, etc...).

Do people usually have multiple vim splits open? At least I usually just have one terminal with new tmux splits and usually one vim with multiple buffers I switch between if needed.

However if I'm looking for something or doing something in another split and I need to view a file or edit a file I don't go back to my vim split and open it there I just open vim right there where I am.

I typically split if I have some interrelated code that I need to cross-reference, e.g. when refactoring. Especially if this code is in the same file, in which case windows into the same buffer have the added benefit of synchronized editing. Another example: when programming C, it is useful sometimes to have the header in one vim window and the definitions in the other. Or you're navigating via ctags, and it's convenient to view a tag in a new window rather than change the buffer for the current window.

If I'm just editing two completely unrelated files I don't see any advantages to this workflow over opening multiple instances. Maybe if you have plugins that are slow to start up?

In standard usage, I am using splits (horizontal). The only real time I am using buffers is when I open up multiple files from command line in some type of ‘vim ‘git grep -l “<search>” .’‘ scenario where I will do an action against all opened buffers in vim.

Regarding your second point, I behave similarly. If I am off in one of my non main editor tmux panes and I need to open a quick file, I will do it there instead of moving back to my main editor vim session. This is usually due to the file needing to be opened is outside the cwd of the main vim session.

First thank you folks for the suggestions. After using Vim panes for a day, I can say this:

Vim panes vs vim instances in tmux, who wins? It depends on the use case. In case you want to edit files that are completely independent of each other and you prefer not sharing registers between these files, then you are better of using tmux panes; however if the use case involves similar files and involves lot of copy pasting between the 2 buffers, then it is very much recommended that you use Vim panes as the sharing of registers proves its worth in this use case.

One of my favorite features of Vim is that it completely decouples tabs/windows from files/buffers. In mostly all IDEs I'm aware of, you open a file, and you get a tab to go along with it. In Vim, I have one instance (of Vim) running, maybe just one window if I feel like it, and potentially hundreds of files open, maybe some edited but not saved yet. And all I do it switch in and out which file/buffer I want to view in my one window. (more typically 2-4 panes/windows, and yay BufExplorer for making that easy)

Xcode’s editor tabs aren’t tied to files.

Shared registers, synchronized editing. The main advantage of tmux over vim windows is persistent sessions, but you can have that and still use vim windows.

I'm always curious the mental-model people use with tmux+vim. The combination gets brought up so often, but don't often talk about when they use Tmux panes vs vim windows.

A few reasons to prefer vim windows are diffs and sharing the same buffers when flipping between them.

They are called "windows" in Vim. That's why many related commands begin with Ctrl+w.

They are orthogonal?

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact