As of 2016-02-26, there will be no more posts for this blog. s/blog/pba/
Showing posts with label Vim. Show all posts

I couldnt run vim-particle but the GIF from the project really is interesting.

https://cdn.rawgit.com/mattn/vim-particle/3f202b19af62c9541baafc406cf83deba1bf4e70/data/screenshot.gif

It can only be run with GVim on Windows. Someone please at least ports it to Linux, and if possible, to Vim, although that could be very difficult. I wonder if you can do it within Vim, that is not drawing in X, but that would probably mess up the file being edited.

If you want to see another particle sparkling out and in terminal, try particle.cpp.

vim-particle was created by Yasuhiro Matsumoto, written in C and Vim script under the MIT License, currently git-37ecd82 (2015-12-01).

When I blog, sometimes I write in reStructuredText if the post is about technical topic involving programming codes, otherwise I just write in Blogger's editor.

Oftentimes, I was lazy when the post is very short like this one you are reading, so Blogger's editor was. If there is some HTML code or code like a >= b, I'll have to use escape tool or manually escape by hand.

Since I use Pentadactyl (fork of Vimperator), by pressing Ctrl+I, I can use external editor Vim when I need to do some re-indentation or mass replacement using regular expression. If you have done some reviewing on Stack Overflow, you definitely need to take care of a lot re-indentation, some code's spaces or tabs were eaten randomly, mysteriously, by the posters.

I adapted a code from Vim wiki, so I can also do escaping.
" ==========================================================================
" HTML Encode/Decode
" ==========================================================================
" ref: http://vim.wikia.com/wiki/HTML_entities#Perl_HTML::Entities
function! HTMLEncode()
perl << EOF
 use HTML::Entities;
 @pos = $curwin->Cursor();
 $line = $curbuf->Get($pos[0]);
 $encvalue = encode_entities($line);
 $curbuf->Set($pos[0],$encvalue)
EOF
endfunction

function! HTMLDecode()
perl << EOF
 use HTML::Entities;
 @pos = $curwin->Cursor();
 $line = $curbuf->Get($pos[0]);
 $encvalue = decode_entities($line);
 $curbuf->Set($pos[0],$encvalue)
EOF
endfunction

vmap <Leader>h :call HTMLEncode()<CR>
vmap <Leader>H :call HTMLDecode()<CR>
Only difference is the key mapping, it's most likely I only use escaping in Visual mode, because the code needs to be escaped is only a part of post. Certainly, I would select the part and escape them. If I need to escape the whole file, I can just type :%call HTMLEncode() or select all and use the key mapping.

Blogger is fine with some situations with characters like < or >, but definitely not HTML or XML element tag, you will have to escape them or it may think you actually want to use those HTML for your post.

And, yes, I know there is an option to tell Blogger not to interpret HTML code. But who uses that option, I would guess it's someone who doesn't even know what HTML is or never need to paste code into their posts. I am a programmer and I know what I want. For me, if I need italics style, most times, I wrap the text in <i/> by hand, it's much faster than moving mouse cursor to the button.

Sometimes, I got confused with what the program I was currently using. A few times, I tried to save files in other text editors or word processor by typing in :w.

I have a script to check live Justin.tv streams I follow, always ran it in shell. But moments ago, I forgot I was in Pentadactyl and wondered why auto-completion didnt finish that command for me. Somehow, I even remembered the exclamation mark in :!chk-jTAB. I had never tried the same in Vim before, either.

A few taps of TAB, I finally realized I was using the web browser. Out of curiosity, I manually entered the rest of command name and it gave me:

http://2.bp.blogspot.com/-rfXBJ4251XY/T53CRgtxRaI/AAAAAAAADR8/fPYmD6FX1fU/s1600/2012-04-30--06:19:53.png

I thought I might need to use io.system for the output, but I didnt need to. In terminal, it looks like:

http://3.bp.blogspot.com/-aZrkoyWYwP8/T53CeBUBzNI/AAAAAAAADSE/JE5Q7fTSbDk/s1600/2012-04-30--06:36:02.png

