Categories
geek software tools vim

using vim to write prose

Photo by Chris Lawton on Unsplash

I use Vim to write blog posts, notes and documentation before using another text editor. I don’t consider myself a true writer, but I like to use the speed and shortcuts I know from writing code while I’m writing prose as well.

Vim is primarily a programmers’ editor, so to make it better for general writing, here are a number of tricks and plugins I find useful.

If you are new to Vim/GVim, but interested, you can read about setting Vim up in Windows and how to start configuring it.

Built in spell checker

Vim has a built-in spell checker you can activate with :set spell or :setlocal spell to activate it for the current buffer.
I keep forgetting how to switch it on and off, so I added these shortcuts to my Vim configuration to easily switch spelling on and off again.
With these mappings, I can enable spelling for English or Dutch, and turn it back off. Handy if you write in multiple languages.

" -- Spelling, cause I keep forgotting how to do that
nnoremap <Leader>se <Esc>:setlocal spelllang=en<CR><Esc>:setlocal spell<CR>
nnoremap <Leader>sn <Esc>:setlocal spelllang=nl<CR><Esc>:setlocal spell<CR>
nnoremap <Leader>ns <Esc>:setlocal nospell<CR>

Find out more in the help file with :h spell

Themes

If you’re using GVim on Windows, the default look is just plain ugly. To get a wide range of good-looking color schemes, you only need to install the base-16 vim themes I talked about here. These also look good in a terminal window.

Proselint and ALE

Proselint scans your text for linguistic problems. It isn’t a grammar checker. It’s an aggregation of best writing practices integrated in a command line tool. Used together with the ALE plugin, you can scan your text files automatically with Proselint and get markers in Vim when things are not quite right.

Proselint is a Python tool, so you need to install Python first, then install the Proselint package using the Pip package manager.

pip install proselint

ALE will pick up Proselint automatically and indicate any problems by adding an indicator in the margin.

Proselint highlights a number of problems with blue, squiggly lines.

A bigger font for writing

Vim-fontzoom is a simple plugin to increase your font size. Good for your eyes on small screens, and works excellent in combination with the focus writing plugins listed below.

Previewing markdown

If you are often writing in Markdown format like myself, the Previm preview plugin comes in handy. It renders your Markdown document to HTML and opens it in a browser window. It’s great if you split the browser and (g)Vim window side by side, because it automatically updates the HTML file when you save.

A Markdown preview of this very post.

Focus writing

Get rid of the code editor look Vim sports by default and enter distraction-free writing environment with Goyo and Limelight.
Use :Goyo to remove all distractions, use :Limelight to highlight the current lines you are working on.

Goyo and Limelight for distraction free writing in Vim.

Finding files

If you have folders with notes and other documents in your working folder and subfolders, finding them fast without having to exit Vim is handy.
CtrlP is a great plugin to do just that. Hit the Ctrl-P shortcut and start typing a word that’s part of a file or subfolder somewhere in your current path, to find the file.

Highlighting text

Plain text files are just too plain. With txt.vim you’ll get some “syntax” highlighting for even the most boring of files. Numbers, dates, smileys and bracketed text look better with this little plugin.

Markdown files already have syntax highlighting in Vim, so you don’t need an extra plugin for that.

Text completion

If you made it this far, you’re a pretty hardcore Vim user already. So why not take it up another notch? CoC or Conquer of Completion is a plugin that masters code completion for all sorts of programming languages.
It’s also useful for any type of text document. Any word in your document is ready to be reused for automatic completion in Vim. Type half a word, or a few letters, and it presents you a list of already typed words from the current buffer. Handy if you frequently use long and complex names, like for writing documentation or science-fiction.

I use this together with Supertab, which makes it easy to complete words by pressing tab. Just like I’m used to in other editors for programming.

More Vim writing resources

In a VimConf 2021 session Theena Kumaragurunathan (an actual writer) demonstrates his writing an world building workflow using Vim.

He also has the GitHub repository OVIWrite up with a Vim configuration setup for writers. I’m not sure if you can just copy and paste this setup, but it’s an interesting starting point to see what plugins he’s using.

I noticed quite a few plugins that I highlighted here, so I guess I’m on the right track. :)

Categories
programming tips tools vim

base16-vim has all the vim themes you’ll ever need

You think that’s a bold statement? Hear me out. Vim themes are great, but you (and most certainly I) spend way too much time downloading and trying out all these fancy new themes, right? At some point I came across this base16-vim plugin which has a shit-load of cool themes, all using a base of 16 colors. I haven’t looked back since. Well, maybe once of twice, but still, I’m sticking to it.

Base16-vim contains 128 different themes, and it has all the popular ones like the github theme, solarized (dark & light), monokai, gruvbox and many more. To avoid spending too much time trying them all out, here are some examples of the ones I like the best. If you want to try them in a browser, check out the previews to find the perfect one for your setup.

Be sure to check out the geeky base16-greenscreen theme, it’s awesome.

Categories
programming tips vim

javascript development with vim

Vim is great as a lightweight editor for JavaScript programming. But pretty soon I end up missing some more advanced features like syntax checks and code completion. Syntax highlighting only takes you so far.

Recently I was working on a Node.JS project, so I thought I’d find me some plugins for Vim to get those more advanced development features. Vim is super extensible with its massive plugin ecosystem, but sometimes it can take some tinkering to get things to work. After a bit of searching and trying things out, I managed to get code completion working for JavaScript code, and have basic syntax checking active.

I found that the CoC plugin, in combination with some of its extensions work perfectly.

First you need to install the CoC plugin. This plugin is more of a framework to get code completion and VSCode-like features in Vim. So you can use this for other languages too, like Python, Rust, Java, PowerShell, C#, etc. You need Node installed for this to work, but if you’re working with JavaScript, you most likely have that installed already.

