Automatically restore last session in Gvim - session

I installed sessionman, and it works fine. But I lose session when reload the X session twice (logout/login, reboot, etc), because when KDE restores Gvim, it does not load session automatically, but only last file, and then saves this under the last session name on next reload. If I did not run SessionOpen then on next reload my last session is lost.
I configured session autosave already. It would be nice if Gvim can load last session automatically too. However, this feature does not work for me even manually. When I restart Gvim, SessionShowLast prints "Last session is undefined, current session is """.
The sessionman documentation says: "The name of an opened session is saved in g:LAST_SESSION variable which is saved in the viminfo file if 'viminfo' option contains '!'". But I have not found any clear explanation what is "viminfo option", where it should contain '!', and how do I set it. Also I'm not sure how to execute SessionOpenLast from vimrc.
If the way I'm trying to fix the problem is wrong then please correct me.

viminfo is a variable that describes what data should be stored in the viminfo file.
For full details, run :help 'viminfo' (note the quotes) in vim:
! When included, save and restore global variables that start
with an uppercase letter, and don't contain a lowercase
letter. Thus "KEEPTHIS and "K_L_M" are stored, but "KeepThis"
and "_K_L_M" are not. Nested List and Dict items may not be
read back correctly, you end up with a string representation
instead.
Use :set viminfo to see the current value of your viminfo setting. Modify it in your ~/.vimrc file.
set viminfo='100,<500,s10,h,!
Because vimrc is loaded before plugins, adding SessionOpenLast to vimrc will not work. To solve this, create an auto-command:
autocmd VimEnter * SessionOpenLast

"My Sessionman Conf
set viminfo='100,<500,s10,h,!
let sessionman_save_on_exit = 1
function! ReadSession()
SessionOpenLast
endfunction
" if no file args then open the last session
autocmd VimEnter * if argc() == 0 | call ReadSession() | endif

Related

Keep vim syntax file in same directory as documents

