How to use abbrev in MacVim - macos

I'm new to mvim and I'm not sure how to use commands that have been saved in my vimrc file like the following:
"Load the current buffer in Chrome - Mac specific.
abbrev chrome :! open -a Google\ Chrome %:p<cr>
(note: This was originally:
"Load the current buffer in Chrome - Mac specific.
abbrev ff :! open -a FireFox.app %:p<cr>
)
I'm just confused as how this abbrev works all together and couldn't find an answer on google :\ Maybe someone here could help?

These lines create an abbreviation for a commonly used command. In this case, it is creating an abbreviation for opening the current buffer in a particular application. For instance if you were working on a file called index.html and you entered the command :chrome in vim, it would open index.html in Chrome.
For the three major Mac browsers, use the following lines in your .vimrc file:
" Open the current buffer in a web browser
abbrev ff :!open -a Firefox.app %:p
abbrev chrome :!open -a Google\ Chrome.app %:p
abbrev sf :!open -a Safari.app %:p
This works because the :! at the beginning of each abbreviation definition tells vim to execute a shell command. In these cases, the shell command is open -a [WHICHEVER_BROSWER].app using the current buffer %:p.
Here is some further reading:
Vim Wiki article on Abbreviations
Blog Post about Opening Buffers in External Apps

While using abbrev works in this scenario it's normally used for saving typing or fixing common spelling mistake:
iabbrev mmm myultRalonganDrandomlycapiTAlizedemailadDress#example.com
iabbrev teh the
If you want to execute a command, I think normal mappings are conceptually better suited. Here is what I have in my ~/.vimrc:
let os=substitute(system('uname'), "\n", "", "")
if os == "Darwin" || os == "Mac"
nnoremap <F12>f :exe ':silent !open -a firefox %'<CR>
nnoremap <F12>c :exe ':silent !open -a "google chrome" %'<CR>
nnoremap <F12>o :exe ':silent !open -a opera %'<CR>
nnoremap <F12>s :exe ':silent !open -a safari %'<CR>
elseif os == "Linux"
nnoremap <F12>f :exe ':silent !firefox %'<CR>
nnoremap <F12>c :exe ':silent !chromium-browser %'<CR>
nnoremap <F12>o :exe ':silent !opera %'<CR>
endif

Related

How to launch textadept on macos x in curses mode