Of course, this also works in Vim and it even has auto-completion. It doesnt necessary mean I will run this script in web browser whenever I need to check up, I still prefer to read in terminal.

Subconscious has strong influence in me.

I use VimNotes and I often launch Vim and type :Note NoteName, just for editing the same note. I could map a special key for it, but this repeating process got me thinking if I could run a Vim command when bringing up Vim.

Vim likes many other programs, such as Bash or Python, it has -c option. So I can simply do the following:
vim -c 'Note NoteName'
or
vim +Note\ NoteName
That does it. You can supply up to 10 commands. The commands will be executed after (first) file is loaded, if you tell Vim to open a file or files. Also, another --cmd is available but it is executed before vimrc.

If you run Vim in server mode like me, you will need to use --remote-send:
vim --servername SERVERNAME --remote-send ':Note NoteName<CR>'
It is basically emulating your key-presses, so you may need to modify it accordingly. There is also a --remote-expr, you might find it interesting.

I won't use these, at least not for opening my note. I may set up an shell alias, but a key map in Vim is a better option. However, there may have some chances that you really need to run Vim commands in this way.

On GitHub, you can use Fenced Block to have code block syntax-highlighted, for example:
```js
var a=1;
```
Also it seems to be only way to have syntax highlighting. The indentation doesn't enable highlighting from what I see.

This will confuse Vim's syntax highlighter because that is not part of standard syntax of Markdown. Although, it is fairly coder-readable, you know it is a code block. I am not sure Markdown processors will do when they don't understand such syntax.

But that is not a major issue to me, the sort-of-incorrect syntax highlighting in Vim is. Not only Vim is confused, so am I. It is very hard to read the content while you are writing. (Note: I was using this mkd.vim)

I found a fix for it. The ideal way to have additional syntax is to add an after-syntax, normally they are stored in ~/.vim/after/syntax. So I added a file with the following content:
syn region markdownCode matchgroup=markdownCodeDelimiter start="``` \=" end=" \=```" keepend contains=markdownLineStart
I didn't use that fix, it seems to have slight glitch like default markdown syntax highlighting from Vim for fenced block. But maybe it is because I messed some stuff up while I was trying with that mkd.vim. Right now, I am only using highlighting from Vim.

When I am writing blog posts using reStructuredText, I have F5 bound to running an external script with current filename using %. The external script generates temporary file with template, which I can preview in browser with. The post title is extracted from the filename.

Inevitably, characters like \' or ! are used sometimes. If you have used shell long enough, you will know those characters may cause issues if they are not quoted or escaped correctly.

Since I am just a normal Vim user, not really have much knowledge and experience of coding for Vim configuration. But this time, I managed to put together stuff to get away from escaping issue.

My original keybinding is as the following:

map <F5> <ESC>:w<CR>:!gen-blog-rst.sh '%'<CR>

It saves the file, then run the gen-blog-rst.sh script. As you can see \'%\' definitely will have issues with \' character. I found out there is a Vim function shellescape() which does what I expect:

:echo shellescape("\"blah\" 'foorbar' blah!")
" outputs the following string
'"blah" '\''foorbar'\'' blah!'

You can safely pass to :!some_command as an argument. It will be like running (my default shell is Bash):

bash -c some_command '"blah" '\''foorbar'\'' blah!'

This some_command will get that long word from $1.

The problem is when using :!{cmd}, I can not put anything function in it, it is basically a plain string be passed to your default shell. I dont know if Vim has something like Command Substitution in Bash to work in string, if it does, let me know.

The solution I found for this part is to use :exec[ute] to wrap it up:

:exec '!gen-blog-rst.sh ' . shellescape(%)

So, the final map is:

map <F5> <ESC>:w<CR>:exec '!gen-blog-rst.sh ' . shellescape(%)<CR>

Note that % is used for the function, instead of just %. I used this to update my vimrc.

Once again, I switch to something better. But this time is different, it's not the Vimperator broken, well it is in some sense of interpretation. It's because the basis of philosophy. I would never expect myself switching because this kind of point of view. I always think people who so insist in using Open Source are like Saints. (Hope you get what I really mean in that)

