Preventing redefining existing buffer mapping - keyboard-shortcuts

I want to prevent *noremap command from remapping an existing sequence, but only if this sequence is local to buffer:
noremap a b
" Will fail, must succeed
noremap <buffer> <unique> a c
noremap <buffer> a b
" Will fail, OK
noremap <unique> <buffer> a c
noremap a b
noremap <buffer> a c
" Will fail, OK
noremap <unique> <buffer> a d

With newer vim one can use maparg() with fourth argument:
let oldmap=maparg('a', '', 0, 1)
if empty(oldmap) || !oldmap.buffer
noremap <buffer> a c
endif
or, better (also supports older vim), but requires frawor:
execute frawor#Setup('0.0', {'#/mappings': '0.0'})
let oldmap=s:_r.map.maparg('a', 'n', 0)
if empty(oldmap) || !oldmap.buffer
noremap <buffer> a c
endif

Related

NSIS - Variable is not resolving properly

I'm trying to check the version of folder with !define variable from compiler.nsh. On mismatch of version, I'm trying to update compiler.nsh with ReplaceInFile macro. But the Filepath I have provided on the ReplaceInFile macro is not resolving variable ${CURRENT_KIT}. Am I missing something? Please help
;***************************************************************************************
; Version Check
;***************************************************************************************
Var /GLOBAL CURRENT_KIT
Var /GLOBAL CURRENT_VER
;Function CompilerFile
Section
System::Call "kernel32::GetCurrentDirectory(i ${NSIS_MAX_STRLEN}, t .r0)"
${WordFind} "$0" "\" "-1" "$CURRENT_KIT"
${CharStrip} "${APP_CLASSKIT}" "$CURRENT_KIT" "$CURRENT_VER"
Push "$CURRENT_VER"
Push "${CHK_VERSION}"
Call VersionCheck
Pop $0
${If} $0 == 0
MessageBox MB_OK "Kit Version matches compiler.nsh"
!define APP_VERSION "${CHK_VERSION}"
${ElseIf} $0 == 1
!insertmacro _ReplaceInFile **"${NETWORK_PATH}\${CURRENT_KIT}\compiler.nsh"** '!define CHK_VERSION "${CHK_VERSION}"' '!define APP_VERSION "$CURRENT_VER"'
${EndIf}
I don't really know what you are trying to do but if I am guessing the variable can not be resolved this way.
Rather try this syntax for resolving the variables in a string:
!insertmacro _ReplaceInFile **"${NETWORK_PATH}\$CURRENT_KIT\compiler.nsh"** '!define CHK_VERSION "${CHK_VERSION}"' '!define APP_VERSION "$CURRENT_VER"'
You don't need the extra curly brackets for these kind of variables.
Let me know if I helped you with your issue.

What improvements can I do to my .vimrc to improve my experience in NeoVim?