I am keeping notes in console vim on my laptop, and
I want to add syntax highlighting to my notes in order
to enhance them. However, I don't want to add a
million different filetypes for every area of knowledge
(for example my notes on compilers would have different
keywords than my notes on the FHS), and I also want to
make it easy to share these notes. After doing some
research, I discovered that I can get the behavior I
want, but it doesn't seem like a very elegant solution.
I added the following lines to my .vimrc:
if (filereadable("./.custom_syntax.vim")
let mysyntaxfile = "./.custom_syntax.vim"
syntax enable
else
syntax enable
endif
I don't really like this solution because it still
requires me to ask them to modify their .vimrc, but I
suspect that there's no way to do this without changing
anything on their system. Additionally, if I have any
files in the directory that aren't notes, vim will still
highlight them with the .custom_syntax.vim file because
I don't know what the filetype is.
Is there any better way to accomlish this?
Instead of using the old mysyntaxfile variable, I'd just :syntax enable (once) and then :source the syntax file. You can define an :autocmd that looks for an eponymous Vimscript file next to the original:
" Automatically source an eponymous <file>.vim or <file>.<ext>.vim if it exists, as a bulked-up
" modeline and to provide file-specific customizations.
function! s:AutoSource()
let l:testedScripts = [expand('<afile>') . '.vim']
if expand('<afile>:e') !=# 'vim'
" Don't source the edited Vimscript file itself.
call add(l:testedScripts, expand('<afile>:r') . '.vim')
endif
for l:filespec in l:testedScripts
if filereadable(l:filespec)
execute 'source' fnameescape(l:filespec)
endif
endfor
endfunction
augroup AutoSource
autocmd! BufNewFile,BufRead * call <SID>AutoSource()
augroup END
You do need this or something like this in your Vim configuration, though.

Determine OS X keyboard layout ("input source") in the terminal/a script?

I would like to determine the OS X keyboard layout (or "input source" as OS X calls it) from the terminal so that I can show it in places like the tmux status bar.
So I want to know if the current layout is "U.S." or "Swedish - Pro" for example.
Googling turns up nothing for me. Is this possible?
Note: #MarkSetchell deserves credit for coming up with the fundamental approach - where to [start to] look and what tools to use.
After further investigation and back and forth in the comments I thought I'd summarize the solution (as of OS X 10.9.1):
do shell script "defaults read ~/Library/Preferences/com.apple.HIToolbox.plist \\
AppleSelectedInputSources | \\
egrep -w 'KeyboardLayout Name' | sed -E 's/^.+ = \"?([^\"]+)\"?;$/\\1/'"
Note how \ is escaped as \\ for the benefit of AppleScript, which ensures that just \ reaches the shell. If you want to execute the same command directly from the shell (as one line), it would be:
defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources | egrep -w 'KeyboardLayout Name' |sed -E 's/^.+ = \"?([^\"]+)\"?;$/\1/'
The currently selected keyboard layout is stored in the user-level file ~/Library/Preferences/com.apple.HIToolbox.plist, top-level key AppleSelectedInputSources, subkey KeyboardLayout Name.
defaults read ensures that the current settings are read (sadly, as of OSX 10.9, the otherwise superior /usr/libexec/PlistBuddy sees only a cached version, which may be out of sync).
Since defaults read cannot return an individual key's value, the value of interest must be extracted via egrep and sed - one caveat there is that defaults read conditionally uses double quotes around key names and string values, depending on whether they are a single word (without punctuation) or not.
Update:
Turns out that AppleScript itself can parse property lists, but it's a bit like pulling teeth.
Also, incredibly, the potentially-not-fully-current-values problem also affects AppleScript's parsing.
Below is an AppleScript handler that gets the current keyboard layout; it uses a do shell script-based workaround to ensure that the plist file is current, but otherwise uses AppleScript's property-list features, via the Property List Suite of application System Events.
Note: Obviously, the above shell-based approach is much shorter in this case, but the code below demonstrates general techniques for working with property lists.
# Example call.
set activeKbdLayout to my getActiveKeyboardLayout() # ->, e.g., "U.S."
on getActiveKeyboardLayout()
# Surprisingly, using POSIX-style paths (even with '~') works with
# the `property list file` type.
set plistPath to "~/Library/Preferences/com.apple.HIToolbox.plist"
# !! First, ensure that the plist cache is flushed and that the
# !! *.plist file contains the current value; simply executing
# !! `default read` against the file - even with a dummy
# !! key - does that.
try
do shell script "defaults read " & plistPath & " dummy"
end try
tell application "System Events"
repeat with pli in property list items of ¬
property list item "AppleSelectedInputSources" of ¬
property list file plistPath
# Look for (first) entry with key "KeyboardLayout Name" and return
# its value.
# Note: Not all entries may have a 'KeyboardLayout Name' key,
# so we must ignore errors.
try
return value of property list item "KeyboardLayout Name" of pli
end try
end repeat
end tell
end getActiveKeyboardLayout
Recently I had written a small console utility (https://github.com/myshov/xkbswitch-macosx) on Objective-C to do this. It's a lot faster than a script based solutions. It can to get the current input layout but also it can to set the given input layout.
To get a current layout:
$xkbswitch -ge
> US
To set a given layout:
$xkbswith -se Russian
I am not sure of this answer, but it may be worth checking out. If you look in file:
/Library/Preferences/com.apple.HIToolbox.plist
there is a variable called
AppleCurrentKeyboardLayoutSourceID
and mine is set to "British" and I am in Britain...
You can read the file in a script with:
defaults read /Library/Preferences/com.apple.HIToolbox.plist AppleEnabledInputSources
sample output below:
(
{
InputSourceKind = "Keyboard Layout";
"KeyboardLayout ID" = 2;
"KeyboardLayout Name" = British;
}
)
So, I guess your question can be simply answered using this:
#!/bin/bash
defaults read /Library/Preferences/com.apple.HIToolbox.plist AppleEnabledInputSources | grep -sq Swedish
[[ $? -eq 0 ]] && echo Swedish
This question led to the creation of the keyboardSwitcher CLI Tool:
https://github.com/Lutzifer/keyboardSwitcher
Though similar to the already mentioned https://github.com/myshov/xkbswitch-macosx this has additional features, e.g. the list of Layouts is not hardcoded and thus can also support third party layouts (e.g. Logitech) and supports installation via homebrew.
Figured out how to do it with AppleScript, assuming you have the menu bar input menu.
Run this in a terminal:
osascript -e 'tell application "System Events" to tell process "SystemUIServer" to get the value of the first menu bar item of menu bar 1 whose description is "text input"'
Works fine even if you only show the input menu as flag icons, without the input source name.
Mavericks will probably prompt you to allow access, the first time. In earlier versions of OS X I suspect you'll need to turn on support for assistive devices in your accessibility preferences.
I was searching for an answer to an issue I was having with the keyboard layout that lead me to this post. I found the solution for my problem here.
Resolved Issues
You might experience difficulty logging into your account because the keyboard layout may change unexpectedly at the
Login window. (40821875)
Workaround: Log in to your account, launch Terminal, and execute the
following command:
sudo rm -rf /var/db/securityagent/Library/Preferences/com.apple.HIToolbox.plist
This is an Apple official release note for Mojave

How does .vimrc set its own filetype?

In response to this question on SuperUser, I wrote a small vimscript that will detect the filetype of a symbolic link and change the syntax highlighting:
au BufNewFile,BufRead * if &syntax == '' | silent! execute (':set filetype='.matchstr(resolve(#%),'.[^.]*$')[1:]) | endif
So if I open a symbolic link with no extension, it will look at the extension of the file it points to.
It works, but an unintended consequence is that now the syntax highlighting of my .vimrc file is gone. By default, my .vimrc file has vim syntax highlighting (:echo &syntax returns vim).
But when I add the above line, :echo &syntax returns vimrc (an invalid type).
I don't know why this is happening. Shouldn't &syntax=='' evaluate to false, and thus keep &syntax==vim? I suspect that my code is executing before the syntax highlighting is set to vim. But how (and when) exactly is the syntax highlighting set to vim for .vimrc? Additionally, how can I make my script behave the way it should?
Look in Vim's runtime area for filetype.vim. You can bring it up in vim with:
:e $VIMRUNTIME/filetype.vim
In my version, it looks like this line does the trick:
" Vim script
au BufNewFile,BufRead *vimrc* call s:StarSetf('vim')
Perhaps you want to put your autocmd in ~/.vim/after/filetype.vim. I believe this will cause it to be registered after the system ones, and then &syntax should be set up correctly.
jszakmeister's answer diagnoses the problem accurately: filetype.vim sets the filetype for vimrc.
An alternative solution though, to keep everything contained in in your .vimrc file, is to use:
au BufNewFile,BufReadPre * if &syntax == '' | silent! execute (':set filetype='.matchstr(resolve(#%),'.[^.]*$')[1:]) | endif
Note the change from BufRead to BufReadPre. The change causes this line to execute before filetype.vim. The syntax will be changed to vimrc, but then subsequently changed to vim. The upshot is that the script works as it should, but syntax highlighting for vimrc is preserved.
When the current file is ~/.vimrc, this part of your code
matchstr(resolve(#%),'.[^.]*$')
returns your file name: .vimrc.
I have no idea how you could imagine that /home/username/.vimrc would produce vim.
This is rather obviously a bad approach for the problem you tried to solve but there's no bug: you get exactly what you ask for. Using the filename/extension for filetype can't work reliably (js vs javascript, py vs python…).
You will probably need a filename/filetype list or many if/elseif if you want your code to do what you want.
See :h syntax-loading.

How to add the current session file name in the status line in Vim?

I recently added the sessionman plugin to my Vim configuration, and I like it so far.
I understand that Vim sets v:this_session to the session file name when a session is being used and I’d like to add it to my status line. Unfortunately, v:this_session contains the full file path and it is often way too long for it to fit in the status line.
So my question is: How can I extract the file name without its full path from v:this_session and add it to my status line?
For this, Vim has the :help filename-modifiers like :t for the last component of the file. You can use them with the fnamemodify() function (or expand() if you want to modify a built-in Vim file identifier like %):
:echo fnamemodify(v:this_session, ':t')
Since the forward slash character is used as path separator in Vim on all
operating systems, to obtain the filename from a full path it is sufficient to
extract the last component of the path. For doing that one can can use the
matchstr() or substitute() functions:
substitute(t, '^.*/', '', '')
or
matchstr(t, '[^/]*$')

How can I make org-protocol work on Openbox?

I tried the instructions - I am using Firefox on Lubuntu (Openbox). But I get the error
"Firefox doesn't know how to open this address, because the protocol (org-protocol) isn't associated with any program".
How should I fix this?
The following steps for setting up org-protocol work with Ubuntu 16.04 (Xenial Xerus) and presumably later versions. Org-mode is assumed to have already been set-up (and installed using apt-get install org-mode or via the ELPA repository).
Set-up
Add .desktop file
Create and save a file called org-protocol.desktop to ~/.local/share/applications containing:
[Desktop Entry]
Name=org-protocol
Exec=emacsclient %u
Type=Application
Terminal=false
Categories=System;
MimeType=x-scheme-handler/org-protocol;
Then run:
$ update-desktop-database ~/.local/share/applications/
This step makes Firefox aware that "org-protocol" is a valid scheme-handler or protocol (by updating ~/.local/share/applications/mimeinfo.cache), and causes Firefox to prompt for a program to use when opening these kinds of links.
Add config settings to ~/.emacs.d/init.el (or ~/.emacs) file
Have the following settings in your Emacs configuration file:
(server-start)
(require 'org-protocol)
Also add some template definitions to the configuration file, for example:
(setq org-protocol-default-template-key "l")
(setq org-capture-templates
'(("t" "Todo" entry (file+headline "/path/to/notes.org" "Tasks")
"* TODO %?\n %i\n %a")
("l" "Link" entry (file+olp "/path/to/notes.org" "Web Links")
"* %a\n %?\n %i")
("j" "Journal" entry (file+datetree "/path/to/journal.org")
"* %?\nEntered on %U\n %i\n %a")))
Now run Emacs.
Create your notes.org file
Assuming you use the capture templates defined in step 2, you will need to prepare a notes.org file at the location you specified in step 2. You must create this file -- if it is not created along with the headlines specified in step 2, org-mode will just give a warning when you try to capture web-pages. So, given the capture templates from step 2, notes.org should contain the following:
* Tasks
* Web Links
Add bookmarklet(s) to Firefox
Save bookmark to toolbar containing something like the following as the location:
javascript:location.href='org-protocol://capture?template=l&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&body='+encodeURIComponent(window.getSelection())
If you are using an older version of org-mode, you may need to use the following instead:
javascript:location.href='org-protocol://capture://l/'+encodeURIComponent(location.href)+'/'+encodeURIComponent(document.title)+'/'+encodeURIComponent(window.getSelection())
Notice the 'l' (lowercase L) in the above URL -- this is what chooses the capture template (automatically) -- it is the key one would normally have to press when capturing with org-mode via C-c c.
When you click on this bookmarklet, Firefox will ask what program to use to handle the "org-protocol" protocol. You can simply choose the default program that appears ("org-protocol").
Using it
(Optionally) select some text on a webpage you're viewing in Firefox. When you click on the bookmarklet, the link and selected text will be placed in the Emacs capture buffer. Go to Emacs, modify the capture buffer as desired, and press C-c C-c to save it.
Add protocol handler
Create file ~/.local/share/applications/org-protocol.desktop containing:
[Desktop Entry]
Name=org-protocol
Exec=emacsclient %u
Type=Application
Terminal=false
Categories=System;
MimeType=x-scheme-handler/org-protocol;
Note: Each line's key must be capitalized exactly as displayed, or it will be an invalid .desktop file.
Then update ~/.local/share/applications/mimeinfo.cache by running:
On GNOME:
update-desktop-database ~/.local/share/applications/
On KDE:
kbuildsycoca4
Configure Emacs
Init file
Add to your Emacs init file:
(server-start)
(require 'org-protocol)
Capture template
You'll probably want to add a capture template something like this:
("w" "Web site"
entry
(file+olp "/path/to/inbox.org" "Web")
"* %c :website:\n%U %?%:initial")
Note: Using %:initial instead of %i seems to handle multi-line content better.
This will result in a capture like this:
\* [[http://orgmode.org/worg/org-contrib/org-protocol.html][org-protocol.el – Intercept calls from emacsclient to trigger custom actions]] :website:
[2015-09-29 Tue 11:09] About org-protocol.el
org-protocol.el is based on code and ideas from org-annotation-helper.el and org-browser-url.el.
Configure Firefox
Expose protocol-handler
On some versions of Firefox, it may be necessary to add this setting. You may skip this step and come back to it if you get an error saying that Firefox doesn't know how to handle org-protocol links.
Open about:config and create a new boolean value named network.protocol-handler.expose.org-protocol and set it to true.
Note: If you do skip this step, and you do encounter the error, Firefox may replace all open tabs in the window with the error message, making it difficult or impossible to recover those tabs. It's best to use a new window with a throwaway tab to test this setup until you know it's working.
Make bookmarklet
Make a bookmarklet with the location:
javascript:location.href='org-protocol://capture://w/'+encodeURIComponent(location.href)+'/'+encodeURIComponent(document.title)+'/'+encodeURIComponent(window.getSelection())
Note: The w in the URL chooses the corresponding capture template. You can leave it out if you want to be prompted for the template.
When you click on this bookmarklet for the first time, Firefox will ask what program to use to handle the org-protocol protocol. If you are using Ubuntu 12.04 (Precise Pangolin), you must add the /usr/bin/emacsclient program, and choose it. With Ubuntu 12.10 (Quantal Quetzal) or later, you can simply choose the default program that appears (org-protocol).
You can select text in the page when you capture and it will be copied into the template, or you can just capture the page title and URL.
Tridactyl
If you're using Tridactyl, you can map key sequences something like this:
bind cc js location.href='org-protocol://capture://w/'+encodeURIComponent(content.location.href)+'/'+encodeURIComponent(content.document.title)+'/'+encodeURIComponent(content.document.getSelection())
You might also want to add one for the `store-link` sub-protocol, like:
bind cl js location.href='org-protocol://store-link://'+encodeURIComponent(content.location.href)+'/'+encodeURIComponent(content.document.title)
Capture script
You may want to use this script to capture input from a terminal, either as an argument or piped in:
#!/bin/bash
if [[ $# ]]
then
data="$#"
else
data=$(cat)
fi
if [[ -z $data ]]
then
exit 1
fi
encoded=$(python -c "import sys, urllib; print urllib.quote(' '.join(sys.argv[1:]), safe='')" "${data[#]}")
# "link" and "title" are not used, but seem to be necessary to get
# $encoded to be captured
emacsclient "org-protocol://capture://link/title/$encoded"
Then you can capture input from the shell like this:
tail /var/log/syslog | org-capture
org-capture "I can capture from a terminal!"
These instructions are more up-to-date than the ones in Mark's answer.

Resources