I studied all the manual about Textadept but still don't understand how from a command line in Terminal start Textadept in terminal aka curses mode.
I wish it be something like:
> textadept -g(ui) somefile.txt
starts editor in GUI mode
> textadept -c(onsole) somefile.txt
starts editor in curses mode
One way is to create an alias somewhere in a directory that is on your path, for example ~/.local/bin:
ln -s /Users/{your usename}/Applications/Textadept.app/Contents/MacOS/textadept-curses textadept-c
(Assuming you have Textadept installed in ~/Applications
Then you can launch it as
textadept-c somefile.txt

Ctrl-h mapping in Neovim's terminal emulator

I've enabled the following mappings in my init.vim:
tnoremap <Esc> <C-\><C-n>
tnoremap <C-h> <C-\><C-n><C-w>h
tnoremap <C-j> <C-\><C-n><C-w>j
tnoremap <C-k> <C-\><C-n><C-w>k
tnoremap <C-l> <C-\><C-n><C-w>l
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
These greatly improve windows navigation in Vim.
However, I've noticed that C-h does not work as expected when executed in terminal buffer. Trying this on a usual terminal session results in Backspace action. So probably it seems to be one of these alternative key combinations, like C-i for Tab or C-[ for Esc. But is there any way to make C-h work in Neovim's terminal session as per my bindings?
Thanks!
This issue has already been extensively debated here. Original Vim does not rely on terminfo and includes its own patch for correct handling of C-h sequences. Neovim does look at terminfo though.
Briefly, the fix is executing these commands in the shell:
infocmp $TERM | sed 's/kbs=^[hH]/kbs=\\177/' > $TERM.ti
tic $TERM.ti

Get vim to pass the current file path to my shell

I already have this in my vimrc:
set shell=powershell
set shellcmdflag=-command
Now I want to pass powershell the path of the current file so it can run it. I tried this:
nnoremap <C-q> :! expand('%:p')<cr>
And here is what I see in powershell:
powershell -command " expand('\\path\to\my\file....
Obviously this doesn't work because I've that darn expand() in there. How can I pass just the full path of the current file to my shell?
Also, before anyone says something I have my own mapping I use for visual block which is why I'm re-mapping <C-q>
The special characters like % (:help cmdline-special) are automatically expanded by Vim "at places where a file name can be used" (to quote from the help). This includes the :! command, so the following is enough:
nnoremap <C-q> :! %:p<CR>
Now, if you wanted to do more complex mangling than the :p, there would be two options:
Using :execute: nnoremap <C-q> :execute '!' expand('%:p')<CR>
Using <C-R> with the expression register: nnoremap <C-q> :! <C-r>=expand('%:p')<CR><CR>
How about just using %:p
nnoremap <C-q> :!%:p<CR>

How to start "emacsformacosx" in terminal

I am using MAC OX 10.6 , and install the emacs from here http://emacsformacosx.com/
I want to know how to start it in terminal, so my ecb can open current directory
It is actually quite easy, just run it from terminal like this:
/Applications/Emacs.app/Contents/MacOS/Emacs -nw
the -nw option means to start emacs without the gui frame.
You can put the following in your shell (on my mac .zshenv) :
alias Emacs="/Applications/Emacs.app/Contents/MacOS/Emacs -nw"
Then I just have two commands:
Emacs : for emacs version 24
emacs : for the apple version of emacs
Of course you can just alias the Emacs.app to emacs, but this allows me to customize the two differently - for instance Emacs 24 allows me to use list-packages and so forth. emacs 22 ignores most of this, so I can always revert to a 'bare metal' emacs if need be. Your usage may vary, but if you don't remember the arguments to emacs you can find them by doing this:
emacs --help
Some interesting ones:
Emacs.app --fullscreen
Emacs.app --line-spacing
Emacs.app --vertical-scroll-bars
More info here : http://www.gnu.org/software/emacs/manual/html_node/emacs/Option-Index.html#Option-Index
The answer from #Toymakerii is a good one, but you might also consider adding:
export PATH=/Applications/Emacs.app/Contents/MacOS/bin:$PATH
This way, you can use emacsclient to open files in an already-running Emacs instance:
emacsclient -t SOMEFILE # Open SOMEFILE in a terminal frame
emacsclient -c SOMEFILE # Open SOMEFILE in a new graphical frame
Depending on your Emacs version, you might need to put the following in your ~/.emacs.d/init.el (or ~/.emacs, if you're old-fashioned):
(require 'server)
(unless (server-running-p)
(server-start))
In my ~/.profile i have the following:
function emacs
{
if [ -e "$#" ]
then
command open -a emacs "${#}"
else
touch "$#"
command open -a emacs "${#}"
fi
}
(The reason for having a function is to make it also work when the specified file does not yet exist when emacs is started)
By default terminal will open /usr/bin/emacs on OS X.
You can change this behavior by changing what the "emacs" command will do. Open up ~/.profile and type the following:
alias emacs=open /Applications/Emacs.app
The next time you open a prompt this change will be active. (or you can run "source ~/.profile")
The easiest is to simply do
open /Applications/Emacs.app --args foo
An alias would then be
alias emacs=open /Applications/Emacs.app --args "${#}"
or in csh/tcsh
alias emacs 'open /Applications/Emacs.app --args $1'
edit: this seems to need a full path to open the correct file... I don't know if this is a problem with Emacs.app or with tcsh

Mac terminal Vim will only use backspace when at the end of a line

I seem to have something odd with either my Mac 10.6 terminal or my .vimrc.
When I type backspace on my laptop's keyboard, it only works when the cursor is at the end of the line. Trying to delete from within a line does nothing. MacVim operates normally. Google hasn't helped because I can't even figure out what to call this behavior.
All other backspace commands in my Terminal work as expected, so I am leaning towards it being Vim specific.
Here's the output of my ~/.vimrc 's mappings, I can't see anything that would make Vim in the terminal operate this way:
cflewis#coral-reef ~> egrep ".*map.*" ~/.vimrc
"inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
let mapleader = ","
map Q gq
nmap <silent> <leader>s :set nolist!<CR>
" extended '%' mapping for if/then/else/end etc
map <S-Insert> <MiddleMouse>
map! <S-Insert> <MiddleMouse>
nmap <silent> <C-N> :silent noh<CR>
nmap <C-E> :b#<CR>
nmap <C-P> :NERDTreeToggle<CR>
nmap <leader>p :NERDTreeFind<CR>
nmap <leader>/ :call NERDComment(0, "invert")<cr>
vmap <leader>/ :call NERDComment(0, "invert")<cr>
nmap <leader>t :TlistToggle<CR>
nmap <leader>e :e **/
nmap <Leader>b :MiniBufExplorer<cr>
nmap <Leader>sh :ConqueSplit bash<cr>
nmap <Leader>r :ConqueSplit
" map ,y to show the yankring
nmap <leader>y :YRShow<cr>
imap <silent> <Down> <C-o>gj
imap <silent> <Up> <C-o>gk
nmap <silent> <Down> gj
nmap <silent> <Up> gk
cmap w!! %!sudo tee > /dev/null %
inoremap jj <Esc>
nnoremap JJJJ <Nop>
Any ideas would be appreciated. I tried flipping the delete key to send ^H or ^?, to no difference.
Most likely, the "problem" you're seeing is that you can't delete anything that was not typed during your current insert mode session. This is due to the default setting for the 'backspace' option. Adding set backspace=indent,eol,start to your ~/.vimrc is the behavior that you probably want.
This is the only explicit backspace mapping I have in my config. I do not know if it will help for your problem, but it might be worth a try?
" allow backspacing over everything in insert mode
set backspace=indent,eol,start

Resources