Can't use CTRL-<Space> with Cscope plugin in gvim (windows) - windows

Configuration
Windows 7 Service Pack 1 (64-bit)
VIM 7.4 (2013 Aug 10), 32-Bit GUI version
cscope_macros.vim plugin from www.vim.org, version 2.0.0
The problem
The plugin maps several cscope find functions to open in horizontal or vertical splits using 'CTRL-spacebar' or <CTRL-#>, as this is how VIM recognises it according to the plugin documentation. here is a snippet from the plugin:
" Using 'CTRL-spacebar' (intepreted as CTRL-# by vim) then a search type
" makes the vim window split horizontally, with search result displayed in
" the new window.
"
" (Note: earlier versions of vim may not have the :scs command, but it
" can be simulated roughly via:
" nmap <C-#>s <C-W><C-S> :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-#>s :scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-#>g :scs find g <C-R>=expand("<cword>")<CR><CR>
However, 'CTRL-spacebar' does not work. When I look at what has been mapped <C-#> is actually translated as <nul>. For example, if I use the command :map, this is the result for the cscope plugin mapped keys.
n <nul>d :scs find d <C-R>=expand("<cword>")<CR><CR><Tab>
n <nul>i :scs find i <C-R>=expand("<cfile>")<CR><CR><Tab>
n <nul>f :scs find f <C-R>=expand("<cfile>")<CR><CR><Tab>
n <nul>e :scs find e <C-R>=expand("<cword>")<CR><CR><Tab>
The only thing I can find 'CTRL-spacebar'/<CTRL-#> (:help index) is
tag char action in Insert mode ~
-----------------------------------------------------------------------
i_CTRL-# CTRL-# insert previously inserted text and stop
insert
But this is not the behaviour I observe.
When I try using 'CTRL-spacebar' in insert mode all that happens is that a space is inserted at the cursor. When I use it in normal mode it seems to move the cursor to the beginning of the next word, or the next line if it is blank.
So, how do I map 'CTRL-spacebar' in VIM on windows?

In Windows GVIM, use the straightforward <C-Space> as the left-hand side of the mapping. <C-#> or the equivalent <Nul> is a workaround for the (Linux) terminal, which in general offers fewer mappable keys. The instructions were presumably aimed at that only.

Related

How to change font properties in PowerShell 5?

I want to make the font as bold for the path that I'm printing using Write-Host. I'm flexible to using other methods like echo or something else.
I've tried other methods like Write-Debug, etc, also checked the module WindowsConsoleFonts.
But none of them supports font properties like making them bold or italic while printing them.
$pathString = "[" + (Get-Location) + "]"
Write-Host $pathString -ForegroundColor Cyan
I'm using PowerShell 5.1 which doesn't support MarkDown rendering, else I would have done it using Markdown.
You can achieve bold text via VT (Virtual Terminal) escape sequences.
However, regular Windows console windows (conhost.exe) do not support italics, and neither does their upcoming successor, Windows Terminal (at least as of this writing).[1]
In recent versions of Windows 10, support for VT sequences is enabled by default in both Windows PowerShell and PowerShell Core.
However, Write-Host has no support for them, so you must embed the escape sequences directly into the strings you pass to Write-Host (or strings you send to the success output stream, if it goes to the console):
Note:
I'm omitting Write-Host from the examples below, because it isn't strictly necessary, but colored text generally should indeed be written to the display (host), not to the success output stream.
While it is better to consistently use VT sequences for all formatting needs - including colors - it is possible to combine them with Write-Host -ForegroundColor /-BackgroundColor`.
PowerShell Core:
PowerShell Core supports embedding escape sequences directly in "..." (double-quoted strings), via the `e escape sequence, which expands to a literal ESC character, which is the character that initiates a VT escape sequence.
"To `e[1mboldly`e[m go ..., in `e[36mcyan`e[m."
Windows PowerShell:
There's no support for `e; the easiest solution is to use \e as placeholders and use -replace to substitute actual ESC characters (Unicode code point 0x1b) for them:
"To \e[1mboldly\e[m go ..., in \e[36mcyan\e[m." -replace '\\e', [char] 0x1b
[1] From PowerShell Core, you can run the following test command to see if the word italics prints in italics: "`e[3mitalics`e[m after"
Note that italics in the terminal are supported on macOS and at least in some Linux distros; e.g., Ubuntu 18.04

Vim Command Mapping "Copy to Clipboard" Not Working

