Categories
Tools

VIM and tabs and simplicity

I use vim at work for all of my coding. I recently tried to abandon NERDtree in favor of CtrlP, coaxed by a co-worker who did the same. This is an abandonment of a directory tree to find my files in favor of using fuzzy search to find the filename. After I made this switch, I felt like I needed tabs. They didn't seem necessary when I had a directory tree there, but now I feel like I need the tabs so I can switch back for forth between files more easily. I guess I didn't mind clicking multiple times, but I mind typing file names multiple times.

I also program mostly in grails these days, developing a web application that monitors some of our services. In grails, filenames are named in a very specific way so repeat file names are not common. Therefore, the default tab name in vim, whose path is cleverly compressed, is still too verbose for me. E.g.:

[code lang="plain"]
" Tab formatting
" http://vimdoc.sourceforge.net/htmldoc/tabpage.html#setting-tabline
function MyTabLabel(n)
let buflist = tabpagebuflist(a:n)
let winnr = tabpagewinnr(a:n)
let file = bufname(buflist[winnr - 1])
return fnamemodify(file, ':p:t')
endfunction

function MyTabLine()
let s = ''
for i in range(tabpagenr('$'))
" select the highlighting

if i + 1 == tabpagenr()
let s .= '%#TabLineSel#'
else
let s .= '%#TabLine#'
endif

" set the tab page number (for mouse clicks)
let s .= '%' . (i + 1) . 'T'

" the label is made by MyTabLabel()
let s .= ' %{MyTabLabel(' . (i + 1) . ')} '
endfor

" after the last tab fill with TabLineFill and reset tab page nr
let s .= '%#TabLineFill#%T'

" right-align the label to close the current tab page
if tabpagenr('$') > 1
let s .= '%=%#TabLine#%999Xclose'
endif

return s
endfunction

:set tabline=%!MyTabLine()
[/code]

I found most of that in the docs, but this line I wrote to just pull the base filename:

[code lang="php"]
let file = bufname(buflist[winnr - 1])
return fnamemodify(file, ':p:t')
[/code]

I wish there were a simpler solution, but for now this works.

Categories
Tools

vim and markdown editing

I struggled with vim a bit tonight. I wanted to set it up to edit markdown files and have it behave like a word processor, where at a certain point on a line the text would automatically wrap. After googling for, well, hours, I found out that vim is not a word processor and word wrap simply isn't in its genes.

Well, kind of.

The first thing I did was install the markdown file type plugin. There are a few and I think I like this one the best: https://github.com/tpope/vim-markdown. Not sure why, it just seems to do the most stuff.

Then I set a few options in .vimrc, specific to markdown filetypes:

[sourcecode language="plain"]
autocmd Filetype markdown setlocal wrap
autocmd Filetype markdown setlocal linebreak
autocmd Filetype markdown setlocal nolist
autocmd Filetype markdown setlocal columns=80
[/sourcecode]

The columns=80 actually sets the width of the vim application to 80 columns, which sets he size of the terminal window to 80-columns on my Mac's iTerm. This is a bit annoying. What I really wanted was for vim to be wide, like the terminal, but for the text to wrap at 80 characters.

Meh.

Alternatively, if I could live with line breaks, I could set up vim so hitting gq would force a paragraph to wordwrap, or gggqG to format the entire document. That config looks like this:

[sourcecode language="plain"]
autocmd Filetype markdown setlocal tw=80
autocmd Filetype markdown setlocal wm=4
autocmd Filetype markdown setlocal fo=cat
[/sourcecode]

But I can't live with line breaks. This markdown editing is what I do all of my writing in..., it's simple. I can strip it out if necessary, or I could go the other way and generate html, pdf's, etc. More on that in another post.