Anyway, after I posted about encryption status in Vimperator, I stumbled on this wiki page, VimperatorVsPentadactyl. Now I see why they have been getting rid of us.

Right now, I have switched to Pentadactyl. Here is the diff of what I have changed. It virtually produces exactly same result as before.

A few things I have learnt:
  • Vim syntax file is generated by running :mks in Pentadactyl. It will put the file at right place for you.
  • Colorscheme extension is .penta.
  • guioptions not toolbars, since that's where it was. It also has option for statusline, so I don't need to hack the style anymore.
  • You need to define function in window in your ~/.pentadactylrc, i.e. window.func = function(){};. this !== window.
  • :if :elseif :else :endif is nice.

Pentadactyl intergates statusline with the Addon bar, so those addon button, you can see next to the statusline. I personally do not like this. But I like the loading indication, which is what Vimperator nuked. And the encryption status, also what Vimperator nuked. I now understand how many stuff Vimperator has erased.

I have known Pentadactyl long ago, at that time, Vimperator has made a lot of changes, so I decided not to switch. But, it doesn't really seem to change much, some of my code I didn't even touch.

Two things I have to complain is now I need to get used to C-V and C-Z after I finally got comfortable with inhuman Ins or S-Esc.

The latter is the worst keybinding throughout the computing history, though insert key isn't much better, that takes me eons to get used of them. They should've just tell those people, use your mouse to uninstall Vimperator, problem solved!

The second thing, the name is...

Time for the final step:

:extdelete Vimperator

Mission accomplished.

some tweaks for coding Python in Vim


I have been wanting to have that red vertical line since I started coding Python. The current line highlighter is also one thing I want to have.

Now I have more than those. You can see the vimrc diff.

I didn't really need those indentation indication but they are definitely helpful. I found them in this blog post, the original is a after-syntax file and it's for tabs. I managed to make it work for spaces.

At first, all these are turned on by default. And I felt they did slow Vim a bit. So I made them to be switchable. I really didn't know much about Vim customization/scripting, so my skill in that diff must seem awful. Anything you have in mind, please leave a comment.


You can press F8, then press one of F7 to F10 to toggle those.

There is a big issue since I started to use Vim, using Vim with terminal multiplexer such as GNU/Screen or tmux, there is always some issue comes from nowhere and you wouldnt have if you just use Vim in xterm or urxvt.

I had been navigating in my files with a silly way: Press Left/Right key to go up/down in a line. There is a faster way: Press Shift+Left/Right to move backward/forward a word. But it never worked when I was in Screen or tmux.

I googled it and got the solutions, and knew the exactly the problem is. If you look at the key control codes (returned by cat -v), you will see the problem.

$TERM Left S-Left C-Left
xterm ^[[D ^[[1;2D ^[[1;5D
rxvt-unicode ^[[D ^[[d ^[Od
screen ^[[D ^[OD ^[OD

For xterm and urxvt, they both have correct reaction in Vim; for Screen and tmux (tmux uses screen as $TERM), they havent and they are worse because there is no distinguish between Shift+Left and Ctrl+Left, but for former problem its fixable.

There are a few ways to fix it, I decided to remap the keys. And I have these in my .vimrc:

map ^[OC <Right>
map ^[OD <Left>
map ^[[C <S-Right>
map ^[[D <S-Left>

You can not just copy them to your .vimrc, you have to input ^[OC by pressing in insert mode: Ctrl+V, .

When I try to change Layout of blogs on Blogger, those popups are always opened as new tab. I disabled some and re-enabled addons, then I found out this is about Vimperator. It always overrides browser.link.open_newwindow.restriction with value 0. Which caused the popups open as in a new tab. No matter how you manually reset or issue a command :set popup=2 in Vimperator, that goes back to 0 after Firefox restarts.



After read the :help, Vimperator does support a configuration file, ~/.vimperatorrc. That functions like a .vimrc. Simply write your commands in, so I have

set popups=2
in ~/.vimperatorrc.



RTFM is the solution.