Here's my .vimrc
call plug#begin('~/.vim/plugged')
Plug 'dart-lang/dart-vim-plugin'
Plug 'natebosch/vim-lsc'
Plug 'natebosch/vim-lsc-dart'
Plug 'tpope/vim-sensible'
Plug 'vim-airline/vim-airline'
Plug 'w0rp/ale'
Plug 'pearofducks/ansible-vim', { 'do': './UltiSnips/generate.sh' }
Plug 'ncm2/ncm'
Plug 'roxma/nvim-yarp'
Plug 'ncm2/ncm2-bufword'
Plug 'ncm2/ncm2-path'
Plug 'iamcco/markdown-preview.nvim', { 'do': 'cd app && yarn install' }
Plug 'codota/tabnine-vim'
Plug 'Chiel92/vim-autoformat'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'jiangmiao/auto-pairs'
Plug 'autozimu/LanguageClient-neovim', {
\ 'branch': 'next',
\ 'do': 'bash install.sh',
\}
" Plug 'valloric/youcompleteme'
let g:lsc_server_commands = {'dart': 'dart_language_server'}
call plug#end()
let g:ale_fixers = {
\ 'javascript': ['eslint']
\}
let g:ale_sign_error = '❌'
let g:ale_sign_warning = '⚠️'
let g:lsc_enable_autocomplete = v:true
let g:lsc_auto_map = v:true
let g:ale_fix_on_save = 1
let g:ale_javascript_prettier_use_local_config = 1
let g:ale_javascript_eslint_options = '-c ~/.eslintrc'
let g:ale_lint_on_enter = 0
let g:ansible_unindent_after_newline = 1
autocmd BufEnter * call ncm2#enable_for_buffer()
set completeopt=noinsert,menuone,noselect
" set softtabstop=2
" set shiftwidth=2
" set expandtab
set expandtab
" show existing tab with 2 spaces width
set tabstop=2
set softtabstop=2
" when indenting with '>', use 2 spaces width
set shiftwidth=2
nnoremap <A-Down> :m .+1<CR>==
nnoremap <A-Up> :m .-2<CR>==
inoremap <A-Down> <Esc>:m .+1<CR>==gi
inoremap <A-Up> <Esc>:m .-2<CR>==gi
vnoremap <A-Down> :m '>+1<CR>gv=gv
vnoremap <A-Up> :m '<-2<CR>gv=gv
set statusline=%<%f\ %h%m%r%{kite#statusline()}%=%-14.(%l,%c%V%)\ %P
set laststatus=2
let g:python3_host_prog='/home/thebozzkg/anaconda3/bin/python3'
au BufWrite * :Autoformat
let g:airline#extensions#tabline#enabled = 1
let g:airline_detect_spell=1
let g:airline_left_sep = ''
let g:airline_left_alt_sep = ''
let g:airline_right_sep = ''
let g:airline_right_alt_sep = ''
let g:airline_symbols.branch = ''
let g:airline_symbols.readonly = ''
let g:airline_symbols.linenr = '☰'
let g:airline_symbols.maxlinenr = ''
let g:airline_symbols.dirty='⚡'
nnoremap <ALT-Left> :tabprevious<CR>
nnoremap <ALT-Right> :tabnext<CR>
Some notes about me:
I don't use hjkl I use the arrow keys (pls don't insult me)
I write Javascript, Dart, Markdown, YAML, JSON, C, & C++ (so far) in neovim (I'm switching from VSCode)
Most of the stuff I have I literally copied from GitHub and some Vim wikis
I have Anaconda installed
I'm using Ubuntu Linux 20.04 (if that matters)
For some reason, I don't know which plugins are doing it but my autocompletion keeps messing up my files (editing YAML files and saving them are absolute torture, some indentation is lost with almost all files) and also crashing (TabNine)
Can someone help me with this mess?
Welcome to Vim!
I think most new vim users have been there. I certainly have! I wanted a
'vim as python IDE' and copied a whole bunch of stuff from every blog under the
sun into my vimrc almost immediately after installing vim.
After some time spent fighting with all the settings, plugins and remaps I
didn't understand, I decided to go through my vimrc, line by line and comment
out anything I didn't understand (nearly all of it).
Then I used this more minimal vim for a while and whenever I decided I had a
need for a certain feature, I checked the largely commented vimrc for anything
that looked related, and/or googled for that particular feature only. Often
you find that there is a built in method to do it with the core vim commands,
and if not, then there are a lot of solutions for the problem (and often, you
find that there is an even more powerful way that didn't occur to you - these
are good days).
But the key is to not try and coerce vim into a huge IDE overnight! Let it
happen gradually and things will make more sense, and you'll end up with a
vimrc that you understand and therefore be in a position to add to it and tweak
it.
The last thing I'll say is to recommend the following books:
'Learn VimScript the Hard Way' by Steve Losh
'Practical Vim' by Drew Neil, and his accompanying screencast series.
(Also there is The Primagean who does high quality youtube tutorials)
Having said all that, and acknowledging that an objective answer can't be given
for your question, here is a minimal vimrc which has a few plugins and settings
that do simple but very useful things (but do read up on them to understand how
they work!):
"==== PLUGINS =================================================================
set nocompatible " don't try to be compatible with Vi
filetype plugin indent on " use default plugins
call plug#begin('~/.vim/plugged')
" plugins I would put in a new vimrc
Plug 'tpope/vim-sensible'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-commentary'
Plug 'vim-scripts/ReplaceWithRegister'
Plug 'flazz/vim-colorschemes'
"==== CUSTOM CONFIGURATIONS ===================================================
"- general settings -----------------------------------------------------------
set encoding=utf-8
set linebreak " wrap long lines at char in 'breakat' (default " ^I!#*-+;:,./?")
set nowrap " don't wrap lines by default
set expandtab " expand tabs into spaces
set tabstop=4 " a tab is the same as 4 spaces
set softtabstop=4 " when I hit <tab> in insert mode, put 4 spaces
set shiftwidth=4 " when auto-indenting, use 4 spaces per tab
set autoindent " when creating a new line, copy indent from line above
set incsearch " show matches for patterns while they are being typed
set smartcase " with both on, searches with no capitals are case insensitive...
set ignorecase " ...while searches with capital characters are case sensitive.
set spell spelllang=en
set nospell " don't hightlight misspellings unles I say so
set lazyredraw " don't redraw screen during macros (let them complete faster)
set t_Co=256 " use full colours
syntax enable " highlight special words to aid readability
colorscheme zenburn
" THIS NEXT BIT I DEBATED INCLUDING - IT'S MY PERSONAL PREFERENCE AND BY
NO MEANS 'NECESSARY' AND COULD SAFELY BE EXCLUDED... BUT I JUST HATED THE
BLOCK CURSOR IN INSERT MODE SO MUCH...
"- cursor behaviour (make cursor blink for insert mode, block for normal mode)
augroup cursor_behaviour
autocmd!
" reset cursor on start:
autocmd VimEnter * silent !echo -ne "\e[2 q"
" cursor blinking bar on insert mode
let &t_SI = "\e[5 q"
" cursor steady block on command mode
let &t_EI = "\e[2 q"
" highlight current line when in insert mode
autocmd InsertEnter * set cursorline
" turn off current line hightlighting when leaving insert mode
autocmd InsertLeave * set nocursorline
augroup END
Good luck!

extracting subtext between two characters using grep

I have text file which has information like follows
#Mp_chzt_1
asdjhsadhasdhdbjashdjaudashdjashdasdhasdhasdh
asdasdkasjdkaskdskadkasdkasdkjaskldasdklasdas
ahsjdasdfdfsdhghrtuztiuiuzozuoiouiouiouiouiou
asjkjieqjeroiweoriksfjksjksjkf
+
!!!#!!!!!!!!++??????????????~~~~~~~~~~~~~
BBBBBBBBBBBBMMMMMM!!!!!++LLLLLL******
#Mp_btrea_1
uokjjkzghqawsdasduihdlöklöaklöskdlkaökgzgzggz
asdasduzuqwtzeqweuvixcvdjfiisduiifuzwpqüqwoeü
kjkjiuijwiqquzwuziziqz
+
**********||||||||||||#########++++?????????
MMMMMMMMMUUUU***+++~~~~~~~~~~~~~~~~~~~~~~~~~~
#Mp_trwe_3
jhtrqhkjiqkjkqwjelasjjljiewkjkljkldjflsjljki8u
immhgwqtzopirpjgbsdkfjieipwippieoroeirkvsdjjfk
jkahdjhjhfuhjkwekksjakjeiuwiurweiurioweuroweod
poplrtm,ernmjhazqweqwjidiipfiopdifosidpfppsdif
mnasnbdhgqweqweipoipoxkajksdökalsklsaksldkasöd
asdas
+
!!!!!!!!!!!!!!!!!!#####???????????????????
I would like extract the region only between #Mp_* and + that comes right below the text and export it to txt file like following
#Mp_chzt_1
asdjhsadhasdhdbjashdjaudashdjashdasdhasdhasdh
asdasdkasjdkaskdskadkasdkasdkjaskldasdklasdas
ahsjdasdfdfsdhghrtuztiuiuzozuoiouiouiouiouiou
asjkjieqjeroiweoriksfjksjksjkf
#Mp_btrea_1
uokjjkzghqawsdasduihdlöklöaklöskdlkaökgzgzggz
asdasduzuqwtzeqweuvixcvdjfiisduiifuzwpqüqwoeü
kjkjiuijwiqquzwuziziqz
#Mp_trwe_3
jhtrqhkjiqkjkqwjelasjjljiewkjkljkldjflsjljki8u
immhgwqtzopirpjgbsdkfjieipwippieoroeirkvsdjjfk
jkahdjhjhfuhjkwekksjakjeiuwiurweiurioweuroweod
poplrtm,ernmjhazqweqwjidiipfiopdifosidpfppsdif
mnasnbdhgqweqweipoipoxkajksdökalsklsaksldkasöd
asdas
When I used the following code
grep -o -P '(?<=#MP.*).*(?=+)' query.txt > output.txt
It gave me "grep: nothing to repeat".
Could anyone guide where my mistake is and how to rectify it.
Thanks in advance.
Better use awk for this:
awk '/^#/{f=1} /^+/ {f=0} f' file > output.txt
Or, if you have leading spaces, match them with \s*:
awk '/^\s*#/{f=1} /^\s*\+/ {f=0} f' file > output.txt
This uses a flag f to decide whether the line should be printed or not.
When it sees a line starting with #, it activates it.
When it sees a line starting with +, it deactivates it.
Then, it evaluates the flag and prints if it is True.
With your given input it returns:
#Mp_chzt_1
asdjhsadhasdhdbjashdjaudashdjashdasdhasdhasdh
asdasdkasjdkaskdskadkasdkasdkjaskldasdklasdas
ahsjdasdfdfsdhghrtuztiuiuzozuoiouiouiouiouiou
asjkjieqjeroiweoriksfjksjksjkf
#Mp_btrea_1
uokjjkzghqawsdasduihdlöklöaklöskdlkaökgzgzggz
asdasduzuqwtzeqweuvixcvdjfiisduiifuzwpqüqwoeü
kjkjiuijwiqquzwuziziqz
#Mp_trwe_3
jhtrqhkjiqkjkqwjelasjjljiewkjkljkldjflsjljki8u
immhgwqtzopirpjgbsdkfjieipwippieoroeirkvsdjjfk
jkahdjhjhfuhjkwekksjakjeiuwiurweiurioweuroweod
poplrtm,ernmjhazqweqwjidiipfiopdifosidpfppsdif
mnasnbdhgqweqweipoipoxkajksdökalsklsaksldkasöd
asdas

Vim - How do I show a helpful error message when using the arrow keys in insert mode?

I'm trying to stop using the arrow keys in vim. But instead of setting them to <nop>, as other people do, I want them to show an error message. This does the thing in normal mode:
noremap <up> :echoerr 'USE K TO GO UP'<CR>
noremap <down> :echoerr 'USE J TO GO DOWN'<CR>
noremap <left> :echoerr 'USE H TO GO LEFT'<CR>
noremap <right> :echoerr 'USE L TO GO RIGHT'<CR>
I tried to do the same in insert mode, but it didn't work:
inoremap <up> :echoerr 'USE K TO GO UP'<CR>
inoremap <down> :echoerr 'USE J TO GO DOWN'<CR>
inoremap <left> :echoerr 'USE H TO GO LEFT'<CR>
inoremap <right> :echoerr 'USE L TO GO RIGHT'<CR>
This instead prints :echoerr 'USE K TO GO UP' where my cursor is when I press up in insert mode.
How can I fix the inoremap directives so this works as I expected? (also, an explanation of why it doesn't work would help).
EDIT: Thanks to all the people that helped me. I ende up using this:
noremap <up> :echom 'USE K TO GO UP'<CR>
noremap <down> :echom 'USE J TO GO DOWN'<CR>
noremap <left> :echom 'USE H TO GO LEFT'<CR>
noremap <right> :echom 'USE L TO GO RIGHT'<CR>
inoremap <up> <ESC>:echom 'USE K TO GO UP'<CR>
inoremap <down> <ESC>:echom 'USE J TO GO DOWN'<CR>
inoremap <right> <ESC>:echom 'USE L TO GO RIGHT'<CR>
inoremap <left> <ESC>:echom 'USE H TO GO LEFT'<CR>
The vim-autoclose plugin was giving me trouble with the up/down keys on edit mode. I just uninstalled the plugin.
:inoremap <up> <Esc>:echoerr 'MSG HERE'<CR>
Rather then using <Esc>:echoe 'message'<CR> I would suggest <C-o>:echoe 'message'<CR> here: it won’t leave insert mode. <C-o>:throw 'message'<CR> to make it more annoying: you’ll get Press ENTER prompt, not just highlighted message:
inoremap <Up> <C-o>:throw 'USE “<Esc>k” TO GO UP'<CR>
. Note that I would also suggest using the equivalent echohl ErrorMsg | echomsg 'Message' | echohl NONE instead of :echoerr: though it is irrelevant here, but :echoe should be forgotten because it is highly context-dependent: unlike :echom which never breaks execution and :throw which always does this, :echoe breaks execution if it is inside the try block, and does not do so otherwise. That makes any construct like
SomeCommand
if error_cond
echoe 'abc'
endif
SomeCommand2
unstable: you never know whether SomeCommand2 will or won’t be executed if error_cond is true. Irrelevant here because there are no subsequent commands and it is unlikely to be run inside try block, but don’t make yourself think that :echoe may be a good choice for echoing errors.
You have more pressing issues than trying to use hjkl instead of the arrows.
You should read :help mapping, for example.
With inoremap <up> :echoerr 'USE K TO GO UP'<CR> you instruct Vim to imput :echoerr 'USE K TO GO UP'<CR> at the cursor when you press <Up>. It is an insert mode mapping, so, as long as you don't include <Esc> or <C-o> somewhere in the mapped sequence the whole thing will just be inserted as is.
In order to execute the sequence of commands you must first exit insert mode; hence the <Esc> in Eric's answer.

What are the best ways to automate a GDB debugging session?

Does GDB have a built in scripting mechanism, should I code up an expect script, or is there an even better solution out there?
I'll be sending the same sequence of commands every time and I'll be saving the output of each command to a file (most likely using GDB's built-in logging mechanism, unless someone has a better idea).
Basically, in this example I wanted to get some variable values in particular places of the code; and have them output until the program crashes. So here is first a little program which is guaranteed to crash in a few steps, test.c:
#include <stdio.h>
#include <stdlib.h>
int icount = 1; // default value
main(int argc, char *argv[])
{
int i;
if (argc == 2) {
icount = atoi(argv[1]);
}
i = icount;
while (i > -1) {
int b = 5 / i;
printf(" 5 / %d = %d \n", i, b );
i = i - 1;
}
printf("Finished\n");
return 0;
}
The only reason the program accepts command-line arguments is to be able to choose the number of steps before crashing - and to show that gdb ignores --args in batch mode. This I compile with:
gcc -g test.c -o test.exe
Then, I prepare the following script - the main trick here is to assign a command to each breakpoint, which will eventually continue (see also Automate gdb: show backtrace at every call to function puts). This script I call test.gdb:
# http://sourceware.org/gdb/wiki/FAQ: to disable the
# "---Type <return> to continue, or q <return> to quit---"
# in batch mode:
set width 0
set height 0
set verbose off
# at entry point - cmd1
b main
commands 1
print argc
continue
end
# printf line - cmd2
b test.c:17
commands 2
p i
p b
continue
end
# int b = line - cmd3
b test.c:16
commands 3
p i
p b
continue
end
# show arguments for program
show args
printf "Note, however: in batch mode, arguments will be ignored!\n"
# note: even if arguments are shown;
# must specify cmdline arg for "run"
# when running in batch mode! (then they are ignored)
# below, we specify command line argument "2":
run 2 # run
#start # alternative to run: runs to main, and stops
#continue
Note that, if you intend to use it in batch mode, you have to "start up" the script at the end, with run or start or something similar.
With this script in place, I can call gdb in batch mode - which will generate the following output in the terminal:
$ gdb --batch --command=test.gdb --args ./test.exe 5
Breakpoint 1 at 0x804844d: file test.c, line 10.
Breakpoint 2 at 0x8048485: file test.c, line 17.
Breakpoint 3 at 0x8048473: file test.c, line 16.
Argument list to give program being debugged when it is started is "5".
Note, however: in batch mode, arguments will be ignored!
Breakpoint 1, main (argc=2, argv=0xbffff424) at test.c:10
10 if (argc == 2) {
$1 = 2
Breakpoint 3, main (argc=2, argv=0xbffff424) at test.c:16
16 int b = 5 / i;
$2 = 2
$3 = 134513899
Breakpoint 2, main (argc=2, argv=0xbffff424) at test.c:17
17 printf(" 5 / %d = %d \n", i, b );
$4 = 2
$5 = 2
5 / 2 = 2
Breakpoint 3, main (argc=2, argv=0xbffff424) at test.c:16
16 int b = 5 / i;
$6 = 1
$7 = 2
Breakpoint 2, main (argc=2, argv=0xbffff424) at test.c:17
17 printf(" 5 / %d = %d \n", i, b );
$8 = 1
$9 = 5
5 / 1 = 5
Breakpoint 3, main (argc=2, argv=0xbffff424) at test.c:16
16 int b = 5 / i;
$10 = 0
$11 = 5
Program received signal SIGFPE, Arithmetic exception.
0x0804847d in main (argc=2, argv=0xbffff424) at test.c:16
16 int b = 5 / i;
Note that while we specify command line argument 5, the loop still spins only two times (as is the specification of run in the gdb script); if run didn't have any arguments, it spins only once (the default value of the program) confirming that --args ./test.exe 5 is ignored.
However, since now this is output in a single call, and without any user interaction, the command line output can easily be captured in a text file using bash redirection, say:
gdb --batch --command=test.gdb --args ./test.exe 5 > out.txt
There is also an example of using python for automating gdb in c - GDB auto stepping - automatic printout of lines, while free running?
gdb executes file .gdbinit after running.
So you can add your commands to this file and see if it is OK for you.
This is an example of .gdbinit in order to print backtrace for all f() calls:
set pagination off
set logging file gdb.txt
set logging on
file a.out
b f
commands
bt
continue
end
info breakpoints
r
set logging off
quit
If a -x with a file is too much for you, just use multiple -ex's.
This is an example to track a running program showing (and saving) the backtrace on crashes
sudo gdb -p "$(pidof my-app)" -batch \
-ex "set logging on" \
-ex continue \
-ex "bt full" \
-ex quit