Editing in binary - vim

I've been tinkering with multiple hex editors but nothing really has worked.
What I'm looking for is a way to change a binary in actual binary (not in hex). This is purely for educational purposes and I know it's trivial to convert between both, but I wanted to be able to change the ones and zeroes just like I would do hex.
I've tried using vim with the %!xxd -b but then it won't work with %!xxd -r. I know how to convert the file into binary, but I'm looking for a way to dynamically change it in this format and being able to save it.
Better yet would be if I could find a way to actually create a binary by coding purely in actual binary.
Any help would be appreciated :D

vim or gvim should work for you directly, without the xxd filter.
Open the file in (g)vim. Place your cursor on a character and type ga to see its character code in the status line. To insert character NNN, place your cursor where you want it, go in insert mode and type Ctrl-v and then the three digit decimal code value. Use Ctrl-v x HH to enter the character by its hexadecimal code.
Make sure your terminal is not set to use UTF8, because in UTF8, typing Ctrl-v 128 will in fact insert c280, the utf-8 encoding of character 128, instead of 80.
LC_ALL=C vim binary-file
is the easiest way to make sure you're doing binary character based editing in vim, but that might do weird things if your terminal is utf-8.
LC_ALL=C gvim binary-file
should open a stand-alone window with proper display.
FYI, if you did want to work in utf-8, Ctrl-v u HHHH is how to enter the Unicode character with Hex code point HHHH.

windows
open cmd.exe or notepad++ or whatever editor
enable numlock key
On laptops you need to use the function key or the blue / grey silver numbers above alphabet keys (using the numbers on the top line will not work as they map to different scan code.
press alt key + 255 will correspond to 0xff
press alt key + 254 will correspond to 0xfe
see below for a demo
C:\>copy con rawbin.bin
 ■²ⁿ√·∙⌂~}─^Z
^Z
1 file(s) copied.
C:\>xxd rawbin.bin
0000000: fffe fdfc fbfa f97f 7e7d c41a 0d0a ........~}....
C:\>

Related

How to fill in a binary file with a hex value in vim?

As in subject.
I'd like to fill in a part of the binary file (base address + offset) in vim with a value given.
For example I'd like to add 1M values of 0xfd starting from address 0x0fff.
What command could I use?
Open the file in binary mode, either via vim -b {filespec} or via :edit ++bin {filespec}.
Go to byte index 0x0fff with the go command or :goto. You need to convert to decimal, e.g.: :exe 'goto' 0x0fff.
Insert the value; you can supply a [count] to the i command, and insert arbitrary hex values via :help i_CTRL-V_digit: 1048576i<C-V>xfd<Esc>
Write the file and exit :wq.
An alternative within Vim is using an intermediate hex dump format and converting back and forth via the xxd tool that ships with Vim. See Improved hex editing for details.
So, while Vim isn't the perfect tool for this (as mentioned in the comments, a hex editor is the right / better tool), it can be done.

How to insert Unicode character U+2611 in gvim

