DEV Community

Volker B. Duetsch
Volker B. Duetsch

Posted on

Some Vim snippets from my workflow

Intro

There exist a lot of introductions to Vim. So I avoid writing another one. I want to share some information that helps me in my workflow and a small part of customizing.

Everyone should be able to use the tools that are best suited to their individual way of working. For all kind of writing stuff, for me, it's Vim. Bram I miss you.

Add words to own/specific spell file

Define your own spell file in your .vimrc.

set spellfile=d:/apps/vim/spell.add
Enter fullscreen mode Exit fullscreen mode

With zg the word under the cursor is added to spell.add file.


Auto commit a file to an existing repo, if you write it

autocmd BufWritePost d:/projects/my/wiki/* execute '!git add % && git commit -m "Automatic commit: % by Vim"'
Enter fullscreen mode Exit fullscreen mode

Every time I write a file to d:/projects/my/wiki it will automatically be committed to the existing repo.

The % in "Automatic commit: % by Vim" will be replaced with the filename.


Dictionaries for technical terms

I write almost everything exclusively in Vim. I have often to deal with technical terms or project related terms, so I use several dictionaries.

set dict=d:/apps/vim/terms_p1.txt
set dict+=d:/apps/vim/own_tags.txt
set complete+=k
Enter fullscreen mode Exit fullscreen mode
  • terms_p1.txt holds terms specific to my current project (p1).
  • own_tags.txt holds special terms/tags I often use.
  • set complete+=k is needed to complete the terms with ctrl-n

Dictionaries allow only words without spaces, one word per line.


Find out where a key mapping comes from

:verbose map gx


Show current mode by color of statusline

I use orange for normal mode and green for insert mode.

au BufEnter * hi statusline guifg=black guibg=orange
au InsertEnter * hi statusline guifg=black guibg=green
au InsertLeave * hi statusline guifg=black guibg=orange
Enter fullscreen mode Exit fullscreen mode

My customized status line

I know there are several great plugins, but I try to depend on as little as possible plugins.

set laststatus=2                                    " always show statusline
set statusline=
set statusline+=%{StatuslineMode()}
set statusline+=\ %h%m%r%w                          " flags
set statusline+=\ %F                                " path and filename
set statusline+=\ \-\-\ %l:%L\ \-\-\ %p%%\ \        " line from lines, in %
set statusline+=\ \-\-\ Col\ %c                     " 2 spaces, column number
set statusline+=\ \-\-\ Bn\ %n                      " Buffer number
set statusline+=%=                                  " right align
set statusline+=\FT\:\%{strlen(&ft)?&ft:'none'},    " filetype
set statusline+=\ \ENC\:\%{strlen(&fenc)?&fenc:&enc}, " encoding
set statusline+=\ \FF\:\%{&fileformat},               " file format
set statusline+=\ \%{wordcount().words}\ words,         " number of words
set statusline+=\ \%L\ lines,         " number of lines
set statusline+=\ size\:\ \%{getfsize(expand(@%))}

function! StatuslineMode()
  let l:mode=mode()
  if l:mode==#"n"
    return "NORMAL"
  elseif l:mode==?"v"
    return "VISUAL"
  elseif l:mode==#"i"
    return "INSERT"
  elseif l:mode==#"R"
    return "REPLACE"
  elseif l:mode==?"s"
    return "SELECT"
  elseif l:mode==#"t"
    return "TERMINAL"
  elseif l:mode==#"c"
    return "COMMAND"
  elseif l:mode==#"!"
    return "SHELL"
  endif
endfunction
Enter fullscreen mode Exit fullscreen mode

The function is not from me. I don't remember where I got it from. Thanks to the developer.


Vim and shell

Will pipe the output to vim:

- fd PATTERN | vim -
- rg PATTERN | vim -
- curl -s https://example.com | vim -
Enter fullscreen mode Exit fullscreen mode

Convert a diff to HTML

:TOhtml most people probably know.
The command creates a new buffer with the buffer content of the buffer from which the command was called, converted to HTML.

vim -d file1.go file2.go +TOhtml +"w diff.html" +qa!

vim -d file1.go file2.go -- starts vim in diff mode for file1.go and file2.go 
+TOhtml                  -- convert to html
+"w diff.html"           -- write in file diff.html
+qa!                     -- close all buffers and exit
Enter fullscreen mode Exit fullscreen mode

Let Vim write

No snippet-plugin or abbreviation is used! I can't remember when for which task I used this, maybe just because it is possible.

Of course also works with echo, cat, bat, etc.

vim +"normal iHello" +"w n.txt" +q

vim +"normal iHello" -- will start Vim and type Hello
+"w n.txt"           -- will write current buffer to file n.txt
+q                   -- will close Vim
Enter fullscreen mode Exit fullscreen mode

Folding

I use folding by marker. Default marker is {{{ and }}}. If you don't like the default you can try something like this:

set foldmethod=marker
set fmr=▶▶▶,◀◀◀
Enter fullscreen mode Exit fullscreen mode

Use a fixed set of emojis

ab :fr: 👉
ab :fl: 👈
ab :fh: 👍
ab :gk: 🟩
ab :rk: 🟥
ab :yk: 🟨
Enter fullscreen mode Exit fullscreen mode

Just type :fr: followed by space and the emoji will show up

I'm not a big fan of emojis so for my personal documents I like to decide to get rid of one or another.
To search for them:

:vimgrep /[^\x00-\x7F]/g %
Enter fullscreen mode Exit fullscreen mode

The result can be found in the quickfix list, use :copen to open it.


Indent and out dent

For writing my outlines I need this often.

In insert mode:
ctrl-t -- indent
ctrl-d -- out dent

In normal mode:
>> -- indent
<< -- out dent
Enter fullscreen mode Exit fullscreen mode

For my Golang coding I mostly rely on go fmt.

For other languages gg=G and >i}.


Moving lines to marks

Define a mark m by typing mm in normal mode.

Now you can do something like

:.,+1m'm

.,+1 -- current line plus 1 following line
m    -- move
'm   -- to mark labled m
Enter fullscreen mode Exit fullscreen mode
:m0           -- move current line to begin of buffer
:m$           -- move current line to end of buffer
:g/PATTERN/m$ -- move all lines matching PATTERN to the end of buffer
Enter fullscreen mode Exit fullscreen mode

Other useful stuff

:g:PATTERN:d -- delete all lines containing PATTERN
:v:PATTERN:d -- delete all lines NOT containing PATTERN

:g:PATTERN:d is similar to :g/PATTERN/d 
but with : instead of / you often can save 
the need to escape special chars.

:g/pattern1/,/pattern2/d -– delete all lines between PATTERN1 and PATTERN2
:%normal A;              –- adds ; to the end of every line
:%normal I//             -– adds // to the beginning of every line
Enter fullscreen mode Exit fullscreen mode

g+        -- "Time machine": go to newer text state.
g-        -- "Time machine": go to older text state.
gM        -- go to the middle of the line that is visible
gJ        -- Join the next **line** without space
dH        -- delete from cursor position to top of the screen / not buffer
dM        -- delete from cursor position to middle of the screen / not buffer
dL        -- delete from cursor position to bottom of the screen / not buffer
d/PATTERN -- delete from cursor position to position before PATTERN is matched
ctrl-6    -- switch between two buffers
Enter fullscreen mode Exit fullscreen mode

Helpful resources


Whoami

In more than 30 years in IT project support and software development, I have taken on numerous roles, e.g. developer, lead developer, troubleshooter, "care taker", head of xxx, Scrum Master, Product Owner, project manager, moron on duty etc. - from conception to implementation.

I'm using Vim since ~1997.

I have seen many programs come and go. Vim has always been there. I have never regretted learning Vim. And I am still learning something new. There is only one thing you should avoid: adjust your Vim config daily ;)

I'm slowly publishing my expierences as dev to GitHub converting projects notes etc. sitting in paper and several large text and markdown files. If you like check to out my field notes: https://github.com/vbd/Fieldnotes

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more