I recently began using Vim as my primary editor instead of programs like Atom/VSCode. I added a number of leader mappings to simplify tasks I do quite often but I'm having trouble with a few of them.
In Visual mode, I would like to be able to press <Space>y to copy the current selection to the clipboard (+ register). I've verified that I can do this manually by entering visual mode, selecting the text I want, and pressing "+y. However, my mapping doesn't seem to work:
vmap <Leader>y "+y
I set my leader the following way:
map <Space> <Leader>
I do it this way so that when showcmd is set, I get a visual indicator in operator-pending mode. By looking at that indicator, I can tell that when I press <Space>, I do enter operator pending mode on the \ key as expected. Then, when I press y, I am no longer in operator pending mode, but I am still in visual mode and haven't yanked the selection to the register.
To make sure there wasn't a plugin colliding with my mapping, I backed up my .vimrc and replaced it with one that only has the following contents:
set showcmd
map <Space> <Leader>
vmap <Leader>y "+y
Does one of these keys need to be escaped? Or am I doing something else wrong?
(I'm currently running Ubuntu Bash on Windows. Vim is version 7.4)
For reference, I got the idea from this article (And use the exact same command):
https://sheerun.net/2014/03/21/how-to-boost-your-vim-productivity/
Thankfully, the fix is pretty simple.
map <Space> <Leader>
is incorrect. The right way is
let mapleader=" "
From :help mapleader
*<Leader>* *mapleader*
To define a mapping which uses the "mapleader" variable, the special string
"<Leader>" can be used. It is replaced with the string value of "mapleader".
If "mapleader" is not set or empty, a backslash is used instead. Example: >
:map <Leader>A oanother line<Esc>
Works like: >
:map \A oanother line<Esc>
But after: >
:let mapleader = ","
It works like: >
:map ,A oanother line<Esc>
Note that the value of "mapleader" is used at the moment the mapping is
defined. Changing "mapleader" after that has no effect for already defined
mappings.
How to define the "leader" key is explained under :help mapleader. If you want to use <Space> as "leader" you are supposed to do:
let mapleader = "\<Space>"
Note that the "leader" key is not a special key at all. With <Space> as "leader", the two mappings below are strictly equivalent:
vmap <leader>y "+y
vmap <Space>y "+y

Bash keybinding with "bind" - avoid recursion like noremap in Vim

I've remapped the following keys in Bash:
bind '"a" "b"'
bind '"b" "c"'
If I press a or b both times a c will be printed.
How can I map the keys so that by pressing a and b will be printed and only by pressing a and c will be printed (like with Vims **nore**-map)?
You can make a char in the right part a literal with a preceeding "^V":
bind '"x":"^Vx "'
The key sequence to enter the text after colon is:
" Ctrl-v Ctrl-v x space "
In bash "Ctrl-v x" results in a literal x, without key-mapping interpretation .
You may find it useful to build your own keymapping file which maps the numeric signal you get from the keyboard to a character. This is done with the loadkeys command i.e. if you're switching to the dvorak layout you can do (this is a verbose way to show you the location of the map files:
loadkeys /usr/share/keymaps/i386/dvorak/dvorak.map.gz
You could copy the map file for the layout you use and change the relavent chars, and load your modified mapping. I think loadkeys only affects the command line, though there are similar methods for changing the behaviour in X I believe.

How would you do a cut and paste with this in VIM?

Say you had this text:
SOMETHING_XXXXXXXXXXXXXX_ELSE
SOMETHING_XXXXXXXXXXXXXX_ELSE2
SOMETHING_XXXXXXXXXXXXXX_ELSE3
SOMETHING_XXXXXXXXXXXXXX_ELSE4
And you wanted to replace all XXX..XXX with this word:
HELLOWORLD
If I go into visual mode, then yank the word, how could I then replace the XXX..XXX in the 4 lines above using cut and paste?
If I try, what happens is the X gets into my 'clipboard' and then I'm stuck to just typing it out manually.
I'm not sure if it will work in viemu, but in VIM you can do the following...
Using Yank and Paste
Yank the text to a specific register. Select the text in visual mode and use the command "ay to yank the text to the register a. Then when pasting call the command "ap, which pastes the contents of the a register.
Using Normal Command
But I would strongly prefer to use the normal command. Just select the lines
SOMETHING_XXXXXXXXXXXXXX_ELSE
SOMETHING_XXXXXXXXXXXXXX_ELSE2
SOMETHING_XXXXXXXXXXXXXX_ELSE3
SOMETHING_XXXXXXXXXXXXXX_ELSE4
using line visual mode (<C-v>) and then issue this command: :'<,'>normal fXct_HELLOWORLD. Then you'll have
SOMETHING_HELLOWORLD_ELSE
SOMETHING_HELLOWORLD_ELSE2
SOMETHING_HELLOWORLD_ELSE3
SOMETHING_HELLOWORLD_ELSE4
This means that it will run the command fXct_HELLOWORLD for each line. Let me explain the command:
fX - moves the cursor until the first X;
ct_ - deletes everything untill _ and puts you in insert mode;
HELLOWORLD - the word which will substitute XXXXXXXXXXXXXX;
One way would be to visually select all the code you want to replace and change it at once
Ctrl+v 3jt_cHELLOWORLD[Esc]
Note: it takes a couple of seconds for all lines to be updated
Another way to be by creating a macro:
record macro:
q10fXct_HELLOWORLD[esc]q
run macro on other lines:
j#1j#1j#1
q1 records a macro on character 1
#1 replays macro
But search and replace is a good alternative for your question
Highlight the four lines in visual mode, then
:'<,'>s/X\+/HELLOWORLD/g
Via this question: How do I use vim registers? I found ^R in command mode will paste from a register.
For example, with XXXX highlighted then yanked into the " register:
:s/^R"/HELLOWORLD/g

How does one align code (braces, parens etc) in vi?

How do you prettify / align / format code in vi? What is the command?
I have pasted in a hunk of code and I need to have it all formatted/aligned... obviously I am a vi neophyte.
x
These commands in my answer work in vim. Most people who think they're using vi are using vim. To find out if your 'vi' is really 'vim', open vi and type :version -- if it's vim, it will say so. Otherwise you might just see a version number without the name of the program. Also, when you open vim for the first time you will usually see a splash screen of some sort that says "VIM - VI iMproved"...
Automatic Indentation
To turn auto-indentation on, make sure vim knows the file type you're editing (it usually automatically detects this from the file name extension, but might not figure it out with some file types). You can tell it the filetype using the menus for syntax highlighting. Then, do this:
:filetype indent on
You can disable auto-indentation with
:filetype indent off
Automatically adjusting/correcting indentation
In general, ={motion} will align code to an indentation level.
== align the current line
=i{ align the inner block
=% align to the matching parenthesis/bracket under the cursor
=14j or 14== align the next 14 lines
=G align to the end of the file
vG= same thing, align to the end of the
file (but using visual mode)
vjjj= align four lines (using visual mode)
Manual indentation
If vim is not guessing the indentation level correctly, there are two ways to change it:
If you are in normal mode (where everything is a command), do << to shift a line left, or >> to shift it right by one tab. You can do this with several lines by using the same movement commands I showed above (eg, >i{ indents the current inner code block).
If you are in insert mode, you can indent the line further (without moving the cursor) by doing a Ctrl-T, or un-indent one tab with Ctrl-D
Aligning equals signs, etc
If you want to align equals signs in a list of declarations, you should consider using this vim script: http://www.vim.org/scripts/script.php?script_id=294
Adjusting indentation/tab sizes
If you want vim to use spaces instead of tabs when it indents, run this command (or consider adding it to your vimrc file)
:set expandtab
To set how many spaces equal a tab, I usually do this:
:set expandtab softtabstop=3 tabstop=3 shiftwidth=3
tabstop - how many columns a tab counts for (affects display of existing tab characters)
shiftwidth - controls reindentation size with << and >>, among other commands.
softtabstop - how much space to insert when you press the tab key
expandtab - expand tab keys to spaces
But if you have to work with different amounts of tabs a lot, you could also use this function and keybinding:
function! Ktabs(tabsize)
execute "set softtabstop=" . a:tabsize . " tabstop=" . a:tabsize . " expandtab shiftwidth=" . a:tabsize
"set softtabstop=a:tabsize tabstop=a:tabsize expandtab shiftwidth=a:tabsize
endfunction
noremap <leader><Tab> :call Ktabs(3)<Left>
If you are editing a file with a mix of tabs and spaces, you may want to use this command after setting tab size:
:retab
={motion}
:h =
P.S. You shouldn't use vi if vim is available.
If manually adjusting indents I will open a visual block with V on the first or last line I want to re-indent, move to the brace containing the block, goto the other brace with % then shift the line with > or <
If indents are off by a lot I will shift everything all the way left with < and repeat it with . and then re-indent everything.
Another solution is to use the unix fmt command as described in Your problem with Vim is that you don't grok vi., {!}fmt

Resources