when I try to enter this Unicode character :☑(U+2611) in vim using the command like : ^Vu2611 (which means press ctrl+V then type u2611 in insert mode),Vim somehow breaks it into two characters : &(26) and ^Q(11).
There's no any problem when I tried to insert other kind of characters like □ (U+a1f5).
It seems like Vim stopped its parsing immediately after 26 (which represents character '&') has been read .
So,how can I insert this kind of Unicode characters in Vim (I have tried to paste it into Vim ,it doesn't work)?
Please Help!!!
In order to process Unicode characters, Vim must use an 'encoding' that is able to represent those characters. With a value of latin1, the mentioned character cannot be encoded (this 8-bit encoding only includes ASCII and several Western European characters, see here).
So, you need to
:set encoding=utf-8
With that, any newly created file will use that encoding, and you should be able to insert Unicode characters and write them (also with another Unicode file encoding, like :w ++enc=ucs-2le; but if you tried to persist as :w ++enc=latin1, you'd get a CONVERSION ERROR).

(VIM) Is vimgrep capable of searching unicode string

Is vimgrep capable of searching unicode strings?
For example:
a.txt contains wide string "hello", vimgrep hello *.txt found nothing, and of course it's in the right path.
"Unicode" is a bit misleading in this case. What you have is not at all typical of text "encoded in accordance with any of the method provided by the Unicode standard". It's a bunch of normal characters with normal code points separated with NULL characters with code point 0000 or 00. Some Java programs do output that kind of garbage.
So, if your search pattern is hello, Vim and :vim are perfectly capable of searching for and finding hello (without NULLs) but they won't ever find hello (with NULLs).
Searching for h^#e^#l^#l^#o (^# is <C-v><C-#>), on the other hand, will find hello (with NULLs) but not hello (without NULLs).
Anyway, converting that file/buffer or making sure you don't end up with such a garbage are much better long-term solutions.
If Vim can detect the encoding of the file, then yes, Vim can grep the file. :vimgrep works by first reading in the file as normal (even including autocmds) into a hidden buffer, and then searching the buffer.
It looks like your file is little-endian UTF-16, without a byte-order mark (BOM). Vim can detect this, but won't by default.
First, make sure your Vim is running with internal support for unicode. To do that, :set encoding=utf-8 at the top of your .vimrc. Next, Vim needs to be able to detect this file's encoding. The 'fileencodings' option controls this.
By default, when you set 'encoding' to utf-8, Vim's 'fileencodings' option contains "ucs-bom" which will detect UTF-16, but ONLY if a BOM is present. To also detect it when no BOM is present, you need to add your desired encoding to 'fileencodings'. It needs to come before any of the 8-bit encodings but after ucs-bom. Try doing this at the top of your .vimrc and restart Vim to use:
set encoding=utf-8
set fileencodings=ucs-bom,utf-16le,utf-8,default,latin1
Now loading files with the desired encoding should work just fine for editing, and therefore also for vimgrep.

how to get the digraph key or unicode from a special character

I am configuring tmux & powerline. I would like to change the default separators in tmux status line. I managed to figure out where to do it, however I could not get the special character that I want.
I want to type, in Vim, a left/right pointing triangle that spans the whole line-height, but the only thing I could find is a small triangle (unicode : 25B6,25BA,25C0,25C4...)
There is a big right pointing triangle already in a powerline configuration file, which I could copy and paste, but I want to know its unicode and want a left one. Is there a way to get the unicode from the symbol in Vim or anywhere else?
You can get the codepoint value of a character in Vim by positioning the cursor on the character in question and typing either ga or :ascii.
You can either use ga in command mode or :ascii on the command line (even though it says ascii, it shows information for general encodings)
You may add a permanent preview of current character in your status line (see :h statusline), eg.:
:let &statusline = &statusline . "\ [%03b\ 0x%B]"

vim linebreak after hyphen with gq

Is there some way to configure vim's gq function so that it allows line breaking after a hyphen (in a compound word)? e.g.
twentieth-
century
And by the way, I'm not on my own laptop, but on one of the lab, which runs Windows, so any solution not using programs like par or fmt get bonus points :-) (though I'm also interested in solutions using these tools if this is not possible to do this using only vim -- at least I would be able to do it on my laptop).
Thanks in advance.
For the ASCII hyphen (0x2d), this isn't possible. If you're able to use Unicode, you can use the identically-looking Unicode variant (U+2010, cp. http://en.wikipedia.org/wiki/Dash) instead. This can be inserted via Ctrl-V (Ctrl-Q on most Windows installations of Vim), followed by u2010. Or define a digraph for it:
:digraph -- 8208 " hyphen, U+2010
With
:set formatoptions+=m
Vim will
Also break at a multi-byte character above 255.
Voila! If you need to persist the text as ASCII, you could even write mappings / a wrapper around gq that :substitutes the hyphens back and forth.

Resources