For new files, Sublime Text changes the file name to the first line of the file:

enter image description here

Is it possible to achieve this on Vim?


I don't use Sublime Text, but something like this sounds like what you want:

function! SetFileNameToFirstLine()
  silent execute "file ".fnameescape(getline(1))

let s:set_file_name_to_first_line_active = 0
function! ToggleSetFileNameToFirstLine()
  if s:set_file_name_to_first_line_active
    silent! augroup! SFNTFL
    echo "SetFileNameToFirstLine disabled"
    augroup SFNTFL
      au! InsertLeave,CursorHold,CursorHoldI <buffer> call SetFileNameToFirstLine()
    augroup END
    call SetFileNameToFirstLine()
    echo "SetFileNameToFirstLine enabled"
  let s:set_file_name_to_first_line_active = !s:set_file_name_to_first_line_active

nnoremap <F9> :call ToggleSetFileNameToFirstLine()<CR>

F9 will start changing your filename to the first line. Hit it again to disable this behaviour. (You can make it default, but I really recommend against it...)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.