Once you have the plugin set up, you then add CoC extensions for the languages you want to use. For JS this turns out to be the TypeScript server plugin, which gives your JS & Typescript support, including JSX and TSX. I also added the JSON plugin.

To add the CoC extensions, open Vim and install the extensions using:

:CocInstall coc-json coc-tsserver

That’s about it. Now if you open up a .js file, you should be getting JS code completion. If you mess up some code, you’ll get some error indications in the margins, telling you something is not quite right with your code.
Handy stuff!

Yep. There’s clearly something wrong with that code.
Helpful code completion info right there.

A good way to find a list of more CoC extensions is through VimAwesome. There’s something in there for everyone it seems.

Categories
geek programming software tips vim

url decode and encode text in vim

Artistic closeup of a .vimrc file

Vim has a lot of cool features, and one of them is the ability to take the text you are editing and run it through whatever text-parsing tool you like.

This tool can be a shell command, a standalone executable, or your very own script in your favorite scripting language.
The magic is all in this vim command:

:%!<shell command>

This will dump your buffer as standard output to the shell command and replace it with whatever the output of that command is.
You can also use this to fill up your buffer with text from a command, without having to switch to a console and copy-paste it. For example, this works fine:

:!dir *.*

But on with the magic stuff. Let’s use PowerShell to URL encode or decode text we have in our Vim buffer.
To set this up, we first need a PowerShell script that does just that. Through the power of .NET, this is pretty easy.
Here’s my decode-string.ps1 script:

[Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
[System.Web.HttpUtility]::UrlDecode($input)

This is the encode-string.ps1 script:

[Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
[System.Web.HttpUtility]::UrlEncode($input)

There is one trick to make this all work: the $input variable.
This magic variable will automatically contain all the input piped into the command, so you don’t need to specify an input parameter. It’s one of the automatic variables in PowerShell, just like $_.

Once you have these, all we need to do is set up some Vim bindings in our vimrc file to make this super easy to trigger.

nnoremap <Leader>dc :%!powershell.exe -noprofile -nologo -file c:\mytools\decode-string.ps1<CR>
nnoremap <Leader>ec :%!powershell.exe -noprofile -nologo -file c:\mytools\encode-string.ps1<CR>

What we do here is paste the current buffer’s text with %! to our script, by running the powershell.exe command. Passing in the -noprofile and -nologo make it run faster, because it skips loading custom user profile scripts.
From now on I can paste a URL encoded string in my Vim, press <leader>dc and *bam*, I have the decoded text right there.

Nothing is stopping you from using this for all sorts of text manipulations. You could use this to generate and format code for example. A JSON formatter is an option, although I use the jdaddy plugin for that. You don’t have to use PowerShell either. You can use any command line tool or scripting language you like, as long as it uses standard input & output, and can be called from the command line in Vim.

Happy hacking!

Categories
geek programming software tips tools vim

cool vim tips and tricks you might not know

Vim has tons of awesome shortcuts and features you pick up over time. Some of those I don’t use every day so I have to write them down so I can look them back up when I can’t remember exactly how it works. Instead of keeping them locked away in a text file, I’ll throw them online here and spread the Vim love. None of these need any special plugins. They should all work right out of the box with plain old Vim.

If you want to know more about a specific command listed here, use the Vim :help command to find out more. There are usually more options and possibilities for each of these commands and the Vim documentation is excellent.

Here we go!

When you are on the command line using a console application and you want to edit the output in Vim right away, or open that long list of possible command line switches in Vim for reference, this one will come in handy.
I’m using GVim here because since that opens in a separate window from your shell, this is the most useful.

ls *.txt | gvim -
docker -h | gvim -
git --help | gvim -

This one is for opening a ton of files in a single Vim instance from Powershell, in different tabs. This means you are running this from a Powershell console of course.

gvim -p (ls *.ps1)

For more Vim command line options run this in your favorite shell environment:

vim -h
gvim -h

How about opening a remote file, or fetch HTML from a page over HTTP using Vim:

:e https://n3wjack.net/

When you work with log files a lot, being able to delete lines containing or not containing a specific word can be just what you need.
These two commands are perfect to filter out obsolete exceptions and figure out what is causing that nasty production issue:

:g/DeleteAll/d
:v/DeleteAllButThis/d

Did you know that Vim has a spell checker? I didn’t know that at the beginning (try :h spell for more details).
To activate/deactivate:

:set (no)spell

To jump to the next / previous misspelled word:

]s
[s

To get a list of spelling suggestions (or use the mouse in GVim, which is quite practical):

z=

You can add a simple XML-tidy shortcut to your .vimrc file by adding the following command.
What it does is setting the file type to XML, removes spaces between opening & closing brackets, add a return character in-between the opening & closing brackets and finally formats the document so it looks all nice and indented.

nmap <leader>ppx <Esc>:set filetype=xml<CR>:%s/> *</></g<CR>:%s/></>\r</g<CR><ESC>gg=G<Esc>:noh<CR>

You can force syntax highlighting in Vim as follows for e.g. HTML, XML and Markdown.
Of course this works for a ton of other file types as well, as long as you can figure out what the extension/file type key is. But that’s pretty easy in most cases.

:set syntax=html
:set syntax=xml
:set syntax=md

I add shortcuts for any files I frequently edit by using the leader key and a letter combination that’s easy to remember.
For example this one to edit my custom .vimrc file when I press the leader key followed by “e” and “v” (edit vimrc).

nnoremap <Leader>ev :tabe ~\vimfiles\nj-vimrc.vim<CR>

That’s about it. For more nice Vim tips check out more Vim posts. Another good resource for bite sized Vim tips is the MasteringVim on twitter and it’s newsletter.